5.14 流
文件流(字节流, 字符流)
FileInputStream,FileoutputStream
FileReader,FileWriter
FileInputStream:
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("c:\\go\\file.txt"); } catch (FileNotFoundException e) { System.out.println("找不到指定的文件"); System.exit(-1);//退出当前正在运行的虚拟机,0是正常退出 } 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); } } }
FileoutputStream:
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("c:\\go\\file.txt"); // OutputStream有这个文件就往这个文件里面写, 没有的话就自动创建一个 out = new FileOutputStream("c:\\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("文件成功复制"); } }
FileReader:
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("c:\\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); } } }
FileWriter:
import java.io.FileWriter; import java.io.IOException; public class TestFileWriter { public static void main(String[] args) { FileWriter fw = null; try { fw = new FileWriter("c:\\go\\unicode000.dat"); for (int i = 1; i <= 50000; i++) { fw.write(i); } } catch (IOException e) { System.out.println("写入文件出错 !"); System.exit(-1); } } }
缓冲流
BufferedReader
BufferedWriter
可以直接处理一行信息
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("c:\\go\\bwrite.txt")); BufferedReader br = new BufferedReader(new FileReader("c:\\go\\bwrite.txt")); String s = null; for (int i = 0; i < 100; i++) { s = "" + Math.random(); // bw.write(s); bw.append(s); // 等同于write 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(); } } }
BufferedWriter:
转换流--->字节-字符
InputStreamReader
数据流--->八大数据类型
DetaInputStream
八大数据类型
readInt()
readBoolean()
Print流--->System.out.println();
//打印流
Object流--->对象的流
Person p = new Person()
文件流
posted on 2018-05-14 16:27 sunyexiang 阅读(92) 评论(0) 编辑 收藏 举报