Java 未写入文件的文本

Java 未写入文件的文本,java,file,Java,File,我似乎无法理解这一点。在下面的方法中,我尝试在两个位置向文件写入布尔值,但实际上没有写入任何内容。任何帮助都将不胜感激 private void renameTables(){ String path = MessengerMain.getInstance().getDataFolder() + File.separator + "v3-0-0 Table Rename.txt"; File f = new File(path); try(ResultSet rs = c

我似乎无法理解这一点。在下面的方法中,我尝试在两个位置向文件写入布尔值,但实际上没有写入任何内容。任何帮助都将不胜感激

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();
    }
}
private void renameTables(){
字符串路径=MessengerMain.getInstance().getDataFolder()+File.separator+“v3-0-0 Table Rename.txt”;
文件f=新文件(路径);
try(ResultSet rs=conn.getMetaData().getTables(null,null,“%”,null);Writer w=new PrintWriter(new FileOutputStream(f,false))){
如果(!f.exists()){
f、 createNewFile();
w、 填写(“虚假”);
w、 冲洗();
}
列表行=Files.readAllLines(path.get(path));
if(line.get(0.equalsIgnoreCase(“false”)){
System.out.println(“[Messenger]正在验证表名…”);
整数计数=0;
列表表=新的ArrayList();
表。添加(“消息”);
表。添加(“分数”);
表。添加(“联系人”);
while(rs.next()){
String table=rs.getString(“table_NAME”);
if(表。包含(表)){
更新(“ALTER TABLE”+表格+”重命名为“+”(“messenger_”+表格)+“;”);
计数++;
}
}
如果(计数>0){
System.out.println(“[Messenger]Done.”+count+“table”+(count==1?”:“s”)+“重命名”);
}否则{
System.out.println(“[Messenger]完成。无需重命名任何表。”);
}
w、 书写(“真实”);
w、 冲洗();
}
}catch(SQLException | IOException e){
e、 printStackTrace();
}
}
遵循Elliot Frisch的建议(相同的结果):

private void renameTables(){
字符串路径=MessengerMain.getInstance().getDataFolder()+File.separator+“v3-0-0 Table Rename.txt”;
文件f=新文件(路径);
try(ResultSet rs=conn.getMetaData().getTables(null,null,“%”,null)){
Writer w=新的PrintWriter(新的FileOutputStream(f,false));
如果(!f.exists()){
f、 createNewFile();
w、 填写(“虚假”);
w、 close();//在此处关闭
}
列表行=Files.readAllLines(path.get(path));
if(line.get(0.equalsIgnoreCase(“false”)){
System.out.println(“[Messenger]正在验证表名…”);
整数计数=0;
列表表=新的ArrayList();
表。添加(“消息”);
表。添加(“分数”);
表。添加(“联系人”);
while(rs.next()){
String table=rs.getString(“table_NAME”);
if(表。包含(表)){
更新(“ALTER TABLE”+表格+”重命名为“+”(“messenger_”+表格)+“;”);
计数++;
}
}
如果(计数>0){
System.out.println(“[Messenger]Done.”+count+“table”+(count==1?”:“s”)+“重命名”);
}否则{
System.out.println(“[Messenger]完成。无需重命名任何表。”);
}
w=新的PrintWriter(新的FileOutputStream(f,false));//创建一个新的编写器
w、 书写(“真实”);
w、 close();//在此处关闭
}
}catch(SQLException | IOException e){
e、 printStackTrace();
}
}
这是一份完整的工作清单


您是否尝试过使用调试器进行单步调试?你确定这些语句被访问了吗?考虑一下:你要求我们检查大量代码,通过检查对其进行调试,而不提供我们可以编译和运行的东西。另一方面,你可以运行你的程序并在调试器中单步执行,可能自己会发现问题。是的,文件正在创建,我在尝试读取文件时出错,因为其中没有任何内容。那么调试告诉你了什么?或者你只是忽略了每个人的建议?再一次,所有的东西都在运行,但是当我读到文件的时候,它抛出了一个IndexOutOfBoundsException(稍后我会修正),因为文件中没有任何东西,这不是我正在做的吗?减去我们无法验证的东西;我只能得出结论,在你的帖子中,没有达到这些声明。
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();
    }
}
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]