将一个文件保存在另一个路径下,并重命名

import java.io.*;
import java.util.Date;

public class BackTest {
static int BUFFER_SIZE = 1024;
public static void main(String[] args) {
int len=0;
String fileName = "E:\\a.txt";
String path = "E:\\a";
System.out.println("保存图像方法");
String latterpart= fileName.substring(fileName.lastIndexOf( "." ));
String imagePath=path+"\\"+new Date().getTime()+latterpart;
System.out.println("保存好的图像的路径为"+imagePath);

String imageName=imagePath.substring(imagePath.lastIndexOf( "\\" )+1);
File myFile = new File("E:\\a.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
FileInputStream fis=new FileInputStream(myFile);
byte[] buffer=new byte[BUFFER_SIZE];
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

写的挺乱的,先粘贴上,日后在修改一下。

原文地址:https://www.cnblogs.com/wrh526/p/2200626.html