why buffer.toString does not accept new line in it?
I have tried to enter an empty line between the sentence but buffer.toString does nol allow me to how can i do that?
void convertFile(){
try{
outT =new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("C:/Word.txt",true),
enc)) );
/**************************************************/
BufferedReader in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(inputFilename),
enc));
Scanner sc =null;
try{
sc =new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
/**************************************************/
StringBuffer targetStrBuf =new StringBuffer();
while (sc.hasNext())
if (sc.hasNextInt())
{
int number = sc.nextInt();
targetStrBuf.append(number +" ");
}
else
{
String str = sc.next();
targetStrBuf.append(convert(str) +" ");
}
/*i have tried to put "\n" here put it didnt word*/
outT.println(targetStrBuf.toString());
outT.flush();
outT.close();
/**************************************************/
}finally{
if (sc !=null){
}
}//while for line
sc.close();
outT.print("\n");
outT.flush();
outT.close();
in.close();
outT.close();
}//try
catch (IOException exc)
{
System.err.println("I/O error writing to output file.");
}
}
> I have tried to enter an empty line between the> sentence but buffer.toString does nol allow me to how> can i do that?It does - either your code was wrong or your assumptions about '\n' are.
How are you viewing this file? The Windows newline is \r\n, not \n. Some editors are nice enough to treat \n as a newline as well.
When writing a text file, don't use \n or \r or \r\n. Use System.getProperty("line.separator"), or use BufferedWriter's newLine() (or something like that) method, or PrintWriter's println().
jverda at 2007-7-12 18:26:09 >

my code is working but the space is not inserted after each line
i did use System.getProperty("line.separator"), it gave me 4 empty lines even my data file have 2 lines sperated by one empty line?
what to do
the above code does not insert newline at the begining of the new readed line?
> what to do
>
Spend more time figuring out what the real problem is rather than letting us try to guess what it is.
As I already said, there is absolutely no problem adding a new line to a string buffer.
Thus the actual problem is that you are not in fact adding it or you are using the result in some way which does not generate the result you expect.
Presuming that you are doing the first correctly then the problem is with ththe second. But you haven't provided that information.
i first want to add new line each time the program move to new line in the file i.e
first line
empty line
second line
empty line
and so on
so after it dealt with each line by knowing what is integer and what is string write that line then insert a newline then read the new line
and so on
Again if you are actually adding a '\n' then it will be in the file.However there is difference between '\n' and what a particular OS/editor/display will consider to be a line terminator.
> However there is difference between '\n' and what a
> particular OS/editor/display will consider to be a
> line terminator.
i didnot understand ur sentence
my file having several lines that are sparted with 1 empty line between each sentece and the other
so, i want my program to know what is integer and what is string from that line that i has read it then right it again and add empty line after it then start reading the second line and do as above
but
bufferString do add all the data together and i can't insert an empty line between what it hold
how can i solve this
> > However there is difference between '\n' and what
> a
> > particular OS/editor/display will consider to be a
> > line terminator.
>
> i didnot understand ur sentence
>
If you wrote \n, then there is \n in the file.
But some editors and viewers will not use \n to signal that they should start drawing on the next line.
> my file having several lines that are sparted with 1
> empty line between each sentece and the other
No, they're separated with \n (or \r, or \r\n). Files don't have "empty lines." That concept doesn't apply to files. It only applies in the tools you use to view the files. Different tools on different OSes interpret charaters different ways.
jverda at 2007-7-12 18:26:09 >

> > However there is difference between '\n' and what
> a
> > particular OS/editor/display will consider to be a
> > line terminator.
>
> i didnot understand ur sentence
>
> my file having several lines that are sparted with 1
> empty line between each sentece and the other
>
> so, i want my program to know what is integer and
> what is string from that line that i has read it then
> right it again and add empty line after it then start
> reading the second line and do as above
> but
> bufferString do add all the data together and i can't
> insert an empty line between what it hold
> how can i solve this
You solve problems by
1. Breaking it into pieces
2. Understanding what you WANT to happen
3. Understanding what DOES happen.
An editor displays files using rules that are particular to the editor.
How you see it displayed in an editor is only related to what is in the file if you understand (rule 3) how the editor works.
If you don't understand what the editor is doing then it is pointless to use it.
And right now I am not confident that you actually understand what you need to happen (rule 2) anyways.
So if you sole objective is to make it show up correctly in the editor then you are going to have to experiment with combinations of '\n' and '\r' to determine exactly what the editor does with them. Until you understand how that works (rule 2) it is pointless do discuss how you get that to happen with StringBuffer (rule 3)
i do understand
the editor file are separeted with \n
and i want to use that program to output the lines with \n
i know that with bufferString i should use \r\n together
but where should i inserted in the code
since if i put it before it or with it, it cause no action
> i do understand
> the editor file are separeted with \n
> and i want to use that program to output the lines
> with \n
> i know that with bufferString i should use \r\n
> together
>
> but where should i inserted in the code
> since if i put it before it or with it, it cause no
> action
You're not making any sense.
The file will have exactly the characters you put into it. Nothing more, nothing less.
How those characters get displayed is purely a function of the editor or viewer you use, and has nothing to do with Java. The only thing you can do with Java is decide which characters to put into the file. Whatever you put in through Java will be there.
jverda at 2007-7-12 18:26:09 >

> i do understand
> the editor file are separeted with \n
> and i want to use that program to output the lines
> with \n
> i know that with bufferString i should use \r\n
> together
>
> but where should i inserted in the code
> since if i put it before it or with it, it cause no
> action
Right now you have a bunch of code.
Don't use that code.
Create an app, a brand new app, that uses a StringBuilder to put three lines together and then saves the result to a file.
Add whatever line terminator you want to those lines.
Verify that it creates a file.
Post that code.
Explain exactly what shows in your editor with the above brand new code.