使用此代码在java中复制中文文件。但是目标文件包含问号(?)而不是中文字符。java中有什么方法可以实现这一功能吗。。
File source = new File("H:\work-temp\file");
File dest = new File("H:\work-temp\file2");
try {
FileUtils.copyDirectory(source, dest);
} catch (IOException e) {
e.printStackTrace();
}
首先,在复制文件时,目标应该是文件夹而不是文件。。。所以请将File dest = new File("H:\work-temp\file2");
更改为File dest = new File("H:\work-temp");
接下来您应该使用FileUtils.copyFile(source, dest);
而不是FileUtils.copyDirectory(source, dest);
使用JDK 7 Files.copy
方法之一。它们会创建文件的二进制副本。
您错过了例如:file.txt
和file2.txt
:的文件扩展名
File source = new File("H:\work-temp\file.txt");
File dest = new File("H:\work-temp\file2.txt");
为了应对,使用这个:
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try{
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
有关更多信息,请访问此链接