Java學習筆記43(列印流、IO流工具類簡單介紹),學習筆記工具類

來源:互聯網
上載者:User

Java學習筆記43(列印流、IO流工具類簡單介紹),學習筆記工具類

列印流:

有兩個類:PrintStream,PrintWriter類,兩個類的方法一致,區別在於構造器

PrintStream:構造方法:接收File類型,接收字串檔案名稱,接收位元組輸出資料流(OutputStream)

PringWriter:構造方法:接收File類型,接收字串檔案名稱,接收位元組輸出資料流(OutputStream),接收字元輸出資料流(Writer)

為其他流添加功能,可以方便地列印各種資料值,不同的是:它永遠不會拋出IO異常

 

方法:

package demo;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;public class PrintWriterDemo {    public static void main(String[] args) throws IOException {        function1();        function2();        function3();    }    public static void function1() throws FileNotFoundException {        File file = new File("d:\\1.txt");        PrintWriter pw = new PrintWriter(file);        pw.println(100);// 寫入的不是d,而是100,原樣列印        pw.write(100);// 寫入的是d        pw.flush();        pw.close();    }    public static void function2() throws FileNotFoundException {        FileOutputStream fos1 = new FileOutputStream("d:\\2.txt");        PrintWriter pw1 = new PrintWriter(fos1);        pw1.println("列印流");        pw1.flush();        pw1.close();    }    public static void function3() throws IOException {        FileWriter fw1 = new FileWriter("d:\\4.txt");        PrintWriter pw1 = new PrintWriter(fw1);        pw1.println("列印流");        pw1.flush();        pw1.close();    }}

 

列印流自動重新整理:

package demo;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintWriter;public class PrintWriterDemo {    public static void main(String[] args) throws IOException {        function1();    }    public static void function1() throws FileNotFoundException {        FileOutputStream fos1 = new FileOutputStream("d:\\1.txt");        PrintWriter pw1 = new PrintWriter(fos1, true);        // 第二個參數是否自動書重新整理,如果是,不需要寫flush方法        pw1.println("I");        pw1.println("Love");        pw1.println("You");        pw1.close();    }}

 

 

列印流複製文字檔:

package demo;import java.io.BufferedReader;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;public class Copy {    public static void main(String[] args) throws IOException {        BufferedReader bfr1 = new BufferedReader(new FileReader("d:\\1.txt"));        PrintWriter pw1 = new PrintWriter(new FileWriter("d:\\2.txt"), true);        String line = null;        while ((line = bfr1.readLine()) != null) {            pw1.println(line);        }        pw1.close();        bfr1.close();    }}

 

 

最後簡單寫下工具類,可以大幅度降低代碼量:

apache的commons工具類:

官網下載,複製到當前工程下的建立lib檔案夾,右鍵build path即可

幾個功能強大的常用方法:

package demo;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.apache.commons.io.FilenameUtils;public class CommonsDemo {    public static void main(String[] args) throws IOException {        function1();//檔案名稱操作        function2();//檔案操作    }    public static void function1(){        String name = FilenameUtils.getExtension("a.java");        System.out.println(name);//輸出:java                String filename = FilenameUtils.getName("d:\\b.java");        System.out.println(filename);//輸出:b.java                boolean a = FilenameUtils.isExtension("c.java", "java");        System.out.println(a);//輸出true,判斷檔案尾碼的方法    }            public static void function2() throws IOException{        //讀取文字檔的內容        String s1 = FileUtils.readFileToString(new File("d:\\1.txt"));        System.out.println(s1);                //寫文字檔        FileUtils.writeStringToFile(new File("d:\\b.txt"), "java");        //這裡就建立了一個文字檔,並寫入字串java                //複製檔案(不限於文本)        FileUtils.copyFile(new File("d:\\1.txt"), new File("d:\\11.txt"));                //複製檔案夾        FileUtils.copyDirectoryToDirectory(new File("f:\\new"), new File("d:\\new"));    }}

 

聯繫我們

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