java實現輸入輸出資料流代碼分享_java

來源:互聯網
上載者:User

1,編寫一個程式,讀取檔案test.txt的內容並在控制台輸出。如果源檔案不存在,則顯示相應的錯誤資訊。

 package src;  import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException;  public class Test {    public static void main(String[] args) {     File f = new File("src\\test.txt");//檔案在src名為test.txt中     try {       FileReader fr = new FileReader(f);//將檔案讀取到內容中       int m;       while((m=fr.read())!=-){         System.out.print((char)(m));       }     } catch (FileNotFoundException e) {       // TODO Auto-generated catch block       e.printStackTrace();     } catch (IOException e) {       // TODO Auto-generated catch block       e.printStackTrace();     }   } }

2,編寫一個程式實現如下功能,從目前的目錄下的檔案fin.txt中讀取80個位元組(實際讀到的位元組數可能比80少)並將讀來的位元組寫入目前的目錄下的檔案fout.txt中

 package src;  import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;  public class Test {    public static void main(String[] args) {     File f = new File("src\\fin.txt");//src下fin.txt檔案     File f = new File("src\\fout.txt");//src下fout.txt檔案          try {       FileInputStream fis = new FileInputStream(f);       FileOutputStream fos = new FileOutputStream(f);              byte[] temp = new byte[];//定義一個位元組數組       fis.read(temp);//讀到記憶體中       fos.write(temp);//寫到檔案              fis.close();       fos.close();     } catch (FileNotFoundException e) {       // TODO Auto-generated catch block       e.printStackTrace();     } catch (IOException e) {       // TODO Auto-generated catch block       e.printStackTrace();     }        } }

3,使用java的輸入/輸出流技術將一個文字檔的內容按行讀出,每讀出一行就順序添加行號,並寫入到另一個檔案中。

 package src;  import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;  public class Test {    public static void main(String[] args) {     File f = new File("src\\fin.txt");//src下fin.txt檔案     File f = new File("src\\fout.txt");//src下fout.txt檔案          try {       FileReader fr = new FileReader(f);       FileWriter fw = new FileWriter(f);              BufferedReader br = new BufferedReader(fr);       BufferedWriter bw = new BufferedWriter(fw);              String temp;       int i=;       while((temp=br.readLine())!=null){         bw.write(i+","+temp);         bw.newLine();//換行         i++;       }       bw.flush();//把緩衝區內容寫到檔案       br.close();       bw.close();       br.close();       bw.close();     } catch (FileNotFoundException e) {       // TODO Auto-generated catch block       e.printStackTrace();     } catch (IOException e) {       // TODO Auto-generated catch block       e.printStackTrace();     }   } }

4,編寫一個程式,接收從鍵盤輸入的資料,並把從鍵盤輸入的內容寫到input.txt檔案中,如果輸入"quit",則程式結束。

 package src;  import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner;  public class Test {    public static void main(String[] args) {     File f = new File("src\\input.txt");     try {       FileWriter fw = new FileWriter(f);       Scanner scanner = new Scanner(System.in);       String temp;       while(!((temp=scanner.nextLine()).equals("quit"))){         fw.write(temp);       }       fw.close();     } catch (IOException e) {       // TODO Auto-generated catch block       e.printStackTrace();     }   } }

5,編寫一個程式實現如下功能,檔案fin.txt是無行結構(無分行符號)的漢語檔案,從fin中讀取字元,寫入檔案fou.txt中,每40個字元一行(最後一行可能少於40個字)

 package src;  import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Test {    public static void main(String[] args) {     File f=new File("src\\fin.txt");     File f=new File("src\\fout.txt");     try {       FileReader fr=new FileReader(f);       FileWriter fw=new FileWriter(f);              char temp[]=new char[];       int len;       while((len=fr.read(temp))!=-)       {         if(len==)          fw.write(new String(temp)+"\n");         else           fw.write(temp, , len);       }       fr.close();       fw.close();            } catch (FileNotFoundException e) {       // TODO 自動產生的 catch 塊       e.printStackTrace();     } catch (IOException e) {       // TODO 自動產生的 catch 塊       e.printStackTrace();     }   } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.