南郵JAVA程式設計實驗3 流處理常式設計

來源:互聯網
上載者:User

標籤:南郵java程式設計實驗

南郵JAVA程式設計實驗3  流處理常式設計


實驗目的:

要求學生能在學習和理解課堂學習內容中JAVA流編程理論的基礎上,學習並逐步掌握JAVA流程式的編寫和調試,學習根據處理需求對不同流的正確選擇使用和組合使用方法。


實驗內容:

設計和編寫一個程式從鍵盤讀入一行字串,將其寫入一個文字檔中,再編寫另一個程式從文字檔中讀入字串並在命令列視窗顯示出來。

實驗程式1:

用資料的基本的讀入Scanner讀入一行,用緩衝流寫入檔案。

import java.io.*;import java.util.*;public class JavaOut {    public static void main(String args[]){        Scanner input = new Scanner(System.in); //讀入流        System.out.println("請輸入一行字串:");        String str = input.nextLine();      //讀入一整行字串        File file = new File("test.txt");   //建立檔案        try{            FileWriter output = new FileWriter(file, true);            BufferedWriter outB = new BufferedWriter(output);    //檔案緩衝流            outB.write(str);  //檔案寫入字串            outB.newLine();   //換行            outB.close();     //關閉檔案            outB.close();        }        catch(IOException e){            System.out.println(e);        }    }}

實驗程式2:

用緩衝流讀入檔案,基本輸出輸出整行。

import java.io.*;import java.util.*;public class JavaIn {    public static void main(String args[]){        try{            FileReader input = new FileReader("test2.txt");            BufferedReader inB = new BufferedReader(input); //檔案讀入緩衝流的建立            String s = "";            while((s = inB.readLine()) != null){//整行讀入,直到檔案末尾                System.out.println(s);            }            inB.close();            input.close();        }        catch(IOException e){            System.out.println(e);        }    }}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

南郵JAVA程式設計實驗3 流處理常式設計

聯繫我們

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