文件复制(java)

来源:互联网 发布:腾讯搜索软件是什么 编辑:程序博客网 时间:2024/05/17 02:27
 

import java.io.*;
public class NewClass {
    //将c盘下的文件复制到d盘下,字节的形式读取。
public void file()
 {
  File file1 = null;
  File file2 = null;
  FileInputStream fis = null;
  FileOutputStream fos = null;
  try
  {
   file1 = new File("C:/zhu.txt");
   file2 = new File("D:/zhu.txt");
   fis = new FileInputStream(file1);
   fos = new FileOutputStream(file2);
   byte[] date = new byte[(int)file1.length()];
   int i = 0;
   while ((i = fis.read(date)) != -1)
   {
    String str = new String(date);
    System.out.println(str);
    fos.write(date);
   }
  }
  catch (IOException ie)
  {
   ie.printStackTrace();
  }finally
  {
   try
   {
    fos.close();
    fis.close();
   }
   catch (IOException ie)
   {
    ie.printStackTrace();
   }

  }
 }
 public static void main(String[] args)
 {
  NewClass fo = new NewClass();
  fo.file();
 }
}

原创粉丝点击