qpisqp
qpisqp

Reputation: 51

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

Upvotes: 1

Views: 108

Answers (2)

qpisqp
qpisqp

Reputation: 51

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.

Upvotes: 0

SpringLearner
SpringLearner

Reputation: 13854

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

Upvotes: 2

Related Questions