简体   繁体   中英

Remove a line from txt if all null

So I have a text file which contains an array of lines. Some of them are all null entries. Meaning some lines could be null;null;null;null;null;null; and some are null;null;Apple;Orange;null; The length of each line is different.

How do I remove lines from that txt file that contain ALL null entries only?\\

This is my code now

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(ss!=null){
            allNull = false;
            tncWriter.write(s + System.getProperty("line.separator"));
            break;
        }
    }
}

If I understand your question properly then you have to modify as per below

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;
        }
    }            
}

I finally figured it out after awhile haha. Sorry I wasn't clear too. The null was actually a String object too. And yes I want to delete lines which contain the words "null" throughout. Also I found a way to delete the lines inside that file (cheatingly).

The code is as such:

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
}

As the cheating goes, after I've written to the temp file "temp.txt" I go ahead to delete the original and then rename the temp to it. So it would seem like I've gone in and deleted the lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM