当我从文本文件中删除一行时,将其添加到一个临时文本文件中,然后将该临时文件替换为原始文件,扫描仪未加载正确的文本。它是从原始文件而不是更新文件加载内容。我希望它使用新内容加载更新的文件。
try
{
File inputFile = new File("department.txt");
File tempFile = new File("departmentTemp.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = departResIDField.getText();
String currentLine;
while((currentLine = reader.readLine()) != null)
{
String trimmedLine = currentLine.trim();
if(trimmedLine.startsWith(lineToRemove)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
inputFile.delete();
tempFile.renameTo(inputFile);
fWriter = new FileWriter(inputFile.getAbsoluteFile(), true);
printOut = new PrintWriter(fWriter);
writer = new BufferedWriter(fWriter);
writer.write(departResIDField.getText() + "\t" +
departResNameField.getText() + "\r\n");
writer.close();
int result = Integer.parseInt(departResIDField.getText());
depart.add(new Department(result, departResNameField.getText()));
insResDepartModel.addElement(departResNameField.getText());
depart.remove();
//This is where I am having problem with the code
//The scanner is scanning old data not the updated data
Scanner scan = new Scanner(inputFile);
while(scan.hasNext())
{
depart.add(new Department(scan.nextInt(), scan.next()));
}
scan.close();
for(int i = 0; i < depart.getSize(); i++)
{
Department d1 = depart.get(i);
System.out.println(d1.departID + d1.departName);
}
menuLayout.show(cardLayout, "15");
departResIDField.setText(null);
departResNameField.setText(null);
departUpdateButton.setVisible(false);
departCancelButton.setVisible(false);
} catch (IOException f)
{
f.printStackTrace();
}
输出: 1生物学 2化学 4统计 5物理学 3数学
预期输出: 1生物学 2化学 4统计 5物理学 3微积分
代码删除了“ 3 Math”并添加了“ 3 Calculus
答案 0 :(得分:0)
当然有办法。但是,如果您只想保存主题,则建议您使用LinkedList
之类的格式保存在文本文件中,并尝试仅读写一个File
。例如,您可以使用ObjectOutputStream
保存复杂的数据结构。 LinkedList
Java实用程序有一条名为replace
的指令,用于修改某些索引。因此,如果要修改File
,请加载保存的LinkedList
,然后修改列表并将其保存在同一文件中。
答案 1 :(得分:0)
根据我的理解,您想将包含“ 3 Math”的行替换为“ 3 Calculus”。如果是这种情况,您可以执行以下操作:
Path wiki_path = Paths.get("data/sample.txt");
Charset charset = Charset.forName("UTF-8");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
List<String> lines = Files.readAllLines(wiki_path, charset);
int targetToReplaceIndex = -1;
// You can modify this part to match your logic
for (int i = 0; i < lines.size(); i++) {
if (lines.get(i).startsWith("3 Math")) {
targetToReplaceIndex = i;
break;
}
}
lines.set(targetToReplaceIndex, "3 Calculus");
File temp = File.createTempFile("temp", ".tmp");
temp.deleteOnExit();
BufferedWriter out = new BufferedWriter(new FileWriter(temp));
out.write(convertListToContent(lines));
out.close();
File orig = new File("data/sample.txt");
fis = new FileInputStream(temp);
fos = new FileOutputStream(orig);
FileChannel src = fis.getChannel();
FileChannel dest = fos.getChannel();
dest.transferFrom(src, 0, src.size());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
使用Java 8将字符串列表转换为单个String
的方法非常简单:
private static String convertListToContent(List<String> lines) {
StringBuilder contentBuilder = new StringBuilder();
lines.forEach(l -> contentBuilder.append(l).append("\n"));
return contentBuilder.toString();
}
希望有帮助。