四个节点流:
FileReader使用。
read初级方法,一个一个字符读入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 @Test public void test01 () { FileReader fr = null ; try { File file = new File("hello.txt" ); fr = new FileReader(file); int data = fr.read(); while (data != -1 ){ System.out.println((char )data); data = fr.read(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fr != null ) fr.close(); } catch (IOException e) { e.printStackTrace(); } } }
read重载方法,一次读入一个char数组。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 @Test public void test02 () { FileReader fr = null ; try { File file = new File("hello.txt" ); fr = new FileReader(file); char [] cbuf = new char [5 ]; int len; while ((len = fr.read(cbuf)) != -1 ) { String str = new String(cbuf,0 ,len); System.out.print(str); } } catch (IOException e) { e.printStackTrace(); } finally { if (fr != null ){ try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } }
说明:
read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1
异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理
读入的文件一定要存在,否则就会报FileNotFoundException
输入的标准化过程总结: ① 创建File类的对象,指明读取的数据的来源。(要求此文件一定要存在) ② 创建相应的输入流,将File类的对象作为参数,传入流的构造器中 ③ 具体的读入过程: 创建相应的byte[] 或 char[]。 ④ 关闭流资源 说明:程序中出现的异常需要使用try-catch-finally处理。
OutputStream & Writer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 void write (int b/int c) ;void write (byte [] b/char [] cbuf) ;void write (byte [] b/char [] buff, int off, int len) ;void flush () ;void close () ; 需要先刷新,再关闭此流即以 String 对象作为参数 void write (String str) ;void write (String str, int off, int len) ;用于写出非文本数据之类的原始字节流。要写出字符流,需要使用 FileWriter
FileWriter使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 @Test public 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 have a dream!" ); } catch (IOException e) { e.printStackTrace(); } finally { if (fw != null ){ try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
说明:
输出操作,对应的File可以不存在的。并不会报异常 File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件。 File对应的硬盘中的文件如果存在: 如果流使用的构造器是:FileWriter(file,false) / FileWriter(file):对原文件的覆盖 如果流使用的构造器是:FileWriter(file,true):不会对原文件覆盖,而是在原文件基础上追加内容 因为字符流直接以字符作为操作单位,所以 Writer 可以用字符串来替换字符数组, 即以 String 对象作为参数 void write(String str); void write(String str , int off, int len);
输出的标准化过程总结: ① 创建File类的对象,指明写出的数据的位置。(不要求此文件一定要存在) ② 创建相应的输出流,将File类的对象作为参数,传入流的构造器中 ③ 具体的写出过程: write(char[]/byte[] buffer,0,len) ④ 关闭流资源
说明:程序中出现的异常需要使用try-catch-finally处理。
使用FileReader和FileWriter实现文本文件的复制。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 @Test public void test03 () { FileReader fr = null ; FileWriter fw = null ; try { File srcfile = new File("hao.txt" ); File destfile = new File("hello.txt" ); 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 { if (fr != null ){ try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } if (fw != null ){ try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
FileInputStream / FileOutputStream使用。
对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,…),使用字节流处理
想要复制图片,只要将FileReader和FileWriter改成相应的FileInputStream和FileOutputStream就行。
用FileInput(Output)Stream这两个类也能读入写出文本文件,不过需要做一些处理,否则可能乱码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 @Test public void test4 () { FileInputStream fis = null ; FileOutputStream fos = null ; try { File srcfile = new File("miku.png" ); File destfile = new File("test.png" ); 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 { try { if (fis != null ) fis.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fos != null ) fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
相对路径在IDEA和Eclipse中使用的区别?
IDEA:
如果使用单元测试方法,相对路径基于当前的Module的。
如果使用main()测试,相对路径基于当前Project的。 Eclipse:
单元测试方法还是main(),相对路径都是基于当前Project的。