用Java怎么将文档复制到另一个路径下面并改名
2个回答
展开全部
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class A {
public static void main(String[] args) throws Exception {
String url1 = "D:\\a.txt";// 源文件路径
String url2 = "E:\\b.txt";// 目标路径(复制到E盘,重命名为b.txt)
copy(url1, url2);
}
private static void copy(String url1, String url2) throws Exception {
FileInputStream in = new FileInputStream(new File(url1));
FileOutputStream out = new FileOutputStream(new File(url2));
byte[] buff = new byte[512];
int n = 0;
System.out.println("复制文件:" + "\n" + "源路径:" + url1 + "\n" + "目标路径:"
+ url2);
while ((n = in.read(buff)) != -1) {
out.write(buff, 0, n);
}
out.flush();
in.close();
out.close();
System.out.println("复制完成");
}
}
写一个例子
希望对你有帮助
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class A {
public static void main(String[] args) throws Exception {
String url1 = "D:\\a.txt";// 源文件路径
String url2 = "E:\\b.txt";// 目标路径(复制到E盘,重命名为b.txt)
copy(url1, url2);
}
private static void copy(String url1, String url2) throws Exception {
FileInputStream in = new FileInputStream(new File(url1));
FileOutputStream out = new FileOutputStream(new File(url2));
byte[] buff = new byte[512];
int n = 0;
System.out.println("复制文件:" + "\n" + "源路径:" + url1 + "\n" + "目标路径:"
+ url2);
while ((n = in.read(buff)) != -1) {
out.write(buff, 0, n);
}
out.flush();
in.close();
out.close();
System.out.println("复制完成");
}
}
写一个例子
希望对你有帮助
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询