Results 1 to 5 of 5
- 05-03-2011, 12:34 AM #1
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 47
- Rep Power
- 0
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>
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?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"); } }
-
try this
method to check char for new lineJava Code:... String line = ""; while (br.ready()) { line += br.readLine(); } char lastChar = line.charAt(line.length()-1); isNewLine(lastChar); ...
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.
- 05-03-2011, 04:17 AM #3
That's right.i think readLine() returns the String line without the new line char?
db
- 05-06-2011, 06:39 PM #4
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 47
- Rep Power
- 0
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.
- 05-06-2011, 07:04 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
String file and carriage returns
By AJArmstron@aol.com in forum New To JavaReplies: 2Last Post: 04-17-2010, 01:28 AM -
Waiting for a carriage return
By Ebodee in forum New To JavaReplies: 1Last Post: 02-12-2010, 03:46 AM -
How to know when a thread ends.
By new_2_java in forum New To JavaReplies: 8Last Post: 10-24-2008, 11:22 AM -
Does any file in an FTP server ends up in an HTTP server?
By islamfunny in forum CLDC and MIDPReplies: 4Last Post: 08-15-2008, 04:30 PM -
New line or Carriage Return through FileWriter
By johnt in forum New To JavaReplies: 2Last Post: 05-20-2007, 09:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks