文件字符流FileReader和FileWriter的使用
文件的输入
从文件中读取到内存(程序)中
步骤:
- 建立一个流对象,将已存在的一个文件加载进流
FileReader fr = new FileReader(new File("Test.txt"))
; - 创建一个临时存放数据的数组
char[] ch = new char[1024]
; - 调用流对象的读取方法将流中的数据读入到数组中
fr.read(ch)
; - 关闭资源
fr.close()
;
代码示例:
@Test
public void testFileReader1() {
FileReader fr = null;
try {
// 1.File类的实例化,文件不存在就会报FileNotFoundException
File file = new File("D:\\io\\hello.txt");
// 2.FileReader流的实例化
fr = new FileReader(file);
// 3. 读入的操作
// read(char[] cbuf):返回每次读入cbuf数据中的字符的个数。如果达到文件末尾,返回-1
char[] cbuf = new char[1024];
int len;
while ((len = fr.read(cbuf)) != -1) {
String str = new String(cbuf, 0, len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭资源
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
注意点:
-
read()
的理解:返回读入的一个字符。如果达到文件末尾,返回-1 - 异常的处理:为了保证流资源一定可以执行关闭操作。需要使用
try-catch-finally
处理 - 读入的文件一定要存在,否则就会报
FileNotFoundException
。
文件的输出
从内存(程序)到硬盘文件中
步骤:
- 创建流对象,建立数据存放文件
File Writer fw = new File Writer(new File("Test.txt"))
- 调用流对象的写入方法,将数据写入流
fw.write("HelloWord")
- 关闭流资源,并将流中的数据清空到文件中
fw.close()
;
代码示例:
@Test
public void testFileWriter() {
FileWriter fw = null;
try {
// 1.提供File类的对象,指明写出到的文件
File file = new File("D:\\io\\hello.txt");
// 2.提供FileWriter的对象,用于数据的写出
// false:表示不在文件末尾追加,而是直接覆盖原有文件内容。
fw = new FileWriter(file, true);
// 3.写出的操作
fw.write("I have a dream!\n");
fw.write("you need to have a dream!");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.流资源的关闭
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
小练习
实现文本文件的复制操作
/**
* 实现文本的复制操作
*/
@Test
public void testFileReaderFileWriter() {
FileWriter fw = null;
FileReader fr = null;
try {
// 1.创建File类的对象,指明读入和写出的文件
File srcFile = new File("D:\\io\\hello.txt");
File destFile = new File("D:\\io\\hello2.txt");
// 2.创建输入流和输出流的对象
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
// 3.数据的读入和写出操作
char[] cbuf = new char[5];
int len; // 记录每次读入到cbuf数组中的字符的个数
while ((len = fr.read(cbuf)) != -1) {
// 每次写出len个字符
fw.write(cbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭资源
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
文件字节流FileInputStream和FileOutputStream的使用
文件字节流操作与字符流操作类似,只是实例化对象操作和数据类型不同。
代码示例:
// 使用字节流FileInputStream处理文本文件,可能出现乱码
@Test
public void testFileInputStream() {
FileInputStream fis = null;
try {
//1.指定要读取的文件
File file = new File("D:\\io\\hello.txt");
// 2.造流
fis = new FileInputStream(file);
// 3.读数据
byte[] buffer = new byte[5];
int len; // 记录每次读取的自己的个数
while ((len = fis.read(buffer)) != -1) {
String str = new String(buffer, 0, len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
// 4.关闭资源
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
小练习
实现图片文件复制操作
@Test
public void testFileInptStreamAndFileOutputStream() {
FileOutputStream fos = null;
FileInputStream fis = null;
try {
// 1.创建File对象
File srcFile = new File("D:\\io\\test.jpg");
File destFile = new File("D:\\io\\test2.jpg");
// 2.创建流
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
// 3.复制图片,想复制快一点,byte数组设置大一点
byte[] buffer = new byte[5];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭资源
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
注意点
- 定义路径时,可以用
/
或\\
。 - 输出操作,对应的
File
可以不存在的。并不会报异常。 -
File
对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件。 -
File
对应的硬盘中的文件如果存在:- 如果流使用的构造器是:
FileWriter(file, false)
或FileWriter(file)
:对原有文件的覆盖。 - 如果流使用的构造器是:
FileWriter(file,true)
:不会对原有文件覆盖,而是在原有文件基础上追加内容。
- 如果流使用的构造器是:
- 读取文件时,必须保证文件存在,否则会报异常。
- 对于文本文件(
.txt
,.java
,.c
,.cpp
),使用字符流处理。 - 对于非文本文件(
.jpg
,.mp3
,.mp4
,.avi
,.doc
,.ppt
,...),使用字节流处理。
网友评论