天天看點

javaSE23-- IO流--位元組流中常用類FileInputStream/FileOutputStream

目錄

  • ​​位元組流中常用類FileInputStream/FileOutputStream​​
  • ​​讀寫檔案的步驟FileInputStream​​
  • ​​以位元組為機關讀取檔案的内容​​
  • ​​以位元組為機關讀取檔案的内容, 循環讀取​​
  • ​​異常處理​​
  • ​​一次讀取一個位元組, 手動關閉流, 異常處理​​
  • ​​從檔案中讀取位元組儲存到位元組數組中, 異常處理, 自動 關閉流​​
  • ​​FileOutputStream以位元組為機關把資料儲存到檔案中​​
  • ​​檔案的複制​​

位元組流中常用類FileInputStream/FileOutputStream

  • 位元組輸入流 FileInputStream
  • 位元組輸出流 FileOutputStream

讀寫檔案的步驟FileInputStream

public static void main(String[] args) throws IOException {
    //1)在目前程式與指定的檔案之間建立流通道
    FileInputStream fis = new FileInputStream("d:/abc.txt");
    
    //2) 讀寫檔案内容
    fis.read();
    
    //3)關閉流通道
    fis.close();
  }      

以位元組為機關讀取檔案的内容

read()方法從檔案中讀取一個位元組, 并把講到的位元組傳回, 講到檔案末尾傳回-1

//讀取d:/abc.txt檔案的内容, 通過構造方法指定要通路的檔案,如果檔案不存在會抛出異常
FileInputStream fis = new FileInputStream("d:/abc.txt");      

以位元組為機關讀取檔案的内容, 循環讀取

public static void main(String[] args) throws IOException {
    //1)在目前程式與指定的檔案之間建立流通道,
    //讀取d:/abc.txt檔案的内容, 通過構造方法指定要通路的檔案,如果檔案不存在會抛出異常
    FileInputStream fis = new FileInputStream("d:/abc.txt");
    //檔案内容: ABCabc
    
    //2) 讀取檔案内容, 
    //read()方法從檔案中讀取一個位元組, 并把講到的位元組傳回, 講到檔案末尾傳回-1
    int cc = fis.read();    //65, A的碼值
    while( cc != -1 ){
      //把讀到的位元組cc進行處理,  把cc轉換為字元再列印, 因為目前檔案中隻有英文字元,一個位元組就對應一個字元
      System.out.print(  (char)cc  );
      //繼續讀下個位元組
      cc = fis.read( );
    }
    
    
    //3)關閉流通道
    fis.close();
  }      
public static void main(String[] args) throws IOException {
    //1)在程式與讀取的檔案之間建立流通道
    FileInputStream fis = new FileInputStream("d:/abc.txt");
    //檔案ABCabcABC
    
    byte[] bytes = new byte[1024];
    
    //從流中讀取很多位元組, 儲存到位元組數組中, 傳回讀到的位元組數,如果讀到檔案末尾,傳回-1
    int len = fis.read(bytes);
    
    while(  len != -1 ){
      //從檔案中讀取了len個位元組儲存到了bytes數組中, 對len個位元組進行處理
      //把讀到的len個位元組轉換為字元串  new String(byte[]bytes , 0 , len)
      System.out.print( new String(bytes , 0 , len ));
      //繼續讀
      len = fis.read(bytes);
    }
    
    fis.close();
  }      

異常處理

一次讀取一個位元組, 手動關閉流, 異常處理
private static void m1() {    
    FileInputStream fis = null;
    try {
      fis = new FileInputStream("d:/abc.txt");
      
      int cc = fis.read();
      while( cc != -1 ){
        System.out.print( (char)cc );
        cc = fis.read();
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (fis != null) {
        try {
          fis.close();  //關閉流,釋放系統資源  
        } catch (IOException e) {
          e.printStackTrace();
        }        
      }
    }
    
  }      
從檔案中讀取位元組儲存到位元組數組中, 異常處理, 自動 關閉流
//從JDK7開始, 流可以自動關閉
  private static void m2() {
    try(    //try資源塊,自動釋放
        FileInputStream fis = new FileInputStream("d:/abc.txt");
        ) {
      byte[] bytes = new byte[4];
      int len = fis.read(bytes);
      while( len != -1){
        System.out.print( new String(bytes , 0 ,len));
        len = fis.read(bytes);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }    
  }      

FileOutputStream以位元組為機關把資料儲存到檔案中

  • byte[] bytes = “bjpowernode”.getBytes();
  • fos.write(bytes); //把bytes數組中所有的位元組儲存到檔案中
public static void main(String[] args) throws IOException {
    FileReader fr=  new FileReader("E:\\nihao.txt");
    FileWriter fr1=  new FileWriter("E:\\nihao1.txt",true);
    char[] chars=new  char[1024];

    int len=fr.read(chars);
    while (true){
        if (len==-1) break;
        fr1.write(chars,0,len);
        len=fr.read(chars);
    }
    fr.close();
    fr1.close();
}      

檔案的複制

public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("F:\\Users\\LENOVO\\Desktop\\微機實驗一.doc");

        File file = new File("F:\\Users\\LENOVO\\Desktop\\微機實驗一.doc");


        FileOutputStream out1 = new FileOutputStream("E:\\草稿1.doc");
        FileOutputStream out2 = new FileOutputStream("E:\\草稿2.doc");

        /*
        in.read() 每次一個位元組 讀取完畢傳回-1
        in.read(byte[] b) 傳回的是數組中實際裝的位元組數量
         */

        System.out.println("檔案大小\t"+file.length());

        Date date = new Date();
        System.out.println("第一次測試");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:sss");
        System.out.println(sdf.format(date));

        int a = in.read();
        while (a != -1) {
            out1.write(a);
            a = in.read();

        }

        Date date2 = new Date();

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:sss");

        System.out.println(sdf2.format(date2));

        System.out.println("========================");

        Date date3 = new Date();
        System.out.println("第二次測試");
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:sss");
        System.out.println(sdf3.format(date3));
        byte[] b = new byte[1024];
        int len = in.read(b);
        while (len != -1) {
            out2.write(b);
            len = in.read(b);
        }
        
        Date date4 = new Date();
        SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:sss");
        System.out.println(sdf4.format(date4));
       /* while (true) {
            if (len == -1) break;
            len = in.read(b);
            out.write(b, 0, 3);
        }*/
    }