【问题标题】:arrayList<File> to folder containing those files javaarrayList<File> 到包含这些文件的文件夹 java
【发布时间】:2017-03-19 13:30:51
【问题描述】:

我有一个ArrayList&lt;File&gt;

我需要将这些文件写入我的计算机中的 output_Directory

这是我的代码

for (File file : corpus)


{ 
        try {
            FileWriter fw = new FileWriter (new File(outDirName, file.getName()));
            BufferedWriter bw = new BufferedWriter (fw);
            PrintWriter out = new PrintWriter (bw);
            out.close();
        }
        catch (Exception e){
            System.out.println(e.toString());
        }
    }

语料库是文件的数组列表

错误是

java.io.FileNotFoundException: Diroutput/name of my file 

(没有这种类型的文件或目录)

【问题讨论】:

  • 您从异常的名称、消息和 javadoc 中推断出什么?

标签: java file arraylist


【解决方案1】:

很可能您的输出目录不存在。所以先创建输出目录,错误就消失了。此外,您可以使用 java.nio.file 中的 Files.copy(),这将避免创建多个 FileWriter 实例

try {
    Files.copy(
        Paths.get(file.getAbsolutePath()),
        Paths.get(new File("<correct Path>", file.getName()).getAbsolutePath()));
} catch (IOException e) {
    e.printStackTrace();
} 

【讨论】:

    【解决方案2】:

    此代码有效 文件目录=新文件(outDirName); 如果(!dir.exists()){ dir.mkdir();}

                for (File file : corpus)
                { 
              File new_file= new File(dir,file.getName());  
                    {
                    if(!new_file.exists()){
                        new_file.createNewFile();
                        }
                }
    

    【讨论】:

      猜你喜欢
      • 2013-02-03
      • 1970-01-01
      • 2012-03-10
      • 2016-02-23
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多