1. 程式人生 > 資訊 >華納兄弟明年將為 HBO Max 獨家制作 10 餘部影片

華納兄弟明年將為 HBO Max 獨家制作 10 餘部影片

  

package ersatz;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Ersatz {
  public static void main(String[] args) {

  }

  @Test
  public void m1() {
    int data = 0;
    FileInputStream fileInputStream = null;
    try {
      fileInputStream = new FileInputStream("d:/n2");
      while ((data = fileInputStream.read()) != -1) {
        System.out.println((char) data + " " + data);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        assert fileInputStream != null;
        fileInputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  @Test
  public void m2() {
    byte[] buffer = new byte[2];
    int len = 0;
    FileInputStream fileInputStream = null;
    try {
      fileInputStream = new FileInputStream("d:/n2");
      while ((len = fileInputStream.read(buffer)) != -1) {
        System.out.println(new String(buffer, 0, len));
        for (byte b : buffer) {
          System.out.println(b);
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        assert fileInputStream != null;
        fileInputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  @Test
  public void m3() throws IOException {
    FileOutputStream fileOutputStream = null;
    try {
      fileOutputStream = new FileOutputStream("d:/n1", true);
//      fileOutputStream.write(97788);
      String s = "uiopvbn發生付款啦";
      fileOutputStream.write(s.getBytes(StandardCharsets.UTF_8), 7, 3);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      try {
        assert fileOutputStream != null;
        fileOutputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  @Test
  public void m4() {
    String src = "d:/a.png";
    String dst = "d:/b.png";
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
      fileInputStream = new FileInputStream(src);
      fileOutputStream = new FileOutputStream(dst);
      byte[] buffer = new byte[1024];
      int length = 0;
      while ((length = fileInputStream.read(buffer)) != -1) {
        fileOutputStream.write(buffer, 0, length);
      }
      System.out.println("ok");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (fileInputStream != null) fileOutputStream.close();
        if (fileOutputStream != null) fileOutputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

  

FileReader

package ersatz;

import org.junit.jupiter.api.Test;

import java.io.FileReader;
import java.io.IOException;

public class Ersatz {
  public static void main(String[] args) {

  }

  @Test
  public void fileReader1() {
    FileReader fileReader=null;
    int data;
    try {
      fileReader = new FileReader("d:/courgette.log");
      while ((data = fileReader.read()) != -1) {
        System.out.print((char) data);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        if (fileReader != null) {
          fileReader.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  @Test
  public void fileReader2() {
    FileReader fileReader=null;
    int length=0;
    char[] buffer = new char[8];
    try {
      fileReader = new FileReader("d:/courgette.log");
      while ((length = fileReader.read(buffer)) != -1) {
        System.out.print(new String(buffer, 0, length));
      }
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        if (fileReader != null) fileReader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

  

FileWriter

package ersatz;

import java.io.FileWriter;
import java.io.IOException;

public class Ersatz {
  public static void main(String[] args) {
    FileWriter fileWriter=null;
    char[] chars={'a', '發', '法', 'g'};

    try {
      fileWriter = new FileWriter("d:/n3",false);
      fileWriter.write(98);
      fileWriter.write(chars);
      fileWriter.write("范德薩浪費".toCharArray(),0,3);
      fileWriter.write("微軟微軟離開",0,2);
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        assert fileWriter != null;
        fileWriter.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

  

BufferedWriter

public class BufferedWriter_ {
    public static void main(String[] args) throws IOException {
        String filePath = "e:\\ok.txt";
        //建立BufferedWriter
        //說明:
        //1. new FileWriter(filePath, true) 表示以追加的方式寫入
        //2. new FileWriter(filePath) , 表示以覆蓋的方式寫入
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
        bufferedWriter.write("hello, 飛灑發生!");
        bufferedWriter.newLine();//插入一個和系統相關的換行
        bufferedWriter.write("hello2, 飛灑發生!");
        bufferedWriter.newLine();
        bufferedWriter.write("hello3, 飛灑發生!");
        bufferedWriter.newLine();

        //說明:關閉外層流即可 , 傳入的 new FileWriter(filePath) ,會在底層關閉
        bufferedWriter.close();

    }
}

  

BufferedReader

public class BufferedReader_ {
    public static void main(String[] args) throws Exception {

        String filePath = "e:\\a.java";
        //建立bufferedReader
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
        //讀取
        String line; //按行讀取, 效率高
        //說明
        //1. bufferedReader.readLine() 是按行讀取檔案
        //2. 當返回null 時,表示檔案讀取完畢
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }

        //關閉流, 這裡注意,只需要關閉 BufferedReader ,因為底層會自動的去關閉 節點流
        //FileReader。
        /*
            public void close() throws IOException {
                synchronized (lock) {
                    if (in == null)
                        return;
                    try {
                        in.close();//in 就是我們傳入的 new FileReader(filePath), 關閉了.
                    } finally {
                        in = null;
                        cb = null;
                    }
                }
            }

         */
        bufferedReader.close();

    }
}

  

Buffered copy\

package ersatz;

import java.io.*;

public class Ersatz {
  public static void main(String[] args) throws Exception {
    String src = "d:/a.png";
    String dst = "d:/c.png";
    BufferedInputStream bufferedInputStream=null;
    BufferedOutputStream bufferedOutputStream=null;
    int data;
    try {
      bufferedInputStream = new BufferedInputStream(new FileInputStream(src), 1024);
      bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(dst), 1024);
      while ((data = bufferedInputStream.read()) != -1) {
        bufferedOutputStream.write(data);
      }
      System.out.println("done");
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        if (bufferedInputStream != null) bufferedInputStream.close();
        if (bufferedOutputStream != null) bufferedOutputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

  

public class BufferedCopy_ {

    public static void main(String[] args) {

        // 1. BufferedReader 和 BufferedWriter 是安裝字元操作
        // 2. 不要去操作 二進位制檔案[聲音,視訊,doc, pdf ], 可能造成檔案損壞
        // BufferedInputStream
        // BufferedOutputStream
        String srcFilePath = "e:\\a.java";
        String destFilePath = "e:\\a2.java";

        BufferedReader br = null;
        BufferedWriter bw = null;
        String line;
        try {
            br = new BufferedReader(new FileReader(srcFilePath));
            bw = new BufferedWriter(new FileWriter(destFilePath));

            // 說明: readLine 讀取一行內容,但是沒有換行
            while ((line = br.readLine()) != null) {
                // 每讀取一行,就寫入
                bw.write(line);
                // 插入一個換行
                bw.newLine();
            }
            System.out.println("拷貝完畢...");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 關閉流
            try {
                if (br != null) {
                    br.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}