Deleting a certain character from a text file in j

2019-08-27 08:07发布

So I've recently received a large text file that I need to read unfortunately its formatted terribly as there are next line characters everywhere making it extremely difficult to read. So I've been trying to think of a way to sort through the file and delete each nextLine using a java program.

For example if we had this text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse rhoncus interdum      condimentum. Proin viverra   
justo vel imperdiet sagittis, purus sapien sagittis mi, et blandit purus ante non libero. Nulla ac   
augue ut odio eleifend interdum  
 ac id justo. Quisque rutrum euismod sem, vel euismod nunc convallis eu. Praesent odio velit,
 condimentum id scelerisque  
 iaculis, vulputate vitae lacus. Fusce adipiscing blandit libero eu venenatis.
 Vestibulum nec urna   
pulvinar arcu pretium   
pretium. Duis vitae augue dolor. Etiam consectetur feugiat
 diam sit amet gravida. 

we would like it to turn out like this :

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse rhoncus interdum
condimentum. Proin viverra, justo vel imperdiet sagittis, purus sapien sagittis mi,       
blandit purus ante non libero. Nulla ac augue ut odio eleifend interdum ac id justo.
Quisque rutrum euismod sem, vel euismod nunc convallis eu. Praesent odio velit,
condimentum id scelerisque iaculis, vulputate vitae lacus. Fusce adipiscing blandit
libero eu venenatis. Vestibulum nec urna pulvinar arcu pretium pretium. Duis vitae
augue dolor 

I'm not very experienced with reading/writing text file using Java. My current idea is reading in each char value checking if it is equal to the value of next line and proceeding to write that char to a separate text file if it is not. But i'm not really sure on how to implement this. I'm not sure how to check if a the next character is a next line.

Any help would be great.

3条回答
爷、活的狠高调
2楼-- · 2019-08-27 08:41

You can use a Scanner to do this.

Scanner in = new Scanner(new File(filename));
String new_file_string = "";
while (in.hasNextLine())
    new_file_string += in.nextLine();
// write new_file_string to file.
查看更多
冷血范
3楼-- · 2019-08-27 08:41

I suggest that you use a BufferedReader, and make use of the readLine() method to read the file a line at a time.

  • When you read an empty line, the method will return a (non-null) empty string.

  • When you get to the end of file, the method will return a null.

  • Otherwise, the method will return the line ... with the line terminator removed. (So when you output the line, don't forget to add a line terminator!)

This should be enough for you to write the program yourself. (And that would be a GOOD THING!)

查看更多
别忘想泡老子
4楼-- · 2019-08-27 08:59
File file = new File("yourFilePath.txt");  // create File object to read from
Scanner scanner = new Scanner(file);       // create scanner to read
Printwriter writer = new PrintWriter("someOutputFile.txt"); // create file to write to

while(scanner.hasNextLine()){  // while there is a next line
    String line = scanner.nextLine();  // line = that next line

    // do something with that line
    String newLine = "";

    // replace a character
    for (int i = 0; i < line.length(); i++){
        if (line.charAt(i) != '*') {  // or anything other character you chose
            newLine += line.charAt(i);
        }
    }

    // print to another file.
    writer.println(newLine);
}

You could also just write back to the same file, but that would require using a StringBuilder.

查看更多
登录 后发表回答