java把一個文件的內容復制到另外一個文件


/**
 * java把一個文件的內容復制到另外一個文件
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileInputOutputStreamTest {
 public static void main(String[] args) {
  File af = new File("a.txt");
  File bf = new File("b.txt");
  FileInputStream is = null;
  FileOutputStream os = null;
  if(!bf.exists()){
   try {
    bf.createNewFile();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  try {
   is = new FileInputStream(af);
   os = new FileOutputStream(bf);
   byte b[] = new byte[1024];
   int len;
   try {
    len = is.read(b);
    while (len != -1) {
     os.write(b, 0, len);
     len = is.read(b);
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }finally{
   try {
    if(is != null) is.close();
    if(os != null) os.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM