我正在尝试编写一个程序,该程序将使用诸如插入、删除和排序等命令的文本文件,并让它们从链接列表中插入或删除节点。到目前为止,我已经能够标记字符串并将它们写出来;但我也想使用 if 语句,无论文本行是说插入还是说删除。这就是我到目前为止所拥有的。感谢您的任何帮助。
(车道.txt)
insert 1
insert 7
insert 5
delete 7
insert 2
insert 4
delete 5
和代码:
导入 java.io。; 导入 java.util。;
类令牌测试 {
public static void main (String[] args) {
TokenTest tt = new TokenTest();
tt.dbTest();
}
void dbTest() {
DataInputStream dis = null;
String dbRecord = null;
try {
File f = new File("lane.txt");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// read the first record of the database
while ( (dbRecord = dis.readLine()) != null) {
StringTokenizer st = new StringTokenizer(dbRecord, " ");
String action = st.nextToken();
String key = st.nextToken();
System.out.println("Action: " + action);
if(action == "insert")
{
System.out.println("holla");
}
System.out.println("Key Value: " + key);
if(action == "delete")
{
System.out.println("holla");
}
System.out.println(" ");
}
} catch (IOException e) {
// catch io errors from FileInputStream or readLine()
System.out.println("Uh oh, got an IOException error: " + e.getMessage());
} finally {
// if the file opened okay, make sure we close it
if (dis != null) {
try {
dis.close();
} catch (IOException ioe) {
System.out.println("IOException error trying to close the file: ");
}
} // end if
} // end finally
} // end dbTest
} // 结束类
输出:
行动:插入键值:1
行动:插入键值:7
行动:插入键值:5
行动:删除键值:7
行动:插入键值:2
行动:插入键值:4
行动:删除键值:5