猿问

Java 文件复制 - 如何备份/复制输入文件到其他目标位置?

在执行任何任务之前想要将文件复制/备份到目标文件夹。(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));

我尝试使用以下过程::但出现错误:错误::文件类型中的方法复制(InputStream,OutputStream)不适用于参数(字符串,字符串)


//Calling method in main::

copyFiles(inputFile, archiveFolderPath);



//Copy method :

private static void copyFiles (String inputFile, String 

  archiveFolderPath) throws IOException {

    Files.copy(inputFile, archiveFolderPath); }

请建议替代解决方案,如“文件不适用于参数(字符串,字符串)”。


皈依舞
浏览 104回答 1
1回答

蝴蝶不菲

您可以在对文件执行读取或写入操作之前复制该文件。例子:-Path origin = Paths.get("/home/fm/source.txt");Path destination = Paths.get("/home/fm/source.bak");//Copy source.txt to source.bakFiles.copy(origin, destination, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);Files有关所有方法的详细信息,请参阅javadoc copy。他们中的一些人期待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
随时随地看视频慕课网APP

相关分类

Java
我要回答