import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.FileNotFoundException;public class InputStreamReaderTest { public static void main(String[] args) { BufferedReader br=null; BufferedWriter bw=null; String str=null; FileReader fr= null; FileWriter fw = null; String path ="d:/fuck.txt"; int c=0; br=new BufferedReader(new InputStreamReader(System.in));//不知道这是不是缓冲输入流 System.out.print("请输入数据:"); try {// str=br.readLine();//从键盘上输入一个字符串,以回车结束。 fw = new FileWriter(path); bw = new BufferedWriter(fw); bw.write("太阳出来东方亮,山炮啥样我啥样\\n……"); while(str != null){//用了循环可以多输入几次。 if(str.equalsIgnoreCase("exit")){ break; } System.out.println("从键盘上输入的数据为:" + str);//输出屏幕 bw.write(str);//写到文本 //bw.newLine(); System.out.print("请输入数据:"); str=br.readLine(); } bw.flush(); System.out.println("end"); }catch(FileNotFoundException e){ } catch (IOException e) { } finally{ try { if(br != null) br.close(); } catch (IOException e) { } try { bw.close(); } catch (IOException e) { } try { fw.close(); } catch (IOException e) { } } try { fr = new FileReader(path); while((c = fr.read())!= -1){//从文本读取 System.out.print((char)c); } System.out.println(); } catch (FileNotFoundException e){ } catch (Exception e) { }}}
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class TestBufferStream1 { public static void main(String args[]) { try { String outname = "C:\\zhang.txt"; File f = new File(outname); //f.createNewFile(); FileInputStream fis = new FileInputStream("C:/dat.txt"); // 这里声明了一个FileInputStream准备从里面往外读数据 FileOutputStream out = new FileOutputStream(outname); byte[] buff = new byte[1024]; int c = 0; while(true) { c= System.in .read(buff); String st = new String(buff,0,c); System.out.println(st); if("stop".equals(st.trim())) { System.exit(0); break; } out.write(buff, 0, c); } //fis.close();// 然后关闭 } catch (IOException e) {// 这是异常... e.printStackTrace(); } }}