节点流
定义:程序用于直接操作目标设备所对应的类叫节点流
节点流: FileInputStream、FileOutputStream、 FileReader、 FileWriter
FileReader
public void test() throws IOException {
// 路径,相较于当前module,如果是在main()中则相较于当前工程
// 1、实例化File类的对象,指明要操作的文件
File file = new File("src\\com\\IO\\File\\hello.txt");
System.out.println(file.getAbsolutePath());
// 2、提供具体的流
FileReader fileReader = new FileReader(file);
// 3、数据的读入
// read(): 返回读入的一个字符,如果达到文件的末位,返回-1
// 方式一
// int data = fileReader.read();
// while (data != -1) {
// System.out.print((char) data);
// data = fileReader.read();
// }
// 方式二:语法上针对于方式一的修改
int data;
while ((data = fileReader.read()) != -1) {
System.out.print((char) data);
}
// 4、关闭流
fileReader.close();
}
优化:异常方面:为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally处理
操作方法:选住内容,再Ctrl+Alt+T
public void testFileReader() {
FileReader fileReader = null;
try {
// 路径,相较于当前module,如果是在main()中则相较于当前工程
// 1、实例化File类的对象,指明要操作的文件
File file = new File("src\\com\\IO\\File\\hello.txt");
System.out.println(file.getAbsolutePath());
// 2、提供具体的流
fileReader = new FileReader(file);
// 3、数据的读入
// read(): 返回读入的一个字符,如果达到文件的末位,返回-1
// 方式一
// int data = fileReader.read();
// while (data != -1) {
// System.out.print((char) data);
// data = fileReader.read();
// }
// 方式二:语法上针对于方式一的修改
int data;
while ((data = fileReader.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4、关闭流
try {
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
对read()操作升级:使用read()的重载方法
// 对read()操作升级:使用read()的重载方法
@Test
public void testFileReader1() {
FileReader fileReader = null;
try {
// 1、File类的实例化
File file = new File("src\\com\\IO\\File\\hello.txt");
// 2、FileReader流的实例化
fileReader = new FileReader(file);
// 3、读入的操作
// read(char[] cbuf):返回每次读入cbuf数组中的字符的个数,如果达到文件末尾,返回-1
char[] cbuf = new char[5];
int len;
while ((len = fileReader.read(cbuf)) != -1) {
// 方式一
// 错误写法:每次cbuf[]都会被覆盖,如果没覆盖完整,则可能输出多余的字符
// 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 string = new String(cbuf);
// System.out.print(string);
// 正确写法
java.lang.String s = new java.lang.String(cbuf, 0, len);
System.out.print(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4、资源的关闭
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileWriter
/*
从内存中写出数据到硬盘的文件里
说明:
1、输出操作,对应的File文件可以不存在。
2、File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件
File对应的硬盘中的文件如果存在:
如果流使用的构造器是:FileWriter(file) / FileWriter(file,false):对原有文件的覆盖
如果流使用的构造器是:FileWriter(file,true):不会对原有文件的覆盖,而是在原有文件上添加内容
*/
//
@Test
public void testFileWriter(){
FileWriter fileWriter = null;
try {
// 1、提供File类的对象,指明写出到的文件
File file = new File("src\\com\\IO\\File\\hello1.txt");
// 2、提供FileWriter的对象,用于数据的写出。"append":是否只是添加,false表示不在原有文件上添加,直接覆盖
fileWriter = new FileWriter(file, false);
// 3、写出操作
fileWriter.write("I have a dream!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileWriter != null) {
// 4、流资源的关闭
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileInputStream、FileOutputStream
1. 总结:
1.1、对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
1.2、对于非文本文件(.jpg,.mp3,.mp4,.avi,.dox,.ppt,...),使用字节流来处理
/*
文本文件如果只含有除中文以外的符号,则也可以使用FileInputStream,但是FileInputStream读中文文字就会出现乱码
因为FileInputStream读出来的是字节byte,英文一个字节就可以存得下,中文需要三个字节才可以存得下
总结:
1、对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
2、对于非文本文件(.jpg,.mp3,.mp4,.avi,.dox,.ppt,...),使用字节流来处理
*/
public void FileInputStreamTest() {
FileInputStream fileInputStream = null;
try {
// 1、File类的实例化
File file = new File("src\\com\\IO\\File\\FileInputOutputStreamTest\\hello.txt");
// 2、FileReader流的实例化
fileInputStream = new FileInputStream(file);
// 3、读入的操作
// read(char[] cbuf):返回每次读入cbuf数组中的字符的个数,如果达到文件末尾,返回-1
byte[] buffer = new byte[5];
int len;
while ((len = fileInputStream.read(buffer)) != -1) {
String string = new String(buffer, 0, len);
System.out.print(string);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2. 处理非文本文件
/*
实现对图片的复制操作
*/
public void FileInputStreamTest1() {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
// 1、创建File
File srcfile = new File("src\\com\\IO\\File\\FileInputOutputStreamTest\\进击的巨人.png");
File desfile = new File("src\\com\\IO\\File\\FileInputOutputStreamTest\\进击的巨人copy.png");
// 2、创建FileInputStream流
fileInputStream = new FileInputStream(srcfile);
fileOutputStream = new FileOutputStream(desfile);
// 3、读入和写出
byte[] buffer = new byte[5];
int len;
while ((len = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer);
}
} catch (IOException e) {
e.printStackTrace