import java.io.*;/** * User: Saro * Date: 2005-2-19 * Time: 20:32:15 */ public class FileUtils { public void copyFile(String src,String des){ try{ File srcFile = new File(src); if(!srcFile.exists() || !srcFile.isFile()){ throw new RuntimeException("源文件: '" + src + "'"); } File desFile = new File(des); if(desFile.isDirectory()){ desFile=new File(desFile,srcFile.getName()); } BufferedInputStream in=new BufferedInputStream(new FileInputStream(srcFile)); BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(desFile)); byte[] buff=new byte[4084]; int length=-1; while((length=in.read(buff))!=-1){ out.write(buff,0,length); } in.close(); out.close(); }catch(Exception e){ throw new RuntimeException("无法复制文件 '" + src + "' 到 '" + des + "'" ,e); } } public static void main(String[] args) { new FileUtils().copyFile("H:\\[Hikari][Rozen_Maiden][02][jap_big5].rmvb","c:\\"); } }
import java.io.*; import java.awt.*; class FileCopy{ public static void main(String args[]){ try{ FileInputStream fin = new FileInputStream("d:/100x30_Logo.gif"); FileOutputStream fout = new FileOutputStream("d:/Logo.gif");
而Reader和Buffer是面向字符的传输
BufferedReader,BufferedWriter,char[] buff=new char[512]只要一个buffer就行:
还有,二进制文件,像avi就不能用Reader,Writer.
byte[] bytin=new byte[512];
InputStream is=new FileInputStream("input.file");
OutputStream os=new FileOutputStream("output.file");
while((size=fis.read(bytin))!=-1){
fos.write(bytin,0,size);
}
* User: Saro
* Date: 2005-2-19
* Time: 20:32:15
*/
public class FileUtils { public void copyFile(String src,String des){
try{
File srcFile = new File(src);
if(!srcFile.exists() || !srcFile.isFile()){
throw new RuntimeException("源文件: '" + src + "'");
}
File desFile = new File(des);
if(desFile.isDirectory()){
desFile=new File(desFile,srcFile.getName());
}
BufferedInputStream in=new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(desFile));
byte[] buff=new byte[4084];
int length=-1;
while((length=in.read(buff))!=-1){
out.write(buff,0,length);
}
in.close();
out.close();
}catch(Exception e){
throw new RuntimeException("无法复制文件 '" + src + "' 到 '" + des + "'" ,e);
}
} public static void main(String[] args) {
new FileUtils().copyFile("H:\\[Hikari][Rozen_Maiden][02][jap_big5].rmvb","c:\\");
}
}
import java.awt.*;
class FileCopy{
public static void main(String args[]){
try{
FileInputStream fin = new FileInputStream("d:/100x30_Logo.gif");
FileOutputStream fout = new FileOutputStream("d:/Logo.gif");
int c;
byte[] arr1 = new byte[fin.available()];
fin.read(arr1,0,fin.available());
for(c=0;c<arr1.length;c++){
fout.write(arr1[c]);
}
while((c=fin.read())!=-1){
fout.write(arr1[c]);
}
fin.close();
fout.close();
}catch(FileNotFoundException e){
System.out.println("File Copy1:" + e);
}catch(IOException e){
System.out.println("File Copy2:" + e);
}
}
}
public class Test
{
public static void main(String args[])
{
byte b []=new byte[1024];
int leagth;
try
{
FileInputStream in=new FileInputStream(args[0]);
FileOutputStream out=new FileOutputStream(args[1]);
leagth=in.read(b,0,1024);
while(leagth!=-1)
{
out.write(b,0,leagth);
leagth=in.read(b,0,1024);
}
}
catch(Exception e){}
}
}