如何从 java 中的文本文件中删除整数记录?
How to delete integer record from text file in java?
我有一个简单的问题...我编写这段代码是为了删除一条以字符串开头的记录,但是当尝试应用这段代码删除另一条以整数开头的记录时,它给了我一个错误
这是我的代码:
public void deleteSupplier(String company) throws FileNotFoundException, IOException
{
File inputFile = new File("C:\Users\فاطمة\Downloads\suppliers.txt"); // Your file
File tempFile = new File("C:\Users\فاطمة\Downloads\suppliers2.txt"); // temp file
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String currentLine;
while((currentLine = reader.readLine()) != null)
{
if(currentLine.contains(company))
continue;
writer.write(currentLine);
writer.newLine();
}
writer.close();
reader.close();
File inputFile1 = new File("C:\Users\فاطمة\Downloads\suppliers.txt"); // Your file
File tempFile1 = new File("C:\Users\فاطمة\Downloads\suppliers2.txt"); // temp file
inputFile1.delete();
boolean successful = tempFile1.renameTo(inputFile1);
if(successful == true)
System.out.println("Supplier deleted successfully");
else if(successful == false)
System.out.println("Error: deleting failed, supplier might not be found.");
}
例如我有这样的记录:
Hamada Port 111
Hobeee Jack 447
该代码适用于此,但当尝试为这样的记录应用代码时
1 Hamada 147
7 Hobeee 444
它给我错误。
注意:我用过:
int id
而不是
String company
并在函数中使用 id 而不是 company:
currentLine.contains(id);
但它给出了错误。
有什么帮助吗?
由于您正在调用 String class 的 'contains' 方法,您只能将 CharSequence 的子 classes 的实例作为参数,例如 String(就像您所做的那样对于您的字符串大小写)。由于 Integer 不是 CharSequence 接口的子class,因此在应用到 'contains' 方法之前必须对其进行转换。将 int 值转换为 String 将是可行的方法。
currentLine.contains(company)
我有一个简单的问题...我编写这段代码是为了删除一条以字符串开头的记录,但是当尝试应用这段代码删除另一条以整数开头的记录时,它给了我一个错误
这是我的代码:
public void deleteSupplier(String company) throws FileNotFoundException, IOException
{
File inputFile = new File("C:\Users\فاطمة\Downloads\suppliers.txt"); // Your file
File tempFile = new File("C:\Users\فاطمة\Downloads\suppliers2.txt"); // temp file
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String currentLine;
while((currentLine = reader.readLine()) != null)
{
if(currentLine.contains(company))
continue;
writer.write(currentLine);
writer.newLine();
}
writer.close();
reader.close();
File inputFile1 = new File("C:\Users\فاطمة\Downloads\suppliers.txt"); // Your file
File tempFile1 = new File("C:\Users\فاطمة\Downloads\suppliers2.txt"); // temp file
inputFile1.delete();
boolean successful = tempFile1.renameTo(inputFile1);
if(successful == true)
System.out.println("Supplier deleted successfully");
else if(successful == false)
System.out.println("Error: deleting failed, supplier might not be found.");
}
例如我有这样的记录:
Hamada Port 111
Hobeee Jack 447
该代码适用于此,但当尝试为这样的记录应用代码时
1 Hamada 147
7 Hobeee 444
它给我错误。
注意:我用过:
int id
而不是
String company
并在函数中使用 id 而不是 company:
currentLine.contains(id);
但它给出了错误。
有什么帮助吗?
由于您正在调用 String class 的 'contains' 方法,您只能将 CharSequence 的子 classes 的实例作为参数,例如 String(就像您所做的那样对于您的字符串大小写)。由于 Integer 不是 CharSequence 接口的子class,因此在应用到 'contains' 方法之前必须对其进行转换。将 int 值转换为 String 将是可行的方法。
currentLine.contains(company)