Java 文件复制 - 如何将输入文件Backup/copy 复制到其他目标位置?
Java File Copy -How to Backup/copy the input file to other destination location?
想要copy/backup文件到目标文件夹,然后再执行
任何任务。
(jdk-1.7)
/*Input file path taken from properties file as string is :inputFile
where-in inputFile is :C:\Project\input\filename.txt
Destination file path taken from properties file as string is :
archiveFolderPath */
//Existing code : in main
if (inputFile != null) {
readTextFile(new File(inputFile)); }
// in readTextFile method
BufferedReader br = new BufferedReader(new FileReader(filename));
我尝试使用以下程序::但出现错误:
错误::
Files 类型中的方法 copy(InputStream, OutputStream) 不是
适用于参数 (String, String)
//Calling method in main::
copyFiles(inputFile, archiveFolderPath);
//Copy method :
private static void copyFiles (String inputFile, String
archiveFolderPath) throws IOException {
Files.copy(inputFile, archiveFolderPath); }
请提出替代解决方案,因为“文件不适用于
参数(字符串,字符串)" .
您可以先复制文件,然后再对其执行读取或写入操作。示例:-
Path origin = Paths.get("/home/fm/source.txt");
Path destination = Paths.get("/home/fm/source.bak");
//Copy source.txt to source.bak
Files.copy(origin, destination, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
有关所有 copy
方法的详细信息,请参阅 Files
javadoc。他们中的一些人期待 CopyOption
作为参数。根据节目要求选择合适的CopyOption
。
https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html
https://docs.oracle.com/javase/7/docs/api/java/nio/file/StandardCopyOption.html#COPY_ATTRIBUTES
想要copy/backup文件到目标文件夹,然后再执行 任何任务。 (jdk-1.7)
/*Input file path taken from properties file as string is :inputFile
where-in inputFile is :C:\Project\input\filename.txt
Destination file path taken from properties file as string is :
archiveFolderPath */
//Existing code : in main
if (inputFile != null) {
readTextFile(new File(inputFile)); }
// in readTextFile method
BufferedReader br = new BufferedReader(new FileReader(filename));
我尝试使用以下程序::但出现错误: 错误:: Files 类型中的方法 copy(InputStream, OutputStream) 不是 适用于参数 (String, String)
//Calling method in main::
copyFiles(inputFile, archiveFolderPath);
//Copy method :
private static void copyFiles (String inputFile, String
archiveFolderPath) throws IOException {
Files.copy(inputFile, archiveFolderPath); }
请提出替代解决方案,因为“文件不适用于 参数(字符串,字符串)" .
您可以先复制文件,然后再对其执行读取或写入操作。示例:-
Path origin = Paths.get("/home/fm/source.txt");
Path destination = Paths.get("/home/fm/source.bak");
//Copy source.txt to source.bak
Files.copy(origin, destination, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
有关所有 copy
方法的详细信息,请参阅 Files
javadoc。他们中的一些人期待 CopyOption
作为参数。根据节目要求选择合适的CopyOption
。
https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html https://docs.oracle.com/javase/7/docs/api/java/nio/file/StandardCopyOption.html#COPY_ATTRIBUTES