一:字符流
字符输入流
写入文件字符流
import java.io.FileWriter; import java.io.IOException; //fileWriter public class Demo10 { public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("d:\\1764\\bbb.txt"); for (int i = 0; i < 10; i++) { fw.write("helloWorld"); fw.flush(); } fw.close(); System.out.println("运行完毕"); } }
字符输出流
写入文件输出流
import java.io.*; public class Demo09 { public static void main(String[] args) throws IOException { /*FileInputStream fis = new FileInputStream("d:\\1764\\aaa.txt"); int count = 0; while ((count = fis.read()) != -1){//12个字节4个汉字 System.out.print((char) count); } fis.close();*/ FileReader fr = new FileReader("d:\\1764\\aaa.txt"); char[] b = new char[1]; int data = 0; while ((data = fr.read(b)) != -1){ System.out.println(new String(b,0,data)); } fr.close(); } }
复制文本文件
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; //使用FileReader只能复制文本文件 //字节流可以复制任意文件 //字符流只能复制文本文件 public class Demo11 { public static void main(String[] args) throws IOException { char a = ‘a‘; System.out.println(a); FileReader fr = new FileReader("d:\\1764\\aaa.txt"); FileWriter fw = new FileWriter("d:\\1764\\ccc.txt"); int data = 0; while ((data = fr.read()) != -1){//返回值是int System.out.println((char) data); fw.write(data); fw.flush(); } fr.close(); fw.close(); } }