文章目录
对于文本文件(.txt,.java,.c,.cpp)
,使用字符流处理。
对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,等)
使用字节流处理。
如果文本文件仅仅想复制一下,其实使用字节流也可以,但是不可以在内存中去读文件。
一、FileReader字符输入流
- read():返回读入的一个字符,如果达到文件末尾,返回-1。
- 异常的处理:为了保证流资源一定可以执行关闭操作。需要用try-catch-finally处理。
- 读入的文件一定要存在,否则会报FileNotFoundException();
使用FileReader字符输入流读取文档中的数据(一次读取一个字节):
@Test
public void testFileRead1() {
FileReader fr = null;
try {
File file = new File("hello.txt");
fr = new FileReader(file);
int data;
while ((data = fr.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用FileReader字符输入流读取文档中的数据(一次读取一个数组):
@Test
public void testFileRead2() {
FileReader fr = null;
//1.File类的实例化
try {
File file = new File("hello.txt");
//2.FileReader流的实例化
fr = new FileReader(file);
//3.读入的操作
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1) {
// for(int i=0;i<len;i++){
// System.out.print(cbuf[i]);
// }
System.out.print(new String(cbuf, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
//4.资源的关闭
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
程序执行上面两种方式,都能成功将文档中的数据加载进内存并打印到控制台:
二、FileWriter字符输出流
- 输出操作对应的File可以不存在。
- File对应的磁盘中文件如果不存在,在输出过程中,会自动创建此文件。
- File对应的磁盘中的文件如果存在:
-
- 若使用的构造器是:FileWriter(file) /FileWriter(file,false);对原有文件进行覆盖。
-
- 若使用的构造器是:FileWriter(file,true);对原有文件进行追加。
使用FileWriter字符输出流写出数据到磁盘:
@Test
public void testFileWriter1() {
FileWriter fw = null;
try {
//1.提供File类的对象,指明要写出的文件
File file = new File("hello1.txt");
//2.提供FileWriter对象,用于数据的写出
fw = new FileWriter(file, false);//false是覆盖原有内容
//3.写的操作
fw.write("I have a dream!\n");
fw.write("you new too have a dream!");
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.流资源的关闭
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
程序执行,成功创建出hello1.txt文件并写入数据:
三、使用FileReader和FileWriter实现文本文件的复制
使用字符输入输出流实现文本copy:
@Test
public void testCopy() {
FileReader fr = null;
FileWriter fw = null;
try {
//1.创建File类的对象,指明要读入和写出的文件
File srcfile = new File("hello1.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 (fw != null) {
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
程序执行,成功在当前modul下copy了hello2.txt文件:
四、FileInputStream 字节输入流与FileOutputStream 输出流
使用FileInputStream 字节输入流与FileOutputStream 输出流实现非文本(.jpg)格式文件的copy:
@Test
public void testCopyFile() {
long start = System.currentTimeMillis();
String srcPath = "D:\\Program Files\\JAVA\\workspace_idea\\JavaSenior\\com.java.io\\img1.jpg";
String destPath = "D:\\Program Files\\JAVA\\workspace_idea\\JavaSenior\\com.java.io\\img1copy.jpg";
copyFile(srcPath, destPath);
long end = System.currentTimeMillis();
System.out.println("复制文件花费的时间为:" + (end - start));
}
public void copyFile(String srcPath, String destPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File srcFile = new File(srcPath);
File descFile = new File(destPath);
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(descFile);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
程序执行后,成功copy了img1.jpg非文本文件: