文件字符流
文件的输入
从文件中读取到内存(程序)中
步骤
- 建立一个流对象,将已存在的一个文件加载进流 FileReader fr = new FileReader(new File(“Test. txt”));
- 创建一个临时存放数据的数组 char[] ch = new char[1024];
- 调用流对象的读取方法将流中的数据读入到数组中 fr.read(ch);
关闭资源 fr.close(); ```java @Test public void testFileReader(){ FileReader fr = null; try {
// 1.实例化File类的对象,指明要操作的文件File file = new File("hello.txt"); // 相较于当前Module// 2.提供具体的流fr = new FileReader(file);// 3.数据的读入// read():返回读入的一个字符。如果达到文件末尾,返回-1// 原始// int data = fr.read();// while(data != -1) {// System.out.print((char)data);// data = fr.read();// }// 改进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();// }//或if(fr != null) {try {fr.close();} catch (IOException e) {e.printStackTrace();}}
} }
public static void main(String[] args) { File file = new File(“hello.txt”); // 相较于当前Project System.out.println(file.getAbsolutePath());
File file1 = new File("day09\\hello.txt");System.out.println(file1.getAbsolutePath());
}
**说明**- read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1- 异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理- 读入的文件一定要存在,否则就会报**FileNotFoundException**```java// 对read()操作升级:使用read的重载方法@Testpublic 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数组中的字符的个数。如果达到文件末尾,返回-1char[] 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]);// }// 正确的写法,应该是len// 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 {if(fr != null){// 4.资源的关闭try {fr.close();} catch (IOException e) {e.printStackTrace();}}}}
文件的输出
从内存(程序)到硬盘文件中
步骤
- 创建流对象,建立数据存放文件 FileWriter fw = new FileWriter(new File(“Test.txt”))
- 调用流对象的写入方法,将数据写入流 fw.write(“HelloWord”)
- 关闭流资源,并将流中的数据清空到文件中 fw.close();
说明
- 输出操作,对应的File可以不存在的,不会报异常
- File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件
File对应的硬盘中的文件如果存在
- 如果流使用的构造器是
**FileWriter(file, false)**/**FileWriter(file)**对原有文件的覆盖 如果流使用的构造器是
**FileWriter(file, true)**对原有文件基础上追加内容@Testpublic void testFileWriter() {FileWriter fw = null;try {File file = new File("hello1.txt");fw = new FileWriter(file, false);fw.write("I have a dream!\n");fw.write("you need to have a dream!");} catch (IOException e) {e.printStackTrace();} finally {if(fw != null){try {fw.close();} catch (IOException e) {e.printStackTrace();}}}}
- 如果流使用的构造器是
练习
@Testpublic void testFileReaderFileWriter() {FileReader fr = null;FileWriter fw = null;try {File srcFile = new File("hello.txt");File destFile = new File("hello2.txt");// File srcFile = new File("爱情与友情.jpg");// File destFile = new File("爱情与友情1.jpg");fr = new FileReader(srcFile);fw = new FileWriter(destFile);char[] cbuf = new char[5];int len;while((len = fr.read(cbuf)) != -1) {fw.write(cbuf, 0, len);}} catch (IOException e) {e.printStackTrace();} finally {try {if(fw != null)fw.close();} catch (IOException e) {e.printStackTrace();}try {if(fr != null)fr.close();} catch (IOException e) {e.printStackTrace();}}}
文件字节流
文件字节流操作与字符流操作类似,只是实例化对象操作和数据类型不同
// 使用字节流FileInputStream处理文本文件,可能出现乱码@Testpublic void testFileInputStream() {FileInputStream fis = null;try {// 1. 造文件File file = new File("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.print(str);}} catch (IOException e) {e.printStackTrace();} finally {if(fis != null){// 4.关闭资源try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}
练习
// 实现对图片的复制操作@Testpublic void testFileInputOutputStream() {FileInputStream fis = null;FileOutputStream fos = null;try {File srcFile = new File("爱情与友情.jpg");File destFile = new File("爱情与友情2.jpg");fis = new FileInputStream(srcFile);fos = new FileOutputStream(destFile);// 复制的过程byte[] buffer = new byte[5];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();}}}}
通用操作 ```java // 指定路径下文件的复制 public void copyFile(String srcPath, String destPath) { FileInputStream fis = null; FileOutputStream fos = null; try {
File srcFile = new File(srcPath);File destFile = new File(destPath);fis = new FileInputStream(srcFile);fos = new FileOutputStream(destFile);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();}}
} }
@Test public void testCopyFile(){ long start = System.currentTimeMillis(); String srcPath = “C:\Users\Administrator\Desktop\01-视频.avi”; String destPath = “C:\Users\Administrator\Desktop\02-视频.avi”; // String srcPath = “hello.txt”; // String destPath = “hello3.txt”; // 只是复制不会乱码
copyFile(srcPath, destPath);long end = System.currentTimeMillis();System.out.println("复制操作花费的时间为:" + (end - start)); // 618
} ``` 注意点
- 定义路径时,可以用“/”或“\”。
- 输出操作,对应的File可以不存在的,并不会报异常
- File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件
- File对应的硬盘中的文件如果存在
- 如果流使用的构造器是:FileWriter(file,false) / FileWriter(file)对原有文件的覆盖
- 如果流使用的构造器是:FileWriter(file,true)不会对原有文件覆盖,而是在原有文件基础上追加内容
- 读取文件时,必须保证文件存在,否则会报异常
- 对于文本文件(.txt, .java, .c, .cpp),使用字符流处理
- 对于非文本文件(.jpg, .mp3, .mp4, .avi, .doc, .ppt, …),使用字节流处理
