Results 1 to 5 of 5
  1. #1
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Question Can't determine whether a text file ends with carriage return

    I am making a program that must determine whether a text file ends with a newline or not, or a carriage return.

    For example, suppose I create a file called text.txt and add some text, but I add a carriage return at the end.

    I want my program to return true if the file ends in a new line character or carriage return, and false if it doesn't. However, my program always returns false.

    Here is a complilable SSCCE representing my problem. In order to test it, please create a random "text.txt" file and make sure it ends in a newline. You will see that my program returns false regardless.

    Here it is>

    Java Code:
    import java.io.*;
    
    
    public class mySSCCE{
    
    
    	public static final String newline = System.getProperty("line.separator");
    
    
    
    
    	public static void main(String[] args){
    
    		try{
    
    		System.out.println(endsInNewLine("text.txt"));
    
    		}catch(IOException ioe){}
    
    	}
    
    
    
    
    	public static boolean endsInNewLine(String path) throws IOException{
    
            	File file = new File(path);
    		String line = new String();
    
            	InputStreamReader streamReader = new InputStreamReader(new FileInputStream(file));
            	BufferedReader br = new BufferedReader(streamReader);
    
            	
    		//get last line of file
    
            	while (br.ready()) {
               	line = br.readLine();
    		
            	}
    
    
    		br.close();
    
    		return line.endsWith(newline) || line.endsWith("\n");
    
    
    	}
    
    
    }
    Thanks for your help. To sum up my question: how do I check whether a text file ends in a carriage return or new line?

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    try this

    Java Code:
    ...
    String line = "";
    while (br.ready()) {
      line += br.readLine();
    }
    char lastChar = line.charAt(line.length()-1);
    isNewLine(lastChar);
    ...
    method to check char for new line
    Java Code:
    public static final char CR = '\r';
    public static final char LF = '\n';
     
    public void isNewLine(char c) {
       boolean newLine = (c==LF) || (c==CR);
       System.out.println("Ended with new line: " + newline);
    }

    Edit:
    On second thoughts, i think readLine() returns the String line without the new line char?
    Last edited by ozzyman; 05-03-2011 at 01:47 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    i think readLine() returns the String line without the new line char?
    That's right.

    db

  4. #4
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Thumbs up

    The solution didn't really work for me because it seems like readline() will ignore the last new line character whether or not I used "line += br.readLine()" the last character simply will not be a new line.

    I decided to skip this issue in my program. However, thank you guys for clearing up the fact that readline() does exclude the new line character. Otherwise I would still be scratching my head at why it doesn't work.

    Thanks guys.
    Last edited by PrinceSendai; 05-06-2011 at 07:17 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Use a RandomAccessFile, position the file pointer near the end of the file, read one or two bytes and check whether or not they are \r and/or \n.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. String file and carriage returns
    By AJArmstron@aol.com in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 01:28 AM
  2. Waiting for a carriage return
    By Ebodee in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 03:46 AM
  3. How to know when a thread ends.
    By new_2_java in forum New To Java
    Replies: 8
    Last Post: 10-24-2008, 11:22 AM
  4. Does any file in an FTP server ends up in an HTTP server?
    By islamfunny in forum CLDC and MIDP
    Replies: 4
    Last Post: 08-15-2008, 04:30 PM
  5. New line or Carriage Return through FileWriter
    By johnt in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 09:13 PM

Tags for this Thread

Posting Permissions

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