Results 1 to 13 of 13
  1. #1
    Camden is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default how can we remove blank lines from a .txt

    Copy a file to another file removing all blank lines.

    Typical Input

    Here is an

    input text with just two

    blank lines.

    Typical Output
    Here is an
    input text with just two
    blank lines.

    coding till now (im using acm library, dont pay attention on readLine()):
    Java Code:
    String inputFile = readLine("Insert file: ");
                BufferedReader inputFileReader = new BufferedReader(new FileReader(inputFile));
                String inputFileLine;
                String NewFile = readLine("Insert file name of the new file: ");
        		PrintWriter outputFile=new PrintWriter (new FileWriter(NewFile));
        		while((inputFileLine = inputFileReader.readLine()) != null) {
               	char [] characters= inputFileLine.toCharArray();
               	for (int i=0; i<characters.length; i++){
                	 outputFile.print(characters[i]);
                         }
                outputFile.println("");
                } 
                inputFileReader.close();
                outputFile.close();
    Last edited by Camden; 03-11-2008 at 08:45 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class IOTest {
        public static void main(String[] args) throws IOException {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Insert file: ");
            String inputFile = //scanner.nextLine();
                               "ioTestIn.txt";
            BufferedReader inputFileReader = new BufferedReader(
                                             new FileReader(inputFile));
            String inputFileLine;
            System.out.println("Insert file name of the new file: ");
            String NewFile = //scanner.nextLine();
                             "ioTestOut.txt";
            scanner.close();
            PrintWriter outputFile = new PrintWriter(new FileWriter(NewFile));
            while((inputFileLine = inputFileReader.readLine()) != null) {
                if(inputFileLine.length() == 0)
                    continue;
                outputFile.println(inputFileLine);
            } 
            inputFileReader.close();
            outputFile.close();
        }
    }
    ioTestIn.txt
    Java Code:
    Here is an 
    
    input text with just two 
    
    blank lines.

  3. #3
    reachjava is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default

    How to do this with out writing to a new file, just to read the input from a string variable with empty lines in it and store the result to a new String varaiable, after the empty lines are removed.
    Last edited by reachjava; 07-27-2011 at 12:29 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default

    read the input from a string variable with empty lines in it and store the result to a new String varaiable, after the empty lines are removed.
    One way would be to use the Scanner class to "read" the lines from the String.
    Concatenate the non empty lines to the new String variable.

  5. #5
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    One approach would be to do this:

    Assumptions:
    file 1 exists
    file 2 will be created

    Pseudo Code:
    Java Code:
    Open file 1 for reading
    Open file 2 for writing
    
    while true
        if file1 != ready // EOF encountered
            break
    
        String line = readline
        
        if readline == blank
            // do nothing
        else
            write to file 2
    
    end while
    
    close file1
    close file2

  6. #6
    reachjava is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default

    Hi,
    Thank you for the post. My requirement is not to write to a new file, I will be getting file content in a String variable, in that empty lines will be there at the end of the file, I want to remove these empty lines and put it into a new String variable and I have to use that new variable for further processing...

    Thanks..

  7. #7
    reachjava is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default

    I tried this norm.. Want I want to know is by default outputStreamWriter,FileWriter and PrintWriter writes a empty line at the end of the file? which writes stops the cursor exactly at the last content line in the file?

    Thanks..

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Quote Originally Posted by reachjava View Post
    I want to remove these empty lines and put it into a new String variable and I have to use that new variable for further processing
    This is a bad idea if your file is large and you will run out of memory.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Quote Originally Posted by reachjava View Post
    I tried this norm.. Want I want to know is by default outputStreamWriter,FileWriter and PrintWriter writes a empty line at the end of the file? which writes stops the cursor exactly at the last content line in the file?
    Huh?

    In the above post you say you will not be creating a new file. So why are you asking about all these writer classes that are used for writing out to a file?

  10. #10
    reachjava is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default

    No, this won't be a large file, it will have not more than 20 lines. I have to use it in another tool, there I have to retrieve the file as a String, there I have to do the remaining process, like removing the empty lines and to use that new variable.

  11. #11
    reachjava is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default

    hi master, if you are interested in replying, pls reply, other wise leave it.. why you are saying all this.. Is there anything wrong in knowing, what is not clear or not known from the experts!!

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Are you talking to me? You are not making any sense. All I am trying to do is get you to clarify what you are trying to do. If you don't want my help then I don't give a damn. No skin of my nose.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default

    want to know is by default outputStreamWriter,FileWriter and PrintWriter writes a empty line at the end of the file?
    I don't think they do. Is it happening with a program that you have written?
    Can you post a SSCCE to show the problem?

Similar Threads

  1. Replies: 0
    Last Post: 04-06-2008, 07:45 PM
  2. Joining lines in Eclipse
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-04-2007, 11:17 AM
  3. how to edit lines.
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 04:41 AM
  4. Print a blank space
    By susan in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 01:58 PM
  5. Blank result for jsp_servlet in Eclipse
    By Unni in forum Eclipse
    Replies: 2
    Last Post: 07-12-2007, 04:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •