Мой текстовый файл будет выглядеть так:
а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ 1
б ~ б ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ Ь ~ 2
с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ 3
Мой план - найти номер в конце строки и
удалить эту строку.
Я уже успел найти это число с помощью Delimeter.
Но я не знаю, как удалить всю строку.
Например, я ищу № 2, я хочу, чтобы мой вывод был таким, как показано ниже:
а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ а ~ 1
с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ с ~ 3
Это мой последний код редактирования, и я застрял здесь ..
public class test {
static Scanner x;
public static void main(String [] args) {
String filepath = "input.txt";
String removeterm = "1";
removeRecord(filepath,removeterm);
}
private static void removeRecord(String filepath, String removeterm) {
String tempFile = "temp.txt";
File oldfile = new File(filepath);
File newfile = new File(tempFile);
String ID1 = ""; String ID2 = ""; String ID3 = "";
String ID4 = ""; String ID5 = ""; String ID6 = "";
String ID7 = ""; String ID8 = ""; String ID9 = "";
String ID10 = ""; String ID11 = ""; String ID12 = "";
String ID13 = ""; String ID14 = ""; String ID15 = "";
String ID16 = ""; String ID17 = ""; String ID18 = "";
String ID19 = ""; String ID20 = ""; String ID21 = "";
String ID22 = ""; String ID23 = ""; String ID24 = "";
String ID25 = "";
try
{
FileWriter fw = new FileWriter(tempFile,true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
x = new Scanner(new File(filepath));
x.useDelimiter("[~\n]");
while(x.hasNext())
{
ID1 = x.next(); ID2 = x.next(); ID3 = x.next();
ID4 = x.next(); ID5 = x.next(); ID6 = x.next();
ID7 = x.next(); ID8 = x.next(); ID9 = x.next();
ID10 = x.next(); ID11 = x.next(); ID12 = x.next();
ID13 = x.next(); ID14 = x.next(); ID15 = x.next();
ID16 = x.next(); ID17 = x.next(); ID18 = x.next();
ID19 = x.next(); ID20 = x.next(); ID21 = x.next();
ID22 = x.next(); ID23 = x.next(); ID24 = x.next();
ID25 = x.next();
if(!ID1.equals(removeterm)) {
pw.println(ID1 + "~" + ID2 + "~" + ID3 + "~" + ID4 + "~" + ID5 + "~" + ID6 + "~" + ID7 + "~" + ID8 + "~" + ID9 + "~" + ID10 + "~" + ID11 + "~" + ID12 + "~" + ID13 + "~" + ID14 + "~" + ID15 + "~" + ID16 + "~" + ID17 + "~" + ID18 + "~" + ID19 + "~" + ID20 + "~" + ID21 + "~" + ID22 + "~" + ID23 + "~" + ID24 + "~" + ID25);
JOptionPane.showMessageDialog(null, "Done !");
}
}
x.close();
pw.flush();
pw.close();
oldfile.delete();
File dump = new File(filepath);
newfile.renameTo(dump);
}
catch (NoSuchElementException exception) {
// Output expected NoSuchElementExceptions.
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Error !" + e);
}
}
}