java讀取文件內容並輸出到控制檯,java中實現文件複製

public class TestFileInputStream {
  public static void main(String [] args) {
    //讀取指定文件中內容,並在控制檯輸出
    FileInputStream fis = null;
    byte[] b = new byte[1024];
    int len = 0;

    try {
      fis = new FileInputStream("E:\\javafile\\ja.txt");
      while((len = fis.read(b)) != -1) {
        System.out.write(b, 0, len);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      try {
        fis.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}java

 

public class TestFileInputStream{
  public static void main(String [] args) {
    //實現文件複製
    FileInputStream fis = null;
    FileOutputStream fos = null;

    byte[] b = new byte[1024];
    int len = 0;

    try {
      fis = new FileInputStream("E:\\javafile\\ja.txt");
      fos = new FileOutputStream("E:\\javafile\\jc.txt");

      while((len = fis.read(b)) != -1) {
        fos.write(b, 0, len);
      }

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }catch(IOException io) {
      io.printStackTrace();
    }finally {
      try {
        fis.close();
        fos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}spa

相關文章
相關標籤/搜索