java简单的文件复制

来源:互联网 发布:php 返利网站源码 编辑:程序博客网 时间:2024/05/31 19:24
package test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class CopyFile {public static void main(String[] args) {String path="f://test/";File pathFile=new File(path);if(!pathFile.exists()&&!pathFile.isDirectory()){pathFile.mkdirs();}       File inFile=new File("f://test/test.txt");       File outFile=new File("f://test/copyTest.txt");              try {            if(!inFile.exists()){inFile.createNewFile();      }      if(!outFile.exists()){      outFile.createNewFile();           }            InputStream is=new FileInputStream(inFile);  OutputStream os=new FileOutputStream(outFile);    byte[] b=new byte[1024];  int len=0;    while((len=is.read(b))!=-1){  os.write(b, 0, len);  }  } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

0 0
原创粉丝点击