gpt4 book ai didi

java - FileWriter 未正确写入目录

转载 作者:行者123 更新时间:2023-11-30 06:54:02 26 4
gpt4 key购买 nike

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

示例命令:

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 文件始终存在。使用我的插入命令,如果我将 File = new File 行更改为:

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

然后,它毫不奇怪地使用我的示例命令创建了一个名为“student”的 .txt 文件,但不在“DBName”文件夹中。如果我把它改成这样:

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

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

我尝试在我的主方法中声明 PrintWriterFile 并将它们传入,但这也不起作用。

如何让它写入college目录下的students.txt?

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

最佳答案

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

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;
}

}
}
<小时/>

希望这有帮助!

关于java - FileWriter 未正确写入目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42172869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com