java io流

來源:互聯網
上載者:User
import java.io.*;/*io流細節@  blog.csdn.net/ljfbest*/class Test {public static void main(String[] args) throws IOException {/*FileWriter fw=new FileWriter(new File("a.txt"),true);//在windows中,一個換行相當於\r\n兩個字元,而linux中則是\nfw.write("ssss\r\n");//勿忘重新整理緩衝區,不然寫不到a.txt中,fr.close()前也會自動重新整理一次fw.flush();*/FileReader fr=new FileReader("a.txt");char[] ch1=new char[3];/*a.txt:1. abcde--->abcdec (關鍵理解原理) 2. abc (此處一個換行,在windows中相當於兩個字元,故輸出以下)    e    --->abc         e*/while((fr.read(ch1))!=-1)System.out.print(ch1);//System.out.print(new String (buf,0,num));fr.close();}}import java.io.*;/*字元流的緩衝區 BufferedWriter,BufferedReader*/class Test {public static void main(String[] args) throws IOException {/*FileWriter fw=new FileWriter("a.txt",true);BufferedWriter bf=new BufferedWriter(fw);bf.write("ccc");bf.newLine();//輸入一個分行符號,windows下是\r\n,linux下是\nbf.write("aaa");bf.flush();bf.close();*/BufferedWriter bw=new BufferedWriter(new FileWriter("b.txt"));BufferedReader br=new BufferedReader(new FileReader("Test.java"));String line=null;while((line=br.readLine())!=null){bw.write(line);bw.newLine();bw.flush();}bw.close();br.close();}}import java.io.*;/*字元流的緩衝區 BufferedWriter,BufferedReader*/class Test {public static void main(String[] args) throws IOException {/*FileOutputStream fs=new FileOutputStream("c.txt");fs.write("你好!".getBytes());//必須要轉化為位元組數組//此時不需要重新整理,flush()fs.close();*/FileInputStream fi=new FileInputStream("c.txt");/*int ch;while((ch=fi.read())!=-1)System.out.println((char)ch);        */byte[] by=new byte[1024];int len;while((len=fi.read(by))!=-1)System.out.println(new String(by,0,len));fi.close();}}import java.io.*;import java.util.*;class Test {public static void main(String[] args) throws IOException {/* // 轉換流:將位元組流轉成字元流再使用緩衝區的readLine()InputStream in=System.in;InputStreamReader is=new InputStreamReader(in);//轉換流BufferedReader bf=new BufferedReader(is);String line=null;while((line=bf.readLine())!=null)System.out.println(line);*//*//PrinterWriter  PrinterStreamBufferedReader bf=new BufferedReader(new InputStreamReader(System.in));//此處不用BufferedWriter改用PrintWriter;如果為 true,則 println、printf 或 format 方法將重新整理輸出緩衝區PrintWriter out=new PrintWriter(System.out,true);//此處寫到控制台上,重新整理只對流起作用//PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter("a.txt")));//此處要轉換成流,才能自動重新整理String line=null;while((line=bf.readLine())!=null)out.println(line);out.close();bf.close();*//*DataInputStream in=new DataInputStream(System.in);int n=in.readInt();System.out.println(n);in.close();//注意:DataInputStream與DataOutputStream必須成對出現*//*BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));String s=bf.readLine();byte[] b=s.getBytes("GBK");//編碼    省略時系統預設的String s1=new String(b,"GBK");///解碼  省略時系統預設的System.out.println(s1);//System.out.println(Arrays.toString(b));*/BufferedInputStream bf=new BufferedInputStream(System.in);byte[] b=new byte[10];bf.read(b);String s=new String(b);System.out.println(s);}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.