1 package 文件操作; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 10 public class TestCopy { 11 public static void main(String[] args) throws IOException { 12 copy("D:\\com\\test.txt","C:\\com\\dest.txt"); 13 } 14 public static void copy(String source,String dest) throws IOException{ 15 File fileS=new File(source); 16 File fileD=new File(dest); 17 if(fileS.exists()){ 18 InputStream in=new FileInputStream(fileS); 19 int len=(int)fileS.length(); 20 byte[] b=new byte[len]; 21 in.read(b);//一次性读入 22 in.close(); 23 24 if(!(fileD.exists())){ 25 fileD.getParentFile().mkdirs(); 26 fileD.createNewFile(); 27 } 28 OutputStream out=new FileOutputStream(fileD); 29 out.write(b);//一次性写出 30 out.close(); 31 }else{ 32 System.out.println(source+"文件不存在!"); 33 } 34 } 35 }
一次性读入和写出
时间: 2024-10-16 09:42:56