FileWriter无法正确写入目录

时间:2017-02-11 06:06:52

标签: java string io filewriter printwriter

我有一个简单的程序,可以读入命令并执行它们。 Rightnow我有这个代码用于将某些文本插入文本文件:

示例命令:

INSERT "John Smith" INTO college.student

我的主要方法:

 else if(command.substring(0,6).equalsIgnoreCase("INSERT")){
        String string = command.substring(7, command.indexOf("INTO") - 1);
        String DBNameTBName = command.substring(command.indexOf("INTO") + 5);
        String tableName = DBNameTBName.substring(DBNameTBName.indexOf(".") + 1);
        String DBName = DBNameTBName.substring(0, DBNameTBName.indexOf("."));

        if(DBCommands.insert(string, DBName, tableName)){
            statfileWriter.println("Inserted " + string  + " into table " + tableName + " in " + DBName);
            statfileWriter.println("(" + command + ")");
            statfileWriter.flush();
        }
        else{
            errfileWriter.println("Error: Could not insert " + string + " into table " + tableName + " in " + DBName);
            errfileWriter.println("(" + command + ")");
            errfileWriter.flush();
        }       

它调用的插入方法:

public static boolean insert(String string, String DBName, String tableName){
    try{
        string = string.substring(string.indexOf('"') + 1, string.lastIndexOf('"')); //removes quotes

        File tableToWriteTo = new File(DBName  + "/" + tableName + ".txt");
        if (!tableToWriteTo.exists()){
            return false;
        }

        PrintWriter writer = new PrintWriter(new FileWriter
                  (tableToWriteTo, true));
        writer.println(string);
        writer.close();
        return true;
    }
    catch(Exception e){

        return false;
    }

}

我的插入方法让我变得非常奇怪。它返回true,因为它始终打印到我的状态日志而不是错误日志。我知道创建.txt文件的方法工作正常,我已经多次测试过,而且student.txt文件总是在那里。使用我的insert命令,如果我将File = new File行更改为:

File tableToWriteTo = new File(tableName + ".txt");

然后毫不奇怪地创建了一个名为" student"使用我的示例命令,但不在" DBName"夹。如果我改成它:

File tableToWriteTo = new File(DBName  + "/" + tableName);

然后它会创建一个名为" student"没有类型(例如,Windows询问我想要打开它)但是放入我想要插入的字符串中。我应该注意,如果有多个INSERT命令,那么它会按照我的意愿写入所有字符串。

我尝试在我的主要方法中声明PrintWriterFile并将其传入,但这也无效。

如何将它写入学院目录中的students.txt?

编辑:哦,天哪,我是地球上最愚蠢的人。我没有看到我为这个任务收到的​​完整命令列表,我忘了有一个删除命令,他们两个都在工作。我会删除这个问题,但我会留下这个问题,以防将来有人想看到FileWriter的例子。

1 个答案:

答案 0 :(得分:1)

我更改了if方法中的insert条件。该文件预计不存在。理想情况下,不应该否定这种情况。我使用了以下代码,它对我有用。

public class InsertToWriteTo {

    public static void main(String[] args) {
        boolean ret = insert("\"hello\"", "college", "student");
        System.out.println(ret);
    }

    public static boolean insert(String string, String DBName, String tableName) {
        try {
            string = string.substring(string.indexOf('"') + 1, string.lastIndexOf('"')); // removes quotes

            File tableToWriteTo = new File(DBName + "/" + tableName + ".txt");
            if (tableToWriteTo.exists()) { // changed condition
                System.out.println("File exists");
                return false;
            }

            PrintWriter writer = new PrintWriter(new FileWriter(tableToWriteTo, true));
            writer.println(string);
            writer.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}

希望这有帮助!