String uuidDoc = UUID.randomUUID().toString();
Calendar date = Calendar.getInstance();
int year = date.get(Calendar.YEAR);
int month = date.get(Calendar.MONTH) + 1;
int day = date.get(Calendar.DAY_OF_MONTH);
String afterPath = year + "\\" + month + "\\" + day + "\\" + uuidDoc + ".doc";
String newPath = impl.getDirPaths() + afterPath; //新文件
//拷贝文件
int length=2097152;
File f1 = new File(oldPath);
File f2 = new File(newPath);
int bytesum = 0;
int byteread = 0;
if(f2.createNewFile()){
FileInputStream inStream = new FileInputStream(f1);
FileOutputStream outStream = new FileOutputStream(f2);
byte[] buffer=new byte[length];
while((byteread = inStream.read(buffer)) != -1){
bytesum += byteread; //字节数 文件大小
outStream.write(buffer, 0, byteread);
}
inStream.close();
}
本文介绍了一段Java代码实现文件从旧路径到新路径的拷贝过程,包括使用UUID生成唯一文件名及按年月日组织文件路径的方法。
4547

被折叠的 条评论
为什么被折叠?



