第九章輸入輸出資料流1-4

來源:互聯網
上載者:User
Code:
  1. package ch09;   
  2.   
  3. import java.io.BufferedReader;   
  4. import java.io.BufferedWriter;   
  5. import java.io.CharArrayReader;   
  6. import java.io.CharArrayWriter;   
  7. import java.io.File;   
  8. import java.io.FileInputStream;   
  9. import java.io.FileNotFoundException;   
  10. import java.io.FileReader;   
  11. import java.io.FileWriter;   
  12. import java.io.IOException;   
  13. import java.util.Scanner;   
  14.   
  15. class Homeworkch09 {   
  16.     public Homeworkch09() {   
  17.         System.out.println("第九章的課後練習題。");   
  18.     }   
  19.   
  20.     // 1.編寫一個應用程式,讀取一個文字檔的內容。   
  21.     public void key01() {   
  22.         File file = new File("E:/Workspaced/javaHomeWork", "hello.txt"); // 與當前應用程式同一目錄   
  23.         // System.out.println(file.getPath());   
  24.         byte b[] = "dd".getBytes();   
  25.         try {   
  26.   
  27.             FileInputStream fis = new FileInputStream(file);   
  28.             int n = 0;   
  29.             // 測試亂碼   
  30.             String str = null;   
  31.             while ((n = fis.read(b, 0, 2)) != -1) { // 兩個位元組讀,防止亂碼   
  32.                 str = new String(b, 0, n);   
  33.                 System.out.print(str);   
  34.             }   
  35.         } catch (FileNotFoundException e) { // 檔案找不到的異常   
  36.             e.printStackTrace();   
  37.         } catch (IOException e) {   
  38.             e.printStackTrace();   
  39.         }   
  40.     }   
  41.   
  42.     // 2.編寫一個應用程式,將使用者從鍵盤輸入的10行文字存入檔案。   
  43.     public void key02() {   
  44.         FileWriter file = null;   
  45.         try {   
  46.             file = new FileWriter("myDoc.txt");   
  47.             BufferedWriter bw = new BufferedWriter(file);   
  48.             for (int i = 0; i < 10; i++) {   
  49.                 Scanner reader = new Scanner(System.in);   
  50.                 bw.write(reader.nextLine());   
  51.                 bw.flush();   
  52.             }   
  53.             file.close();   
  54.             bw.close();   
  55.         } catch (IOException e) {   
  56.             e.printStackTrace();   
  57.         }   
  58.     }   
  59.   
  60.     // 3,使用數組字元流將俄文字母寫入記憶體,然後再從記憶體讀出。   
  61.     /** 答案輸出俄文字母是32個,實際是33個,Ё和Е雖然也是兩個不同的字母,但只是在口語中有區別,書寫或印刷時可以把簡略為Е。 */  
  62.     public void key03() {   
  63.   
  64.         int startPosition = (int) 'А';// 俄文首字母   
  65.         int endPosition = (int) 'Я'; // 俄文末字母   
  66.         System.out.println("俄文字母共有:" + (startPosition - endPosition));   
  67.         // for(int i=1040;i<1072;i++){   
  68.         // System.out.print((char)i);   
  69.         // }   
  70.         int n = -1;   
  71.   
  72.         CharArrayWriter out = new CharArrayWriter();   
  73.         for (int i = startPosition; i <= endPosition; i++) {   
  74.             out.write(i);   
  75.         }   
  76.         CharArrayReader in = new CharArrayReader(out.toCharArray());   
  77.         try {   
  78.             while ((n = in.read()) != -1) {   
  79.                 if (n % 2 == 0) {   
  80.                     System.out.println("/n");   
  81.                 }   
  82.                 System.out.printf("/t位置%d,字元/'%c/'", n, (char) n);   
  83.             }   
  84.         } catch (IOException e) {   
  85.             e.printStackTrace();   
  86.         }   
  87.     }   
  88.   
  89.     // 4、編寫一個應用程式,將使用者從鍵盤輸入的10個整數存入檔案,然後順序讀出。   
  90.     public void key04() {       //=============使用緩衝流=================   
  91.         File file = new File("key04.txt");   
  92.         try {   
  93.             FileWriter out = new FileWriter(file);   
  94.             BufferedWriter out2 = new BufferedWriter(out);   
  95.             int i = 0;   
  96.             while (i < 2) {   
  97.                 Scanner s = new Scanner(System.in);   
  98.                 String str = s.nextLine();   
  99.                 out2.write(str);   
  100.                 out2.newLine();// 寫入一個回行   
  101.                 out2.flush(); // 重新整理緩衝區   
  102.                 i++;   
  103.             }   
  104.                            
  105.             FileReader in = new FileReader(file);   
  106.             BufferedReader in2 = new BufferedReader(in);   
  107.                
  108.             String integers=null;   
  109.             while(( integers = in2.readLine())!=null){   
  110.                 System.out.println(integers);   
  111.             }      
  112.         } catch (FileNotFoundException e) {   
  113.             e.printStackTrace();   
  114.         } catch (IOException e) {   
  115.             e.printStackTrace();   
  116.         }   
  117.     }   
  118.   
  119. }   
  120.   
  121. public class Ch09_1 {   
  122.   
  123.     public static void main(String[] args) {   
  124.         Homeworkch09 hk = new Homeworkch09();   
  125.         //測試位置         
  126.     }   
  127. }  

 

聯繫我們

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