所以我有一个包含一系列行的文本文件。其中一些都是空条目。意思是一些行可以为null; null; null; null; null; null;有些是null; null; Apple; Orange; null; 每条线的长度不同。
如何从该txt文件中删除仅包含所有空条目的行?\
这是我现在的代码
update(id) {
//find and update your item, you can do it since you have an 'id'
//follow link: http://codereview.stackexchange.com/questions/43438/writing-a-function-to-add-or-modify-an-existing-object-inside-an-array
// this.setState({
// data:
// })
}
const Links = (props) => {
return (
<li>
<p>{props.data.name}</p>
<p>{props.data.vote}</p>
<button onClick={() => props.update(props.id)}>Up</button>
</li>
);
};
答案 0 :(得分:2)
如果我理解你的问题,那么你必须按照以下修改
BufferedReader tncReader = new BufferedReader(new FileReader("something.txt"));
BufferedWriter tncWriter = new BufferedWriter(new FileWriter("something_cleaned.txt"));
boolean allNull = true;
while(tncReader.readLine() != null ){
String s = tncReader.readLine();
String[] currentLine = s.split(";");
System.out.println(currentLine[0]);
for(String ss:currentLine){
if(!"null".equalIgnoreCase(ss)){ //this line modified
allNull = false;
tncWriter.write(s + System.getProperty("line.separator"));
break;
}
}
}
答案 1 :(得分:0)
代码是这样的:
File input = new File("something.txt");
File output = new File("temp.text");
BufferedReader reader = new BufferedReader(new FileReader(input));
BufferedWriter writer = new BufferedWriter(new FileWriter(output));
String current;
while((current = reader.readLine())!=null){
String[] data = current.split(";"); //puts whole line into an array
boolean allNull = true;
//check if entire array is all null
for(String s:data){
if(!s.equals("null")){
allNull = false; //any traces of some other word besides null would render the boolean untrue
}
}
if(allNull==false){
writer.write(current + System.getProperty("line.separator"));
}
}
reader.close();
writer.close();
//Delete original file
if(!input.delete()){
System.out.println("Could not delete file"); //error handling
}
//Rename file to original
if(!output.renameTo(input)){
System.out.println("Could not rename file"); //error handling
}
作为作弊行为,在我写入临时文件&#34; temp.txt&#34;我继续删除原始文件,然后将temp重命名为它。所以看起来我已经进入并删除了这些行。