BufferedWriter and Stringbuffer Issue- J2SDK.v.1.4.1
I was using this version of java and I wrote a program which appends strings to a string buffer.
It then converts the string buffer to a string and outputs that to a file using bufferedwriter.write
However it causes line breaks in the file where it shouldn't and I dont know why.
Are there any known issues with bufferedwriter in this java version?
[374 byte] By [
scorpion2a] at [2007-11-27 4:40:06]

No as far as I know...post your code.MeTitus
Any line breaks are coming from the source data or your code.
ejpa at 2007-7-12 9:51:00 >

Okay heres the code that does it
class Test2 {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("test.txt");
BufferedWriter bw = null;
StringBuffer strb = new StringBuffer();
strb.append("allow_from");
strb.append(" ");
strb.append("dq@dq.com");
strb.append("\n");
String bufferString= strb.toString();
try {
bw = new BufferedWriter(fw);
// Now put together the contents of the file:
for(int i=1;i<3001;i++)
{
bw.write(i+ " "+bufferString);
}
}
catch(IOException ioe) {
throw ioe;
}
finally {
if(bw!=null) bw.close();
}
}
}
I then use
"type test.txt " on the command prompt to see the contents of the text.txt file and thers a line break on line 1934 like this
"1
9324 allow_from dq@dq.com"
and on line 2879 like this
"2879 al
low_from dq@dq.com"
Message was edited by:
scorpion2
How do you know the byte count of your source? 3001MeTitus
strb.append("\n");That would be considered a line break.
> strb.append("\n");> > That would be considered a line break.Yep, you are constinuosly writing "\n" in your for statement.MeTitus
Is it only the "type" command that shows these spurious line breaks, or do programs such as wordpad also show them?
I dont know.
Im just testing it using 3001 so as to output 3001 lines.
Its for testing purposes.
There is an issue with the line breaking in the middle rather than at the end and it behaves the way I said above except the "2" in "9324" was a misprint.
It' should be 934.
There are no line breaks on wordpad.
Then you can consider this a bug/ "feature" of Microsoft's implementation of the type command. There's nothing you can do to fix it.
Sorry Im wrong about wordpad I cant test whats in wordpad because Im using putty to get into the machineMessage was edited by: scorpion2
then transfer the file to your workstation or use some command line text editor such as "edit"
Yes I transferred the text file over to my local machine to have a look at it in wordpad.
It has all the line breaks that occured when I used type to type it out to the screen and because I can see the whole file now I can see that the occurence of improper line breaks is worse than I thought.
Ive breaks at 660, 998 , a blank line between 1618 and 1619 and the one from 1933 to 1934 that I showed above.
I wonder what causes the lines to break so often.
Its definitely not writing properly to the file whatevers causing it.
Message was edited by:
scorpion2
The above is the whole program? You wouldn't have more than two threads writing to the file?Are the extra line breaks always in the same places or does it vary?
The first line break occurs after 17024 characters.Then the other line breaks occur after around every 8500 characters.Nope thats the only program.Message was edited by: scorpion2
strange .. have there been other problems with the machine in question? needless to say, there's apparently nothing wrong with the program and it works like it should when I run it
Have you ran it with that java version?
no, but i doubt 1.4.1 would have a bug this severe. have you tried running on other versions, like 1.4.2?
It worked ok on my local machine on java version(build 1.5.0_07-b03)Anybody ever heard of this?:Fast VM (build 1.4.1-2, build J2SDK.v.1.4.1I wonder could that be causing problems?Message was edited by: scorpion2
Fast VM? What OS is this? Tru64 UNIX?
It is OpenVMS Alpha V7.3-1Message was edited by: scorpion2
When I open the file in notepad I noticed that instead of changing to a new line it would have a character that looked like this "[]".
Is there anyway of getting that to change.
Also someone mentioned to me about outputtting to a dos text file.
How would I change it so I could do that?
Message was edited by:
scorpion2
What options does Fast VM have? Can you run the program in interpreted mode? Does the problem reoccur in interpreted mode?
Maybe you should take this issue up with whoever makes the virtual machine you are using? Sun only makes VMs for Windows, Linux, and Solaris.
Regarding DOS text files, the only difference between them and unix files is that the line separator is "\r\n" (yes, two characters) instead of just "\n". MS Notepad can't render Unix text files properly, it's kind of stupid in that way.