本文介绍了Java文件IO异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的程序,用于保存收集数据的文本日志。为了进行设置,我在程序开始时运行以下代码(设置日志文件,以及使用它的工具):

I am attempting to create a simple program that will keep a text log of collected data. To set this up, I have the following code run at the start of the program (to set up the log file, and the tools to use it):

File logFile = new File("logs/logFile.txt");
FileWriter fw;
FileReader fr;
BufferedWriter writer;
BufferedReader reader;

public void someMethod(){
    System.out.println(logFile.getAbsolutePath());
    try{
        logFile.createNewFile();
    }catch(Exception e){
        System.err.println("WARNING: CANNOT CREATE FILE");
    }
    try{
        fw = new FileWriter("plugins/Stalker/log.txt");
        fr = new FileReader("plugins/Stalker/log.txt");
        writer = new BufferedWriter(fw);
        reader = new BufferedReader(fr);
    } catch(Exception e){
        System.err.println("ERROR: CANNOT READ OR WRITE TO LOG FILE");
    }
}

当我运行它时,我遇到了两个异常。它不会在第一个println中给出的路径上创建文件或文件夹(日志)。路径就像我期望的那样,我应该对该目录具有写入权限(我知道其他程序经常将日志写入父目录这一事实)...我已经使用了一些文件以前,但它已经有点了,我在这里完全失去了。

When I run this, I hit both exceptions. It does not create either the file, or the folder (logs) at the path given in the first println. The path is as I expect it to be, and I SHOULD have write permissions for that directory (I know for a fact that other programs regularly write logs and such to a parent directory)... I've worked with files a little bit before, but it has been a bit, and I am at a complete loss here.

我可能遇到什么样的问题?您建议尝试修复此问题吗?

What sort of problem(s) might I be running into? What attempts at fixing this would you suggest?

推荐答案

使用文件夹时,必须确保该文件夹存在。

When you are working with folders, you have to make sure that the folder exists.

为此你必须在 logFile.createNewFile(); 之前写一个条件来检查文件夹是否存在因为createNewFild不会创建文件夹。

For that you have to write a condition before logFile.createNewFile(); to check whether the folder exists because createNewFild will not create folders.

你必须像这样修改程序。

You have to modify the program little bit like this.

File logFileFolder = new File("logs");
File stalkerFolder = new File("plugins/Stalker");
File logFile = new File("logs/logFile.txt");
FileWriter fw;
FileReader fr;
BufferedWriter writer;
BufferedReader reader;

public void someMethod(){
    System.out.println(logFile.getAbsolutePath());
    try{
        if (!logFileFolder.exists()){
             // Create folder if does not exist
             logFileFolder.mkdir();
        }
        logFile.createNewFile();
    }catch(Exception e){
        System.err.println("WARNING: CANNOT CREATE FILE");
    }
    try{
        if (!stalkerFolder.exists()){
             // Create folders if does not exist
             stalkerFolder.mkdirs();
        }
        fw = new FileWriter("plugins/Stalker/log.txt");
        fr = new FileReader("plugins/Stalker/log.txt");
        writer = new BufferedWriter(fw);
        reader = new BufferedReader(fr);
    } catch(Exception e){
        System.err.println("ERROR: CANNOT READ OR WRITE TO LOG FILE");
    }
}

这篇关于Java文件IO异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:21