一.文件流(字节流,字符流)
1.字节流
package com.zijie; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class TestFileInputStream { public static void main(String[] args) { int b = 0; FileInputStream in = null; try{ in = new FileInputStream("e:\\go\\file.txt"); } catch(FileNotFoundException e) { System.out.println("找不到指定的文件"); System.exit(-1); } try{ long num = 0; // 返回-1的话就表示已经读到了文件的结尾 while((b = in.read()) != -1) { System.out.print((char)b); num++; } in.close(); System.out.println("\n\n共读取了" + num + "个字节"); } catch(IOException e1) { System.out.println("读取文件时出现异常"); System.exit(-1); } } }
package com.zijie; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class TestFileOutputStream { public static void main(String[] args) { int b = 0; FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("e:\\go\\file.txt"); // OutputStream有这个文件就往这个文件里面写, 没有的话就自动创建一个 out = new FileOutputStream("e:\\go\\fileNew.txt"); // 一边读, 一边写 while ((b = in.read()) != -1) { out.write(b); } } catch (FileNotFoundException e) { System.out.println("找不到指定文件"); System.exit(-1); } catch (IOException e) { System.out.println("文件复制出错"); System.exit(-1); } System.out.println("文件成功复制"); } }
2.字符流
package com.zifu; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class TestFileReader { public static void main(String[] args) { FileReader fr = null; int c = 0; try { fr = new FileReader("e:\\go\\file.txt"); while ((c = fr.read()) != -1) { System.out.print((char) c); } fr.close(); } catch (FileNotFoundException e) { System.out.println("文件未找到"); System.exit(-1); } catch (IOException e) { System.out.println("读取文件时出现异常"); System.exit(-1); } } }
package com.zifu; import java.io.FileWriter; import java.io.IOException; public class TestFileWriter { public static void main(String[] args) { FileWriter fw = null; try { fw = new FileWriter("e:\\go\\unicode000.dat"); for (int i = 1; i <= 50000; i++) { fw.write(i); } } catch (IOException e) { System.out.println("写入文件出错 !"); System.exit(-1); } } }
二.缓冲流
IO的缓冲区的存在就是为了提高效率,把要操作的数据放进缓冲区,然后一次性把缓冲区的内容写到目的地,而不是写一次就往目的地写一次.
在这里要注意的是当我们关闭了缓冲区对象实际也关闭了与缓冲区关联的流对象.
package com.buffer; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class TestBufferRW { public static void main(String[] args) { try { BufferedWriter bw = new BufferedWriter(new FileWriter("e:\\go\\bwrite.txt")); BufferedReader br = new BufferedReader(new FileReader("e:\\go\\bwrite.txt")); String s = null; for (int i = 0; i < 100; i++) { s = "" + Math.random(); //bw.write(s); bw.append(s); bw.newLine(); } bw.flush(); // 特别好用的方法, readLine while((s = br.readLine()) != null) { System.out.println(s); } br.close(); bw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
package com.buffer; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class TestBufferStream { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("e:\\go\\bufferedfile.txt"); BufferedInputStream bis = new BufferedInputStream(fis); int c = 0; System.out.println((char)bis.read()); System.out.println((char)bis.read()); /* while((c = bis.read()) != -1) { System.out.print((char)c+", "); }*/ // 标记到第30的位置再开始读数据 bis.mark(100); for (int i = 0; i <= 10 && (c = bis.read()) != -1; i++) { System.out.print((char)c); } System.out.println(); // 回到mark标记的那个地方 bis.reset(); for (int i = 0; i <= 10 && (c = bis.read()) != -1; i++) { System.out.print((char)c); } bis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
三.转换流
转换流是指将字节流与字符流之间的转换,包含两个类:InputStreamReader和OutputStreamWriter。
转换流的出现方便了对文件的读写,她在字符流与字节流之间架起了一座桥梁,使原本毫无关联的两种流操作能够进行转化,提高了程序的灵活性。
package com.convert; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; public class TestTranForm1 { public static void main(String[] args) { OutputStreamWriter osw = null; try { osw = new OutputStreamWriter(new FileOutputStream("e:\\go\\newreader.txt")); osw.write("山东淄博"); // 默认使用当前系统的字符编码 System.out.println(osw.getEncoding()); osw.close(); // FileOutputStream加第二个参数true表示追加内容 osw = new OutputStreamWriter(new FileOutputStream("e:\\go\\newreader.txt", true), "utf-8"); osw.write("qwerttttt"); System.out.println(osw.getEncoding()); osw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
时间: 2024-10-31 22:25:30