Results 1 to 6 of 6
  1. #1
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default Supressing empty lines and does not do until the end! Why ?

    Hi guys,

    I really don't understand what's happening in my program, almost mad!

    Basically, it just takes a file in input and outputs a file.
    What it tries to do is just suppress all the empty lines, very simple.

    Here is the body of my method :

    Java Code:
    private static void suppressSpaces(String in, String out) {
    		
    		try {
    			FileReader fr = new FileReader(in);
    			FileWriter fw = new FileWriter(out);
    			
    			BufferedReader br = new BufferedReader(fr);
    			PrintWriter pw = new PrintWriter(fw);
    			
    			String inLine = null;
    		
    			while ((inLine = br.readLine()) != null) {
    
    				if (inLine.trim().length() != 0) {
    					pw.println(inLine);
    				}
    			}
    			
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    The input file has 20'609 lines to process and I don't understand why but it kind of stops processing after the line 20'350 about. So in the output file, there is about 97-98% of what there should be normally. No idea why...The file is very regular, I mean there is nothing special at the line 20'350, it's just the same as before.

    I attached a sample input file in TXT format (the whole file exceeds the limit), please have a look if you want.

    Here are the 10 last lines of the output file :

    Java Code:
    1336咸
    BAB0 喊  han3
    BCEA 缄  jian1
    BCEE 碱  jian3
    BCF5 减  jian3
    CFCC 咸  xian2
    F3F0 箴  zhen1
    1337感
    B8D0 感  gan3
    BAB3
    You can see it stops in the middle of the category 1337 and there is total of 1353 categories so it's almost done but dunno why, it doesn't go until the end!!

    Anyone has an idea ?

    Thanks in advance for your help guys!
    Attached Files Attached Files
    Last edited by ze snow; 02-18-2010 at 01:58 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    What I see from the start is that you skip the first line of the input file:
    Java Code:
    String inLine = null;
    //inLine = br.readLine(); <.--- What for???
    		
    while ((inLine = br.readLine()) != null) {
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Ah sorry, that's a mistake.
    This was part of a small test I did before and I forgot to remove it when I posted this topic.

    I edit it.
    Thanks for noticing btw.

    Still the same problem without this line....

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Ok, Ive tested with this:

    Java Code:
    FileReader fr = new FileReader("D:/pinyinlist.txt");
    FileWriter fw = new FileWriter("D:/pinyoutlist.txt");
    		
    BufferedReader br = new BufferedReader(fr);
    PrintWriter pw = new PrintWriter(fw);
    		
    String inLine = null;
    //inLine = br.readLine();
    int linesRead = 0;
    int linesWritten = 0;
    int linesSkipped = 0;
    while ((inLine = br.readLine()) != null) {
    	linesRead++;
    	if (inLine.trim().length() != 0) {
    		pw.println(inLine);
    		linesWritten++;
    	}else{
    		linesSkipped++;
    	}
    }
    pw.flush();
    pw.close();
    br.close();
    System.out.println("Read : " + linesRead);
    System.out.println("Skip : " + linesSkipped);
    System.out.println("Write: " + linesWritten);
    output:
    Read : 1501
    Skip : 845
    Write: 656

    Outputfile attached.
    Attached Files Attached Files
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Wow, it works now...

    But I don't understand why, can you explain to me please ?
    is it because of the flush() method ?

    Thanks so much!

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    To be honest, I always flush the output streams and close the resources. I never use Streams, your best chance is to have a look at the API and the IO tutorial:
    Lesson: Basic I/O (The Java Tutorials > Essential Classes)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. empty arrays.
    By circuspeanuts in forum New To Java
    Replies: 11
    Last Post: 04-06-2009, 08:08 PM
  2. KeyBindings Empty Key?
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 10-24-2008, 08:28 PM
  3. Empty ResultSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 09:36 PM
  4. Removing empty lines from code using Eclipse
    By javaplus in forum Eclipse
    Replies: 1
    Last Post: 12-14-2007, 10:21 PM
  5. BufferedReader empty
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 07:52 PM

Posting Permissions

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