package test; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; // 字节输入流 public class Test { public static void main(String[] args) { int b = -1; FileInputStream in = null; try { in = new FileInputStream("e:\changyong\file2.txt"); } catch (FileNotFoundException e) { System.out.println("找不到文件"); System.exit(0); //退出当前运行的虚拟机 0是正常退出 } try { long num = 0;// 记录读取了多少个字节 while((b = in.read()) != -1) { // in.read调用一次读一个字节 while循环内只能调用一次 System.out.print((char) b);// b是用来记录读的内容 num++; } in.close(); System.out.println(" 共读取了" + num + "个字节"); } catch (IOException e) { System.out.println("读取文件时出现异常"); System.exit(0); } } }
package test; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class TestShuChu { // 字节输出流 public static void main(String[] args) { int b = -1; FileInputStream in = null;// 输入流 = 空 FileOutputStream out = null;// 输出流 = 空 try { // 捕获异常 in = new FileInputStream("e:\changyong\file2.txt"); // 定义一个输入流指向一个文件 out = new FileOutputStream("e:\changyong\file2_new.txt"); // 输出流Output往里面写东西,没有的话就自动创建 while((b = in.read()) != -1){ // 先读 out.write(b); // 后写 } // 然后捕获异常 } catch (FileNotFoundException e) { System.out.println("找不到指定文件"); System.exit(0); }catch (IOException e) { System.out.println("文件复制出错"); System.exit(0); } System.out.println("文件复制成功"); } }
package test; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class TestZiFu { public static void main(String[] args) { FileReader fr = null; // 字符的输入流 处理汉字 int c = -1; try { fr = new FileReader("e:\changyong\zhongwen.txt"); long num = 0; while ((c = fr.read()) != -1) {// 字符流里的read,可以读取一个字节也可以读取一个字符 System.out.print((char) c); num++; } System.out.println(" 共读取了" + num + "个字节"); fr.close(); // 关闭一下整个流 } catch (FileNotFoundException e) { System.out.println("文件未找到"); System.exit(0); }catch (IOException e) { System.out.println("读取文件时出现异常"); System.exit(0); } } }
package test; import java.io.FileWriter; import java.io.IOException; public class TestZiFuW { // 往一个文件内写东西 public static void main(String[] args) { FileWriter fw = null; try { fw = new FileWriter("e:\changyong\file.txt"); String source = "learn how to write to file"; String stu = "中华人民共和国"; fw.write(source); fw.write(stu); fw.flush(); fw.close(); System.out.println("完成"); } catch (IOException e) { System.out.println("写入文件时出错"); System.exit(0); } } }
package test; 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 TestBufferH { public static void main(String[] args) { try { BufferedWriter bw = new BufferedWriter(new FileWriter("e:\changyong\buffer.txt")); BufferedReader br = new BufferedReader(new FileReader("e:\changyong\buffer.txt")); String s = null; for (int i = 0; i < 100; i++) { s = "" + Math.random(); // 取0-1的随机数不包括1,把整个结果变成字符串 bw.append(s);// 等同于bw.write(s),输出流 bw.newLine();// 输出一个换一个行 } bw.flush();// 强制刷新 while ((s = br.readLine()) != null) {// readLine读取一行 //System.out.println(s); } br.close(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } }