J2SE--用JAVA流如何将一个文件拷贝到别的地方-->清高手指教
请教高手如何用JAVA流将一个文件拷贝到别的地方
我在JDK中没有看到关于copy的方法...
------解决方案--------------------拷贝,自己实现吧
就是读出再写入
------解决方案--------------------是叫copy啊,从输入流读入在输出到指定的文件就是copy啊
------解决方案--------------------public static void main(String[] args) {
File oldFile = new File( "c:/oldFile.txt ");
File newFile = new File( "c:/newFile.txt ");
try {
FileInputStream inPutStream = new FileInputStream(oldFile);
FileOutputStream outPutStream = new FileOutputStream(newFile);
byte[] byteArr = new byte[512];
while (inPutStream.read(byteArr) > 0) {
outPutStream.write(byteArr);
outPutStream.flush();
}
outPutStream.close();
inPutStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
------解决方案--------------------用通道不就得了?好多人问....
package test;
import java.io.File;
import java.io.FileInputStream;
import
java.io.FileNotFoundException;
import java.io.FileOutputStream;
import
java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
public class Copyfile {
public static void main (String args[])
{
File f= new File ( "D://eMule-0.47c-VeryCD1215-Setup.exe ");
try {
FileInputStream fin = new FileInputStream (f);
FileChannel fc = fin.getChannel();
FileOutputStream fout = new FileOutputStream( "D://1.exe ");
WritableByteChannel to = fout.getChannel();
fc.transferTo(0, fc.size(),to );
} catch (
FileNotFoundException e) {
System.err.println( "File not found! ");
} catch (
IOException e) {
System.err.println( "there are errors on processing ");
}
}
}
------解决方案--------------------刚刚写了个程序:
/**
* @param args
* @throws IOException
* @throws IOException
* function: 实现文件的COPY功能,把read.txt的内容拷贝到text.txt文件
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File read = new File( "D://read.txt ");
System.out.println(read.length());
FileInputStream readstream = new FileInputStream(read);
byte[] b = new byte[(int)read.length()];
readstream.read(b);
File s = new File( "D://text.txt ");
FileOutputStream writestream = new FileOutputStream(s);
writestream.write(b);
readstream.close();
writestream.close();
writestream.flush();
}
------解决方案--------------------mark
------解决方案--------------------大部分人平时用io都喜欢用FileOutputStream和FileInputStream吗