【问题标题】:How to get over Error when Reading from File into Java从文件读取到 Java 时如何克服错误
【发布时间】:2013-10-28 21:00:33
【问题描述】:

我的问题出在以下代码中。

问题是,当我调用alreadyUser(String username) 时,如果系统上不存在该文件,它会给出 FileNotFoundException。我想克服这个错误,但我想不通。

因此,在应用启动时,系统会要求输入 uname 并通过。然后调用 alreadyUser 方法,如果文件尚未创建,它会给出错误(例如,我手动创建它)。下次我启动程序时,如果文件已经存在,则不能使用新文件切换,因为旧数据将消失 :)

public final class TinyBase {

    final static String FILENAME = "KEYVALUES.txt";
    static FileOutputStream fos = null;
    static FileInputStream fis = null;

    protected static void createUser(String username, String password)

    protected static boolean loadUser(String username, String password)

    protected static boolean alreadyUser(String username) {
        String encode = new String(username);
        String compare = null;
        boolean flag = false; // true - ok no such user ; false - fail username
                                // already in use
        try {
            /* ERROR IS HERE */
            fis = new FileInputStream(FILENAME);
            /* ERROR IS HERE */
            byte[] buffer = new byte[fis.available()];

            while (fis.read(buffer) != -1) {
                compare = new String(buffer);
                if (compare.contains(encode)) {
                    flag = true;
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                return flag;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}

【问题讨论】:

  • 为什么不用Scanner扫描文件?

标签: java file io filenotfoundexception user-profile


【解决方案1】:

检查文件是否存在使用 -

File f = new File(filePathString);
if(f.exists()) { /* do something */ }

【讨论】:

  • 我已经尝试过这样做,但我认为有问题。因为我对它进行了编码并且没有任何反应,所以我的意思是它不会在路径上创建任何文件。
【解决方案2】:

我想,根据你的快照,你需要的是正确处理FileNotFoundCase

// ....
} catch (FileNotFoundException e) {
        Log.d("no settings for the user - assuming new user");
        flag = true;
}

顺便说一句,您需要修复 finally 块,以防之前出现异常,您的 fis 可能为空,因此为避免 NullPointerException 您可能需要额外检查:

if (fis != null) {
    fis.close();
}

更新

根据我对您的问题的理解,这是您可能需要的草图:

    // ... somewhere at startup after username/pass are given

    // check if file exists
    if (new File(FILENAME).isFile() == false) { // does not exist
        fos = new FileOutputStream(FILENAME); // will create 
                                              // file for you (not subfolders!)
        // write content to your file
        fos.close();
    }

    // by this point file exists for sure and filled with correct user data
    alreadyUser(userName);

【讨论】:

  • 感谢您在 fis 中的修复,但我尝试了:code fos = new FileOutputStream(FILENAME, true); if (fos != null) { fos.close();已经用户(用户名);并没有帮助我,我不明白为什么....
  • @user 如果你想只在FILENAME存在的情况下执行alreadyUser(userName),那么你需要使用ajc发布的代码。 (它不会为您创建文件,只有在文件存在时才会返回 true :if (f.exists()) { alreadyUser(userName); } 否则,请更新您的问题并说明您到底想做什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
  • 2013-02-17
  • 2016-09-15
  • 2013-02-17
相关资源
最近更新 更多