What is a new line, really?
Continuing my voyage into Java's I/O space...
Okay, this might sound like a really stupid question, but I'm all new to programming. :)
But what is a new line, really!? I ask since I've gotten the impression that there is more than one "kind" of new line.
I've written the code below to count the number of lines in a file. It seems to work but what if a file contains a different "new line flavor?
int linesInFile()throws IOException
{
FileReader fileIn =null;
Scanner scanner =null;
int numberOfLines = 0;
try{
fileIn =new FileReader("/users/stefan/test/test2.txt");
BufferedReader bufferedIn =new BufferedReader(fileIn);
scanner =new Scanner(bufferedIn);
scanner.useDelimiter("\n");
while ( scanner.hasNext() )
{
System.out.println(scanner.next());
numberOfLines++;
}
}
finally
{
if ( fileIn !=null )
{
scanner.close();
fileIn.close();
}
}
return numberOfLines;
}
By the way. I'm closing the Scanner and the FileReader - but not the BufferedReader. I think it will close when the try statement ends - is that correct?
Kind regards,
Stefan
You're right in that there's more than one type of newline - depending on your system it can be a carriage return followed by a newline character, just a newline character, or some other more uncommon combinations. As it happens, Windows' Notepad is just about the only application that doesn't handle other types of linebreaks correctly.
You can get the line break on the current system by using System.getProperty("line.separator")
, I believe. Using the methods provided by BufferedReader such as "newline()" rather than outputting "\n" characters is usually recommended.
I see no newline() method in BufferedReader. :(
Sorry, BufferedWriter, for when you're writing files. http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedWriter.html
Great, thanks. But do I really have to import a BufferedWriter class to insert something so trivial as a new line!? There must be another way - or what?
Perfect! I just did like this: scanner.useDelimiter( System.getProperty("line.separator") ); Thanks! :)
> Great, thanks.
> But do I really have to import a BufferedWriter class
> to insert something so trivial as a new line!?
> There must be another way - or what?
Why are you so against importing a class ? The more code you import the more you are making use of someone else's code, therefore the more productive you will most likely be.
Well, importing more classes also means filling up more memory. Doesn't it?
> Well, importing more classes also means filling up> more memory. Doesn't it?The classloader will load the classes only when it has to use them the first time.
> Well, importing more classes also means filling up
> more memory. Doesn't it?
Don't worry too much about memory.
But why not do something like this:int linesInFile(String fileName) throws IOException {
int numberOfLines = 0;
Scanner scan = new Scanner(new File(fileName));
while(scan.hasNextLine()) {
numberOfLines++;
scan.nextLine();
}
scan.close();
return numberOfLines;
}
Yes, why don't do something like that!? Because I'm not so clever - ( only one month with programming :) Thanks a million! That is much more elegant.
> Well, importing more classes also means filling up
> more memory. Doesn't it?
Yes, it does. But you don't have a sense of scale yet. A Java class might take as much as 64K of memory, but you have hundreds of megabytes of the stuff. It's as though you were sitting in a large football stadium by yourself and you were concerned that there might not be a seat for another person.
Very nice way to express it. Thanks... :)
> Yes, why don't do something like that!? > > Because I'm not so clever - ( only one month with> programming :) >> Thanks a million! That is much more elegant.You're most welcome.; )
> ...> stuff. It's as though you were sitting in a large> football stadium by yourself and you were concerned> that there might not be a seat for another person.Nice analogy.; )
It always surprised me that MS-Windows uses the \r\n pair for a logical
newline. If you look at those old typewriters when you push that lever
at the right of the cyclindrical carriage, the first thing that happens is that
the cyclinder rotates one line. When you keep on pushing the entire
carriage returns to its home position. I find \n\r much more natural ;-)
kind regards,
Jos (*ping!*)
JosAHa at 2007-7-21 18:04:05 >

Just to be sure... The solution prometheuzz suggest is elegant I think - but how can I be sure if it works on all platforms when the "line.separator" isn't used?
Both BufferedReader and Scanner are programmed to recognize all three of the common line separators: "\r\n" (Windows) or "\n" (Linux/Unix/OSX) or "\r" (older MacOS), even if they're all used in the same file. Like virtually every other commonly-used program today, Java takes care of line separators so you don't have to think about them. The one exception is Windows Notepad, which refuses to recognize anything but "\r\n" (as far as I can tell, it's still broken in Vista). But as long as you use println(), newline(), etc. when writing files, you shouldn't have any problems.
> > Well, importing more classes also means filling up
> > more memory. Doesn't it?
>
> Yes, it does.
No, it doesn't.
Using (which implies loading) more classes uses more memory, but importing (that is, using the import keyword) has zero effect on what happens at runtime.
Yes, I know you knew that. Just engaging in a little PBS.
And Sefan, I don't mean to confuse you with this, but...
If you really are talking about importing (using the keword "import"), then that by itself has no effect on memory footprint or performance. That's simply a compile-time shortcut to let the compiler know what you mean when you provide unqualified class names. Loading classes at runtime (because you need to use them) does take up more memory, but, as DrClap said, not that much in the big picture.
jverda at 2007-7-21 18:04:05 >

Thanks, both of you. :) I like to have certainty from the beginning. So, e.g. knowing that the import statement in the beginning of my classes actually doesn't load the classes into memory is important for me. Once again, thanks.
> (as far as I can tell, it's still broken in Vista). Though IMO, it's actually everything else that's broken. If a newline is defined to be "\r\n", then wrapping only at '\n' is incorrect behavior on the Windows platform. Surely more convenient, but not as specified.
> It always surprised me that MS-Windows uses the \r\n> pair for a logical newline. Blame the Teletype Corporation. It originates with Teletypes, not Typewriters.
> > It always surprised me that MS-Windows uses the \r\n pair for a
> > logical newline.
>
> Blame the ******** Corporation. It originates with ********s, not Typewriters.
Don't say that! I sincerely hate those ********s, especially the TTY/33;
they still make me puke! they made me go crazy! especially those wobbly,
wobbly, wobbly little tape readers that always just *ate* the tapes and
those tape punches that ripped your precious tapes to pieces and
those darn stupid cylindrical keys that always got stuck or kept on
bouncing and that paper feed that didn't work and those ink ribbons that
always broke! and ... and ... pant! *gasp*
sorry, I feel better now ... <dammit>
kind regards,
Jos ;-)
JosAHa at 2007-7-21 18:04:05 >

Have you been at the paint thinner? Again?
> Have you been at the paint thinner? Again?We call it Jenever, in the Netherlands.; )
> > Have you been at the paint thinner? Again?> > We call it Jenever, in the Netherlands.> ; )Define "we" please.kind regards,Jos ;-)
JosAHa at 2007-7-21 18:04:05 >

> > We call it Jenever, in the Netherlands.> > ; )> > Define "we" please.> > kind regards,> > Jos ;-)We Frisians of course!; )Beerenburg boppe!
> > > We call it Jenever, in the Netherlands.
> > > ; )
> >
> > Define "we" please.
>
> We Frisians of course!
> ; )
>
> Beerenburg boppe!
Please people stay behind that safety line; this Frisian savage is about
to perform the "Beerenburg boppe" tribal war dance. Please do note that
bystanders have been killed by razor blade sharp "Frisian doorlopers"
because of this primitive tribal dance. Don't feed the savage please and
do enjoy the show. Yowza-yowza-yowza! ;-)
kind regards,
Jos ;-)
JosAHa at 2007-7-21 18:04:05 >
