首页 > 编程知识 正文

FileOutputStream,java throwable

时间:2023-05-05 22:28:16 阅读:118373 作者:4078

JAVA中的FileInputStream 1、文件字节输入流、万能,任何种类的文件都可以采用该方法

2、使用字节方式、输入操作、读取操作(硬盘——内存)为byte[ ] )排列方式,读取的元素publicclassfileinputstreamtest 01 ) publicstaticvoidmamatest 01 /此处采用绝对路径方法,但相对路径\lucky 777\FLS=new file inputstream (c :_ userslucky 777\ idea projecky byte[] bytes=new byte[4],通过创建byte数组一次读取四个数据; int读取计数=0; //read )方法通过返回已读取数据的ASCII代码(/read )方法调用,按字节读取while ((read count=FLS.read (bytes ) )!=-1 ) (使用字符串构造方法将byte[]数组转换为字符串system.out.println (new string (bytes,0,readCount ) ); }catch(filenotfoundexceptione ) { e.printStackTrace ); }catch(ioexceptione ) { e.printStackTrace ); (finally(//关断流if ) Fls!=null () try ) Fls.close ); }catch(ioexceptione ) { e.printStackTrace ); } } } }} -如何设置相对路径:

read方法的详细byte数组byte[] bytes=new byte[4]; intreadcount=Fls.read(bytes; system.out.println (读取计数); //4读取计数使用导入的数据数String构造方法将byte[]数组转换为字符串system.out.println(newstring ) (byte[] ) readcount=Fls.read(bytes; //新读取的数据复盖旧数据,未读取数据的位数据为system.out.println (读取计数); //2 //不应该全部转换为String,而是应该旋转system.out.println (新字符串(字节,0,读取计数) ); FileInputStream类的两种常见方法: int available(); 返回流中剩余的没有读取到的字节数量

长脚本(longn ); 跳过n字节不读取

可以直接创建fls.available大小的byte[ ]数组来一次读取数据

但是,由于超出了byte[ ]数组的范围,因此不适用于大数据

publicclassfileinputstreamtest 04 { publicstaticvoidmain [ ] args } { fileinputstreamfls=null; try { FLS=new file inputstream (srcioio readme.txt ); skip ) )方法读取跳过两个字节的//fls.skip(2); available ) )方法//尚未加载,因此可以直接创建fls.available大小的数组,但大数据byte () bytes=newbyte[Fls.available //读一遍就足够了intreadcount=Fls.read(bytes ); system.out.println (新字符串) bytes,0,readCount ); }catch(filenotfoundexceptione ) { e.printStackTrace ); }catch(ioexceptione ) { e.printStackTrace ); (Finally ) if ) Fls!=null () try ) Fls.close ); }catch(ioexceptione ) { e.printStackTrace ); } } } }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。