大佬教程收集整理的这篇文章主要介绍了JavaIO流之FileReader与FileWriter,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
抽象基类 InputStream OutputStream Reader Writer
节点流(或文件流) FileInputStream (read(byte[] buffer)) FiLeoutputSteam (write(byte[] buffer,0,len)) FileReader (read(char[] cbuf)) FileWriter (write(char[] cbuf,0,len))
缓冲流(处理流的一种) BufferedInputStream (read(byte[] buffer)) BufferedOutputStream (write(byte[] buffer,0,len)) / flush() BufferedReader (read(char[] cbuf) / readLine()) BufferedWriter (write(char[] cbuf,0,len)) / flush()
使用FileReader将当前module下的Hello.txt文件内容读入程序中,并输出到控制台。 说明点:
@Test
public void testFileReader() {
FileReader fr = null;
try {
//1.实例化File类的对象,指明要操作的文件
File file = new File("Hello.txt");
//2.提供具体的流
fr = new FileReader(filE);
//3.数据的读入
//read():返回读入的一个字符。如果达到文件末尾,返回-1
int data;
while ((data = fr.read())!= -1){
System.out.print((char)data);
}
} catch (IOException E) {
e.printStackTrace();
} finally {
//4.流的关闭操作
try {
if (fr != null)
fr.close();
} catch (IOException E) {
e.printStackTrace();
}
}
}
//对read()操作升级:使用read的重载方法
@Test
public void testFileReader1(){
FileReader fr = null;
try {
//1.File类的实例化
File file = new File("Hello.txt");
//2.FileReader流的实例化
fr = new FileReader(filE);
//3.读入的操作
//read(char[] cbuf):返回每次读入cbuf数组中的字符的个数。如果达到文件末尾,返回-1
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1){
//方式一:
//错误写法
// for (int i = 0; i < cbuf.length; i++) {
// System.out.print(cbuf[i]);
// }
//正确写法
// for (int i = 0; i < len; i++) {
// System.out.print(cbuf[i]);
// }
//方式二:
//错误写法:对应着方式一的错误写法
// String str = new String(cbuf);
// System.out.print(str);
//正确写法
String str = new String(cbuf, 0, len);
System.out.print(str);
}
} catch (IOException E) {
e.printStackTrace();
} finally {
//4.资源的关闭
if (fr != null) {
try {
fr.close();
} catch (IOException E) {
e.printStackTrace();
}
}
}
}
从内存中写出数据到硬盘的文件里 说明: 输出操作,对应的File可以不存在的。并不会报异常 File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件。 File对应的硬盘中的文件如果存在:
@Test
public void testFileWriter() {
FileWriter fw = null;
try {
//1.提供File类的对象,指明写出到的文件
File file = new File("Hello1.txt");
//2.提供FileWriter的对象,用于数据的写出
fw = new FileWriter(filE);
//3.写出的操作
fw.write("I have a dream!n");
fw.write("You need to have a dream!");
} catch (IOException E) {
e.printStackTrace();
} finally {
if (fw != null) {
//4.流资源的关闭
try {
fw.close();
} catch (IOException E) {
e.printStackTrace();
}
}
}
}
使用FileReader和FileWriter来实现文本文件的读入和写出操作(相当于复制该文件) 测试代码:
@Test
public void testFRFW() {
FileReader fr = null;
FileWriter fw = null;
try {
//1.创建File类对象,指明读入和写出的文件
File srcFile = new File("Hello.txt");
File destFile = new File("Hello2.txt");
//2.创建输入流和输出流的对象
fr = new FileReader(srcFilE);
fw = new FileWriter(destFilE);
//3.数据的读入和写出操作
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1){
fw.write(cbuf,0,len);
}
} catch (IOException E) {
e.printStackTrace();
} finally {
//4.关闭流资源
try {
if (fr != null)
fr.close();
} catch (IOException E) {
e.printStackTrace();
}
try {
if (fw != null)
fw.close();
} catch (IOException E) {
e.printStackTrace();
}
}
}
以上是大佬教程为你收集整理的JavaIO流之FileReader与FileWriter全部内容,希望文章能够帮你解决JavaIO流之FileReader与FileWriter所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。