Java基礎知識之IO(2)

來源:互聯網
上載者:User

標籤:java

檔案複製小案例(溫習Java基礎知識之IO(1)中的知識)
package copy;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class CopyDemo {    public static void main(String[] args) throws IOException {        String from = "E:"+File.separator+"psu.jpg";//要複製的檔案        String to = "C:" + File.separator;// 要儲存的檔案        File file_from = new File(from);// 執行個體化檔案        // 若源檔案不存在,則需要退出        if (!file_from.exists()) {            System.out.println("源檔案不存在!");            return;        }        File file_to = new File(to);//執行個體化檔案        //若目標位置不存在,退出        if(!file_to.exists()){            System.out.println("目標位置不存在!");        }        file_to = new File(to+"copy.jpg");//建立檔案        //執行個體化輸入,輸出資料流(相對於程式而言,資料的流向)        InputStream in = new  FileInputStream(file_from);        OutputStream out = new FileOutputStream(file_to);        int temp;        //邊度邊寫        while((temp = in.read())!=-1){            out.write(temp);        }        //關閉流        in.close();        out.close();    }}
記憶體操作流(ByteArrayInputStream、ByteArrayOutputStream)

注意:這兩個內所操作的資料位元置是相對於記憶體而言,從程式向記憶體寫入資料使用ByteArrayInputStream,反之使用ByteArrayOutputStream.

import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;/* * 將字串讀入到記憶體,將其轉換為大寫後,輸出 */public class ByteArrayDemo {    public static void main(String[] args) {        String str = "hello world";//需要操作的字串        //執行個體化流對象        ByteArrayOutputStream out = new ByteArrayOutputStream();        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes());        int temp;        while((temp=in.read())!=-1){            char c=(char)temp;            out.write(Character.toUpperCase(c));        }        //迴圈結束後,字串的內容儲存在記憶體中。        System.out.println(out.toString());    }}

重點注意資料的存在位置。

列印流(PrintStream、PrintWriter)

1、PrintStream
先觀察期構造方法:public PrintStream(OutputStream out)
執行個體化該類時,需要傳入位元組輸出資料流對象,我們知道OutputStream是一個抽象類別,依賴於其子類進行執行個體化,由此可以推測出,PrintStream類的執行個體可以根據傳入不同的執行個體化對象完成不同的功能,具體看例子。

package PrintStrem;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.PrintStream;public class PrintStremDemo {    private static PrintStream outPrint;    public static void main(String[] args) throws IOException, IOException {        File file = new File( "E:"+File.separator+"javaio"+File.separator+"FileOutputStream.txt");        OutputStream out = new FileOutputStream(file,true);        outPrint = new PrintStream(out);        //不換行輸出        outPrint.print(false);        outPrint.print(‘c‘);        //換行輸出        outPrint.println(true);        outPrint.println(123.3132);        //格式化輸出        String name = "高洋";int age = 20;        outPrint.printf("姓名:%s;年齡:%d", name,age);    }}

從例子中可以看出,這種輸出方法很方便,可以輸出任意類型資料。

BufferedReader

從緩衝區讀入資料,封裝輸入資料流,從而提高了讀入的效率,因為直接使用輸入資料流的read()方法,是直接對檔案進行操作,效率低下。

import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class BufferedReaderDemo {    public static void main(String[] args) throws IOException {        File file = new File("E:"+File.separator+"javaio"+File.separator+"FileOutputStream.txt");//建立檔案對象        //BufferedReader構造接收Reader類對象進行執行個體化,所以Reader類的子類的對象都可以為其執行個體化        BufferedReader read = new BufferedReader(new FileReader(file));        int temp = 0;        while((temp = read.read())!=-1){            char c = (char)temp;            System.out.print(c);        }        read.close();    }}

Java基礎知識之IO(2)

聯繫我們

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