文本未写入文件

时间:2016-07-07 23:28:57

标签: java file

我似乎无法弄清楚这一点。在下面的方法中,我试图在2个地方写一个文件的布尔值,但实际上没有写入任何内容。任何帮助将不胜感激。

private void renameTables(){
    String path = MessengerMain.getInstance().getDataFolder() + File.separator + "v3-0-0 Table Rename.txt";
    File f = new File(path);
    try(ResultSet rs = conn.getMetaData().getTables(null, null, "%", null); Writer w = new PrintWriter(new FileOutputStream(f, false))){
        if (!f.exists()){
            f.createNewFile();
            w.write("false");
            w.flush();
        }

        List<String> lines = Files.readAllLines(Paths.get(path));
        if (lines.get(0).equalsIgnoreCase("false")){
            System.out.println("[Messenger] Verifying table names...");
            int count = 0;
            List<String> tables = new ArrayList<String>();
            tables.add("messages");
            tables.add("scores");
            tables.add("contacts");
            while (rs.next()){
                String table = rs.getString("TABLE_NAME");
                if (tables.contains(table)){
                    update("ALTER TABLE " + table + " RENAME TO " +  ("messenger_" + table) + ";");
                    count++;
                }
            }
            if (count > 0){
                System.out.println("[Messenger] Done. " + count + " table" + (count == 1 ? "" : "s") + " renamed.");
            }else{
                System.out.println("[Messenger] Done. No tables need to be renamed.");
            }
            w.write("true");
            w.flush();
        }
    } catch (SQLException | IOException e){
        e.printStackTrace();
    }
}

遵循Elliot Frisch的建议(结果相同):

private void renameTables(){
    String path = MessengerMain.getInstance().getDataFolder() + File.separator + "v3-0-0 Table Rename.txt";
    File f = new File(path);
    try(ResultSet rs = conn.getMetaData().getTables(null, null, "%", null)){
        Writer w = new PrintWriter(new FileOutputStream(f, false));
        if (!f.exists()){
            f.createNewFile();
            w.write("false");
            w.close(); //close here
        }

        List<String> lines = Files.readAllLines(Paths.get(path));
        if (lines.get(0).equalsIgnoreCase("false")){
            System.out.println("[Messenger] Verifying table names...");
            int count = 0;
            List<String> tables = new ArrayList<String>();
            tables.add("messages");
            tables.add("scores");
            tables.add("contacts");
            while (rs.next()){
                String table = rs.getString("TABLE_NAME");
                if (tables.contains(table)){
                    update("ALTER TABLE " + table + " RENAME TO " +  ("messenger_" + table) + ";");
                    count++;
                }
            }
            if (count > 0){
                System.out.println("[Messenger] Done. " + count + " table" + (count == 1 ? "" : "s") + " renamed.");
            }else{
                System.out.println("[Messenger] Done. No tables need to be renamed.");
            }
            w = new PrintWriter(new FileOutputStream(f, false)); //create a new writer
            w.write("true");
            w.close(); //close here
        }
    } catch (SQLException | IOException e){
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:1)

这是一个有效的minimal, complete, verifiable example

public static void main(String[] args) {
    File f = new File(System.getProperty("user.home"), "temp.txt");
    String path = f.getPath();
    try (Writer w = new FileWriter(f)) {
        w.write("false");
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        List<String> lines = Files.readAllLines(Paths.get(path));
        System.out.println(lines);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

输出(正如预期的那样)

[false]
相关问题