标签:stat hello new while stream 创建文件 fileread 存在 har
操作字节:
输入字节流:FileInputStream
输出字节流:FileOutputStream
操作字符:
字符输入流:FileReader
字符输出流:FileWriter
1.FileInputStream 字节输入流[用于读取文件]:
eg1:
public static void yInputStream() throws IOException { //创建字节输入流 FileInputStream fis=new FileInputStream("F:\\temp\\Test.java");//文件不存在自动创建 //创建一个长度为1024的竹筒 byte[] buf=new byte[1024]; //用于保存实际读取的字节数 int hasRead=0; while ((hasRead=fis.read(buf))>0){ System.out.println(new String(buf,0,hasRead)); } fis.close(); }
eg2:
public static void yPrintStream() throws IOException { FileOutputStream fos=new FileOutputStream("F:\\temp\\OutTemp.txt"); PrintStream ps=new PrintStream(fos);//PrintStream 是打印输出流 ps.println("夜归偶怀故人独孤景略"); ps.println(new Test()); fos.close(); }
2.FileOutputStream 字节输出流[用于写入文件]:
public static void yOutputStream() throws IOException { //创建文件输出流 FileOutputStream fos=new FileOutputStream("F:\\temp\\OutTemp.txt"); fos.write(("String世界").getBytes()); //注意需转化为字节 fos.close(); }
3.FileReader 字符输入流[用于读取文件]:
public static void yReader() throws IOException { //创建字符输入流 FileReader fr=new FileReader("F:\\temp\\OutTemp.txt"); //用于保存实际读取的字符数 char[] buf=new char[1024]; int hasRead=0; while((hasRead=fr.read(buf))>0){ //将字符转换成字符串 System.out.println(new String(buf,0,hasRead)); } fr.close(); }
4.字符输出流[用于写入文件]:
public static void yWriter() throws IOException { FileWriter fw=new FileWriter("F:\\temp\\WriteTemp.txt");//文件不存在自动创建 fw.write("夜归偶怀故人独孤景略\r\n"); fw.write("买醉村场半夜归,"); fw.write("西山落月照柴扉。\r\n"); fw.write("刘琨死後无奇士,"); fw.write("独听荒鸡泪满衣!\r\n"); fw.close(); }
/*
* BufferedWriter:将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
* BufferedReader:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
*/
public static void WriteTest() throws IOException { //创建输出缓冲流对象 BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt")); bw.write("hello"); bw.close(); } public static void ReaderTest() throws IOException { //创建输入缓冲流对象 BufferedReader br = new BufferedReader(new FileReader("Introduce.txt")); /* //一次读写一个字符 int ch; while((ch=br.read())!=-1){ System.out.print((char)ch); } */ //一次读写一个字符数组 char[] chs = new char[1024]; int len; while((len=br.read(chs))!=-1) { System.out.print(new String(chs,0,len)); } //释放资源 br.close(); }
/*
* 缓冲流的特殊功能:
* BufferedWriter
* void newLine():写一个换行符,这个换行符由系统决定
* BufferedReader
* String readLine():一次读取一行数据,但是不读取换行符
*/
public static void main(String[] args) throws IOException, IOException { /* BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt")); for(int x=0; x<10; x++) { bw.write("hello"+x); //bw.write("\r\n"); bw.newLine(); bw.flush(); } bw.close(); */ BufferedReader br = new BufferedReader(new FileReader("test.txt")); /* String line = br.readLine(); System.out.println(line); line = br.readLine(); System.out.println(line); line = br.readLine(); System.out.println(line); line = br.readLine(); System.out.println(line); */ String line; //1:br.readLine() //2:line=br.readLine() //3:line != null while((line=br.readLine())!=null) { System.out.println(line); } br.close(); }
标签:stat hello new while stream 创建文件 fileread 存在 har
原文地址:https://www.cnblogs.com/yanghe123/p/11715835.html