java io读取文件操作代码实例
这篇文章主要介绍了javaio读取文件操作代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要分为字节读取和字符读取,字节读取可以一个一个读取和字节数组读取,字符读取同样之,字符读取适合文本读取,字节读取皆可以
这里直接上代码,读取文件的9个小demo
packagecom.io; importorg.junit.Test; importjava.io.*; publicclassFileTest{ //1、字节流字节一个一个读取 @Test publicvoidtest()throwsIOException{ InputStreamfis=newFileInputStream(newFile("E:\project_test\src\com\io\readme.txt")); intlen; //按字节一个一个读取 while((len=fis.read())!=-1){ System.out.print((char)len+"t"); }; fis.close(); } //输出结果helloworld //2、字节流字节数组读取 @Test publicvoidtest1()throwsIOException{ InputStreamfis=newFileInputStream(newFile("E:\project_test\src\com\io\readme.txt")); byte[]bytes=newbyte[2]; intlen; //按字节数组读取bytes存储的是读取的数据 while((len=fis.read(bytes))!=-1){ System.out.print((newString(bytes))+"t"); }; fis.close(); } //输出结果helloworld //3、缓冲字节流字节一个一个读取 @Test publicvoidtest2()throwsIOException{ BufferedInputStreambis=newBufferedInputStream(newFileInputStream(newFile("E:\project_test\src\com\io\readme.txt"))); intlen; while((len=bis.read())!=-1){ System.out.print((char)len+"t"); }; bis.close(); } //输出结果helloworld //4、缓冲字节流字节数组读取 @Test publicvoidtest3()throwsIOException{ BufferedInputStreambis=newBufferedInputStream(newFileInputStream(newFile("E:\project_test\src\com\io\readme.txt"))); byte[]bytes=newbyte[3]; intlen; //按字节数组读取bytes存储的是读取的数据 while((len=bis.read(bytes))!=-1){ System.out.print(newString(bytes)+"t"); }; bis.close(); } //输出结果helloworldrl //5、字符流一个一个读取 @Test publicvoidtest4()throwsIOException{ InputStreamReaderisr=newInputStreamReader(newFileInputStream(newFile("E:\project_test\src\com\io\readme.txt"))); intlen; //按字节数组读取bytes存储的是读取的数据 while((len=isr.read())!=-1){ System.out.print((char)len+"t"); }; isr.close(); } //6、字符流字符数组读取 @Test publicvoidtest5()throwsIOException{ InputStreamReaderisr=newInputStreamReader(newFileInputStream(newFile("E:\project_test\src\com\io\readme.txt"))); char[]chars=newchar[3]; intlen; //按字节数组读取bytes存储的是读取的数据 while((len=isr.read(chars))!=-1){ System.out.print(newString(chars)+"t"); }; isr.close(); } //7、缓冲字符流字符一个一个读取 @Test publicvoidtest6()throwsIOException{ BufferedReaderbr=newBufferedReader(newInputStreamReader(newFileInputStream(newFile("E:\project_test\src\com\io\readme.txt")))); intlen; //按字节数组读取bytes存储的是读取的数据 while((len=br.read())!=-1){ System.out.print((char)len+"t"); }; br.close(); } //8、缓冲字符流字符数组读取 @Test publicvoidtest7()throwsIOException{ BufferedReaderbr=newBufferedReader(newInputStreamReader(newFileInputStream(newFile("E:\project_test\src\com\io\readme.txt")))); char[]chars=newchar[3]; intlen; //按字节数组读取bytes存储的是读取的数据 while((len=br.read(chars))!=-1){ System.out.print(newString(chars)+"t"); }; br.close(); } //9、缓冲字符流按行读取 @Test publicvoidtest8()throwsIOException{ BufferedReaderbr=newBufferedReader(newInputStreamReader(newFileInputStream(newFile("E:\project_test\src\com\io\readme.txt")))); //按字节数组读取bytes存储的是读取的数据 Stringstr; while((str=br.readLine())!=null){ System.out.print(str+"t"); }; br.close(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。