Java IO學習筆記六

  • 2019 年 12 月 31 日
  • 筆記

文章目錄

  1. 1. 列印流
    1. 1.1. PrintStream
      1. 1.1.1. 構造函數
      2. 1.1.2. 常用的函數
      3. 1.1.3. 實例
    2. 1.2. PrintWriter
      1. 1.2.1. 構造函數
      2. 1.2.2. 常用函數
      3. 1.2.3. 實例
    3. 1.3. 參考文章

列印流

  • 在整個IO包中,列印流是輸出資訊最方便的類,主要包含位元組列印流PrintStream)和字元列印流PrintWrite)。列印流提供了非常方便的列印功能,可以列印任何的數據類型,例如:小數、整數、字元串等等,相對於前面學習的幾個文件的操作來說,這裡的列印流是最簡便的一個類了

PrintStream

  • 主要功能是格式化的將內容寫入文件,並不是列印在控制台上
  • PrintStream最大的好處就是可以格式化的輸出,相信學過c的朋友都知道prinf這格式化輸出函數,這裡的PrintStream實現了更加簡單的格式化輸出,不需要使用什麼%d,%f了,只需要都是用%s即可,這個很像python
  • PrintStream 列印的所有字元都使用平台的默認字元編碼轉換為位元組。在需要寫入字元而不是寫入位元組的情況下,應該使用 PrintWriter類。

構造函數

  • PrintStream(File file) 創建具有指定文件且不帶自動行刷新的新列印流。
  • PrintStream(OutputStream out) 創建新的列印流。
  • PrintStream(OutputStream out, boolean autoFlush) 創建新的列印流,並且設置自動刷新
  • PrintStream(String fileName) 創建具有指定文件名稱且不帶自動行刷新的新列印流。
File file_2=new File("/tmp"+File.separator+"test"+File.separator+"test.txt");  PrintStream printStream=new PrintStream(file_2);  //直接使用File  PrintStream printStream=new PrintStream(new FileOutputStream(file_2));  //使用OutputStream的子類FileOutputStream

常用的函數

  • PrintStream append(char c) 在此輸入流的後面追加字元。
  • PrintStream append(CharSequence csq) 將指定字元序列添加到此輸出流。
  • PrintStream append(CharSequence csq, int start, int end) 將指定字元序列的子序列添加到此輸出流。
  • print() 列印常用的數據類型,比如String,char,int ,double,float,boolean,long,short
  • println() 列印常用的數據類型,但是帶有換行符
  • printf(String format, Object... args) 使用指定格式字元串和參數將格式化的字元串寫入此輸出流的便捷方法。
  • format(String format, Object... args) 使用指定格式字元串和參數將格式化字元串寫入此輸出流中。
  • close()
  • flush()

實例

package IO;  import java.io.*;  /**   * Created by chenjiabing on 17-5-25.   */      /**test.txt文件中的結果如下:   陳加兵   2299.9   姓名:陳加兵,n年齡:22,成績:99.9   姓名:陳加兵,n年齡:22,成績:99.9   c   chenjiabi   */  public class demo9 {      public static void main(String[] args) {          PrintStream printStream = null;          File file_1 = new File("/tmp" + File.separator + "test");          File file_2 = new File("/tmp" + File.separator + "test" + File.separator + "test.txt");          if (!file_1.exists()) {              file_1.mkdir();              System.out.println("文件創建成功");          }          try {  //            PrintStream printStream=new PrintStream(file_2);              printStream = new PrintStream(new FileOutputStream(file_2));              String name = "陳加兵";              int age = 22;              float grade = 99.9f;              printStream.println(name);//println()              printStream.print(age);//print()              printStream.println(grade);//print()              printStream.format("姓名:%s,n年齡:%s,成績:%s%s", name, age, grade, "n");//format()              printStream.printf("姓名:%s,n年齡:%s,成績:%s%s", name, age, grade, "n");              printStream.append('c');   //append              printStream.append("nchenjiabing",0,10);  //append          } catch (FileNotFoundException e) {              e.printStackTrace();          } finally {              if (printStream != null) {                  printStream.flush();                  printStream.close();              }          }      }  }

PrintWriter

  • 繼承Writer,主要針對的是字元流的操作
  • 向文本輸出流列印對象的格式化表示形式。此類實現在 PrintStream中的所有 print 方法。它不包含用於寫入原始位元組的方法,對於這些位元組,程式應該使用未編碼的位元組流進行寫入。
  • PrintStream 類不同,如果啟用了自動刷新,則只有在調用 printlnprintfformat 的其中一個方法時才可能完成此操作,而不是每當正好輸出換行符時才完成。這些方法使用平台自有的行分隔符概念,而不是換行符。
  • 此類中的方法不會拋出 I/O 異常,儘管其某些構造方法可能拋出異常。客戶端可能會查詢調用 checkError() 是否出現錯誤。

構造函數

  • PrintWriter(File file) 使用指定文件創建不具有自動行刷新的新 PrintWriter
  • PrintWriter(OutputStream out) 根據現有的 OutputStream 創建不帶自動行刷新的新 PrintWriter
  • PrintWriter(OutputStream out, boolean autoFlush) 通過現有的 OutputStream 創建新的 PrintWriter
  • PrintWriter(String fileName) 創建具有指定文件名稱且不帶自動行刷新的新 PrintWriter

常用函數

這裡的常用到的函數和PrintStream的差不多就不再詳細的列出來了,詳情請看幫助文檔

實例

package IO;  import java.io.*;  /**   * Created by chenjiabing on 17-5-25.   */  public class demo10 {      public static void main(String[] args)      {          PrintWriter printWriter=null;          File file=new File("/tmp"+File.separator+"test"+File.separator+"file.txt");          try {              printWriter=new PrintWriter(new FileOutputStream(file));              printWriter.println("chenjiabing");              printWriter.println("陳加兵");          }catch (IOException e)          {              e.printStackTrace();          }finally {                  if(printWriter!=null)                  {                      printWriter.close();                  }          }      }  }

參考文章