英文:
How to write file path into txt.file?
问题
public class FileScanner {
    public void scanFolder() throws IOException, InterruptedException {
        Scanner input = new Scanner(System.in);
        // Creating log file
        File log = new File("C:/Users/mateu/OneDrive/Pulpit/log.txt");
        if (!log.exists()) {
            log.createNewFile();
        }
        // Object for writing file names and paths to the log
        PrintWriter printIntoLog = new PrintWriter(log);
        while (true) {
            // Providing path of the file to search for
            System.out.println("Input file's directory you are looking for: ");
            String path = input.nextLine();
            // Creating object for the given file
            File searchedFile = new File(path);
            // Checking if the file exists - if yes, writing its path to the log and deleting it
            try {
                if (searchedFile.exists()) {
                    printIntoLog.println(searchedFile.getPath());
                    //searchedFile.delete();
                } else {
                    throw new MyFileNotFoundException("Searching stopped for 1 minute.");
                }
            } catch (MyFileNotFoundException ex) {
                System.out.println(ex);
                TimeUnit.MINUTES.sleep(1);
            }
        }
    }
}
英文:
I am writing simple program that is looking for file and if it exists, writes his path to txt file. If not, program is freezed for 1 minute. My problem is that file path is not being printed into txt file when searched file exists. I think it is something wrong with try block. I do not know where the problem is. How can i solve it?
public class FileScanner {
public void scanFolder() throws IOException, InterruptedException {
    Scanner input = new Scanner(System.in);
    //tworzenie pliku logu
    File log = new File("C:/Users/mateu/OneDrive/Pulpit/log.txt");
    if (!log.exists()) {
        log.createNewFile();
    }
    //obiekt zapisujacy nazwy i sciezki plikow do logu
    PrintWriter printIntoLog = new PrintWriter(log);
    while (true) {
        //podanie sciezki szukanego pliku
        System.out.println("Input file's directory you are looking for: ");
        String path = input.nextLine();
        //utworzenie obiektu do danego pliku
        File searchedFile = new File(path);
        //sprawdzenie czy plik istnieje- jesli tak to zapisuje do logu jego sciezke i usuwa go
        try{
            if (searchedFile.exists()) {
                printIntoLog.println(searchedFile.getPath());
                //searchedFile.delete();
            }else{
                throw new MyFileNotFoundException("Searching stopped for 1 minute.");
            }
        }catch(MyFileNotFoundException ex){
            System.out.println(ex);
            TimeUnit.MINUTES.sleep(1);
        }
    }
}
}
答案1
得分: 0
你应该在执行结束之前关闭 PrintWriter 引用。在对文件进行读写操作后关闭文件是一种强制性的实践。
如果你使用的是 Java +7,你可以使用 try-with-resources 语法来实现这一点:
try (PrintWriter printIntoLog = new PrintWriter(log)) {
   while (true) {
      ....
   }
}
对于旧版本的 Java,你可以使用 try-finally 语法:
try {
  while(true) {
     ...
  }
} finally {
   printIntoLog.close();
}
参考:
英文:
You should close the PrintWriter reference before finishing execution. It's an enforced practice to close a file after read/write on it.
If you are using Java +7 you can use  the fancy way withtry-with-resources syntax
```java
try (PrintWriter printIntoLog = new PrintWriter(log)) {
   while (true) { 
      ....
   }
}
```
With older versions you can use try-finally syntax
try {
  while(true) {
     ...
  }
} finally {
   printIntoLog.close();
}
See
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论