在数据末尾找到参数时如何删除选定行?
How to remove selected line when argument is found at the end of data?
我的文本文件如下:
a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~1
b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~2
c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~3
我的计划是找到行尾的数字,然后
删除该行。
我已经使用 Delimeter 找到了那个数字。
但是我不知道如何删除整行。
例如我搜索No.2,我希望我的输出如下:
a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~1
c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~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);
}
}
}
可能会有帮助!
第 1 步:将您的文件读入列表(推荐使用 LinkedList),例如元素是一行 "a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~1"。
步骤 2:Just 尝试删除列表[i].endWiths("yournumber") 中的元素,然后重写文件或对您的列表做一些事情。 Refer
使用 Java8 功能和 NIO(自 Java7 起)解决方案将非常简单:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
String filepath = "./input.txt";
String removeterm = "1";
removeRecord(filepath, removeterm);
}
private static void removeRecord(String filepath, String removeterm) {
try {
List<String> filtered = Files.readAllLines(Paths.get(filepath)).stream().filter(s -> !s.endsWith(removeterm)).collect(Collectors.toList());
Files.write(Paths.get("./temp.txt"), filtered);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我的文本文件如下:
a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~1 b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~b~2 c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~3
我的计划是找到行尾的数字,然后 删除该行。
我已经使用 Delimeter 找到了那个数字。 但是我不知道如何删除整行。
例如我搜索No.2,我希望我的输出如下:
a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~1 c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~c~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);
}
}
}
可能会有帮助!
第 1 步:将您的文件读入列表(推荐使用 LinkedList),例如元素是一行 "a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~a~1"。
步骤 2:Just 尝试删除列表[i].endWiths("yournumber") 中的元素,然后重写文件或对您的列表做一些事情。 Refer
使用 Java8 功能和 NIO(自 Java7 起)解决方案将非常简单:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
String filepath = "./input.txt";
String removeterm = "1";
removeRecord(filepath, removeterm);
}
private static void removeRecord(String filepath, String removeterm) {
try {
List<String> filtered = Files.readAllLines(Paths.get(filepath)).stream().filter(s -> !s.endsWith(removeterm)).collect(Collectors.toList());
Files.write(Paths.get("./temp.txt"), filtered);
} catch (IOException e) {
e.printStackTrace();
}
}
}