【问题标题】:How do I transfer information from a folder to file?如何将信息从文件夹传输到文件?
【发布时间】:2015-08-15 22:43:04
【问题描述】:

所以我正在研究这个有助于为我建立一个网站的项目,我希望从一个文件夹(包含文件)中读取并写入一个 html 文档。我做了一些研究,但我总是没有打印任何内容或出现错误。让我知道是否有办法做到这一点?这是我的项目,我已将文件夹位置和输出更改为 .txt

public class Project {

@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {

    PrintWriter out = new PrintWriter("output.txt");

    Files.walk(Paths.get("C:/Location")).forEach(filePath -> {
        if (Files.isRegularFile(filePath)) {

            out.print(filePath);

            }    
        }); 
    }
}

【问题讨论】:

  • 您是否也尝试过打印到控制台?有什么出现吗?
  • 是的,当我打印控制台时,一切都会显示出来。
  • 你想用 File.isRegularFile(Path p) 完成什么。您在寻找不是目录的文件吗?
  • "output.txt" 可能是在您的硬盘驱动器的根目录中创建的,位于 C:/,您可能没有写入权限。
  • 您可能想要使用println 而不是print,并且您需要关闭编写器(out.close())。

标签: java file output directory


【解决方案1】:

@JoetheDailyProgrammer,你可能写错了文件。试试这个代码:

public static void main(String[] args) throws IOException {

    File out = new File("output.txt");

    System.out.println(out.getAbsolutePath());
}

运行它,它将显示您的"output.txt" 的绝对路径。如果它指向的位置与您预期的不同,则在您的应用程序中使用绝对路径或使用用户 homedir,如下所示:

public static void main(String[] args) throws IOException {

     File outFile = new File(System.getProperty("user.home") + "/Desktop/output.txt"); // file into users homedir

     System.out.println(outFile.getAbsolutePath()); // print it's location in console

     PrintWriter out = new PrintWriter(outFile); // writer over that file

     Files.walk(Paths.get("C:/Location")).forEach(filePath -> {
         if (Files.isRegularFile(filePath)) {

             out.print(filePath);

             }    
         }); 
     out.close();
    }
}

更新。

如果真的您打算将文件保存到桌面文件夹中,那么您可能应该像以下答案一样导航它:

https://stackoverflow.com/a/1080900/2109067

https://stackoverflow.com/a/570536/2109067

所以:

public static void main(String[] args) throws IOException {
    FileSystemView fsv = FileSystemView.getFileSystemView();
    File outFile = new File(fsv.getHomeDirectory() + "/output.txt"); // file into desktop dir

    System.out.println(outFile.getAbsolutePath()); // print it's location in console
    ...
}

【讨论】:

    【解决方案2】:

    确保在程序结束时使用 PrintWriter("output.txt", true) 自动刷新或刷新它。

    public static void main(String[] args) {
      PrintWriter out = new PrintWriter(System.out, true);
      File dir = new File("images");
      String[] list = dir.list();
      if (list != null) {
        for (String f : list) {
          String[] fileName = f.split("\\.");
          if (fileName.length > 1 && fileName[1].equals("png")) {
            // System.out.println(f);
            out.println(f);
          }
    
        }
      } else {
        System.out.println("list returned null because dir.canRead is " + dir.canRead());
      }
    }
    

    我在 File 类中重写了一些内容。让我知道这些需求是否发生了变化。希望这会有所帮助。

    【讨论】:

    • 我在这里遇到了这个问题 Project.main(Project.java:14) 的线程“main”java.lang.NullPointerException 中的异常
    • 尝试运行 Files.canRead(Your file) 看看是否可以访问。
    • @JoetheDailyProgrammer 立即尝试。
    • 它说我可以。会不会是 Eclipse 的问题?
    • @JoetheDailyProgrammer 尝试在 Eclipse 中创建一个新文件夹(Ctrl N -> 选择文件夹)。随意命名。然后将“output.txt”更改为“whateveryounamedyourfolder/output.txt”。此外,我建议您将该文件复制到桌面,更改路径,然后在那里尝试。
    猜你喜欢
    • 1970-01-01
    • 2020-09-16
    • 2018-05-30
    • 2018-06-14
    • 1970-01-01
    • 2017-01-05
    • 2015-01-29
    • 2016-10-13
    • 1970-01-01
    相关资源
    最近更新 更多