public static String findAndDeleteStudent(String qStudent)
{
String fileName = "data/Students.txt";
String findNumber = qStudent;
File file = new File(fileName);
Scanner dataText = null;
String result="";
try
{
dataText = new Scanner(file);
while (dataText.hasNext())
{
String line = dataText.nextLine();
if (line.contains(findNumber))
{
//System.out.println(line);
//line.trim();
//System.out.println(items[1]);
System.out.println(line);
result ="done";
}
}
}
catch (NumberFormatException nfe)
{
result = "error";
}
catch (NoSuchElementException nsf)
{
result = "error";
}
catch (FileNotFoundException fnf)
{
result = "error";
}
return result;
}
我想知道刪除的行字符串,而無需重新編寫沒有行字符串的全部文本文件(我想刪除)的最短途徑。 我可以使用上面的代碼從文件中以字符串的形式檢索該行。 但無法刪除。請幫忙。在Java中,如何刪除符合搜索條件的文本文件中的行?
你不能隨機訪問文件,所以你必須做你不想做的事。的 – MarsAtomic
可能重複[刪除指定行從Java文本文件?(http://stackoverflow.com/questions/5800603/delete-specific-line-from-java-text-file) – MarsAtomic
感謝兄弟。我會試一試。 – VD007