Java 如果确定文本不存在,如何将新文本添加到文本文件中';不存在
我一直在尝试创建一个类,在文本文件中“更新”、“删除”和“插入新内容”。“更新”和“删除”部件正常工作。“插入新内容”似乎没有任何作用。请任何人就我的代码中可能出现的错误或缺失向我提供建议 我没有收到任何错误报告,也没有代码错误突出显示 如果文本文件中尚未写入新内容,我将尝试将其写入文本文件。这可能类似于,如果用户是新用户,则将其详细信息输入文本文件以进行存储;如果已注册,则不会输入新数据,但他们可以编辑或删除以前存储的详细信息 如果有人能尽可能地帮助我,我会非常感激 先谢谢你Java 如果确定文本不存在,如何将新文本添加到文本文件中';不存在,java,io,Java,Io,我一直在尝试创建一个类,在文本文件中“更新”、“删除”和“插入新内容”。“更新”和“删除”部件正常工作。“插入新内容”似乎没有任何作用。请任何人就我的代码中可能出现的错误或缺失向我提供建议 我没有收到任何错误报告,也没有代码错误突出显示 如果文本文件中尚未写入新内容,我将尝试将其写入文本文件。这可能类似于,如果用户是新用户,则将其详细信息输入文本文件以进行存储;如果已注册,则不会输入新数据,但他们可以编辑或删除以前存储的详细信息 如果有人能尽可能地帮助我,我会非常感激 先谢谢你 import j
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class EditDelete {
static PrintWriter pw;
static String line = null;
public static void newEntry() {
pw.println("A NEW ENTRY");
pw.flush();
}
public static void removeLineFromFile(String file, String lineToRemove) {
try {
File inFile = new File("/D:/TestFile.txt/");
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
//Construct the new file that will later be renamed to the original filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
pw = new PrintWriter(new FileWriter(tempFile));
//Read from the original file and write to the new
//unless content matches data to be removed.
while ((line = br.readLine()) != null) {
if (!line.startsWith(lineToRemove)) {
pw.println(line);
pw.flush();
} else if (line.startsWith(lineToRemove)) {
pw.println("THIS GOT CHANGED" + "\n" + "\n");
pw.flush();
} else if (!line.startsWith(lineToRemove) && !line.startsWith(lineToRemove)) {
newEntry();
}
}
pw.close();
br.close();
//Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile))
System.out.println("Could not rename file");
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
这段代码没有意义:
if (!line.startsWith(lineToRemove)) {
pw.println(line);
pw.flush();
} else if (line.startsWith(lineToRemove)) {
pw.println("THIS GOT CHANGED" + "\n" + "\n");
pw.flush();
} else if (!line.startsWith(lineToRemove) && !line.startsWith(lineToRemove)) {
newEntry();
}
看看这些条件:
a&&a
始终与a
单独测试相同。]public static void removeLineFromFile(String file, int action, String lineToFind, String newLine) {
然后像这样进行测试:
switch (action) {
case 1:
... remove line
break;
case 2:
... replace line with 'newLine'
break;
case 3:
... add 'newLine' after the old line
break;
default:
throw new RuntimeException("Illegal Option");
}
请务必重写与“lineToFind”不匹配的任何行。尝试一下。它有点凌乱,您可以将其清理干净。但这正是您要寻找的。您可以添加一个条目,如果它存在,它将不会再次添加。它还将删除一个条目。查看其使用的主要方法。构造函数处理cre如果该文件不存在,则删除该文件
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class FileModification
{
private File file = null;
/**
* Constructor
*
* @param file
*/
public FileModification(File file)
{
this.file = file;
if(!file.exists())
{
try
{
file.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
* Add an entry if it doesn't exist
*
* @param entry
* @return true if successfully added/updated
*/
public boolean addEntry(String entry)
{
try
{
// Construct the new file that will later be renamed to the original
// filename.
File tempFile = new File(file.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
// Read from the original file and write to the new
// unless content matches data to be removed.
String line;
boolean isDuplicate = false;
while ((line = br.readLine()) != null)
{
if (line.equals(entry))
{
isDuplicate = true;
System.out.println("Is duplicate");
}
pw.println(line);
pw.flush();
}
if(!isDuplicate)
{
System.out.println("Added: " + entry);
pw.println(entry);
pw.flush();
}
pw.close();
br.close();
// Delete the original file
if (!file.delete())
{
System.out.println("Could not delete file");
return false;
}
// Rename the new file to the filename the original file had.
if (!tempFile.renameTo(file))
{
System.out.println("Could not rename file");
return false;
}
return true;
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
/**
* Delete entry
*
* @param entry
* @return true if successfully deleted
*/
public boolean deleteEntry(String entry)
{
try
{
// Construct the new file that will later be renamed to the original
// filename.
File tempFile = new File(file.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
// Read from the original file and write to the new
// unless content matches data to be removed.
String line;
while ((line = br.readLine()) != null)
{
if (!line.equals(entry))
{
pw.println(line);
pw.flush();
}
else
{
System.out.println("Deleted: " + entry);
}
}
pw.close();
br.close();
// Delete the original file
if (!file.delete())
{
System.out.println("Could not delete file");
return false;
}
// Rename the new file to the filename the original file had.
if (!tempFile.renameTo(file))
{
System.out.println("Could not rename file");
return false;
}
return true;
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
/**
* @return the file
*/
public File getFile()
{
return file;
}
/**
* @param file
* the file to set
*/
public void setFile(File file)
{
this.file = file;
}
public static void main(String[] args)
{
FileModification mod = new FileModification(new File("TEST.txt"));
mod.addEntry("NEW ENTRY1");
mod.addEntry("NEW ENTRY2");
mod.addEntry("NEW ENTRY3");
mod.deleteEntry("NEW ENTRY1");
}
}
“lineToRemove”是一个标识符,类似于唯一标识每一行的学校ID。第一个if应该删除ID行。第二个编辑,第三个“添加新内容”。“应该是。”“是关键词。它不会那样做。它不能这样做,因为您只有一个id,但它要么被找到,要么没有。那是两个州。你想做三件事,当你只允许两种状态时,你不能做三件事。