BufferedWriter: newLine();
I am using a bufferedwriter
try{
out =new BufferedWriter(new FileWriter("x.txt"));
out.write(inputString);
out.newLine();
out.close();
}
well this code just continues writing to the same line, why does out.newLine() did not work any ideas?
[466 byte] By [
oforcea] at [2007-11-27 6:46:13]

Didn't you ask this yesterday? Why did you open a whole new thread for this?
Anyway...you're only creating one line of output. So how many lines were you expecting?
UNLESS...perhaps this code is in the middle of a loop, and you neglected to tell us. Why tell the people you're asking help for about the most important aspect of the problem? Maybe you keep writing to x.txt using this block of code in the body of a loop, and you're confused because only the last line is ever seen. If that's the case, the reason is that you're opening the file in truncate mode, not append mode. Look at the other constructors for FileWriter, and you'll see one where you can tell it to append onto existing contents.
it suppose not to create one line, its not in a loop, and the variable is dynamic, it changes according to the other components, it receives 14 parameters and merges them on a string and writes to the x.txt but there is something wrong
it just write: eg:
a a
not
a
a
i have been seacrhing but still could not be able to find whats wrong...
Why merge fourteen items into one String? Why not write them individually, with a newLine after each?
yoy are creating a new file(overwriting any old stuff) every time you run your code.Use RandomAccesFile insted or first read the "old" lines first and append the new line and write the whole stuff.
If you merge 14 items into a single line, and then print the line, then guess what, you get a single line.
If you have a bunch of playing cards in a mess on a table, and then stack them up into a single stack, then you end up with one stack. I don't understand why you would be surprised by this.
in everytime the specified buttons are pressed the parameters suppose to be written in the file then i will use the parameters to draw shapes, each line of the string which is the combination of 14 parameters which represent the specifications of a shape: cordinates color... etc.. i am trying to make a basic save and load controls for my basic java paint program, so does anybody have any different ideas about storing those data?
Create a class that represents those 14 parameters.
Create load() and save() methods on that class, that parse and format those parameters in a comprehensible way. (Hint: don't just shove them into one non-delimited string. Another hint: one easy way might be to use XML or a java.util.Properties object.)
thanks i'll, by the way i just found another way, using StringTokenizer :) then it would not be a problem if all the referances are in the same line ;)
How is a StringTokenizer supposed to help. You get 14 parameters, merge them together into a single line and then use a StringTokenizer to split them apart again. Seems a bloody waste to me.
well thats what i could do at the moment :)
Huh?If you want them as separate items, why bother merging them together?
then how could i code the program so that it should know the difference between a parameter string and a whole referance string when it is read :o from the written line?
What do you mean by a "parameter string" versus a "reference string"?And from that matter "the written line"? Do you mean the command line?Message was edited by: paulcw
sString[0] ... sString[n];
allStrings = sString[0] + ":" + ... + ":" + sString[n];
parameter Strings are the sString[]
and my referance string : allStrings
there are more than 1 allStrings which were written in the file
parameter Strings change according to the options..
fair enough?
You're still being vague.
But it doesn't make much difference. If you have a bunch of separate Strings that belong together, then put them into a class that encapsulates their relationship, as I've already told you. Don't build one big "allStrings" out of it and then break it down into individual Strings again later.
Furthermore, if some of those Strings actually represent non-String types (like having "1.234" when you really mean the number 1.234) then the class should hold the non-String types (such as a double for 1.234).
Then pass instances of that class around. Don't pass around that uber-string.
! PLEASE READ THIS POST !
I think that your problem is elsewhere then you might think.
You are creating a new file(overwriting any old stuff) every time you run your code.
Use RandomAccesFile insted or first read the "old" lines first and append the new line and write the whole stuff.
> ! PLEASE READ THIS POST !
>
> I think that your problem is elsewhere then you might
> think.
>
> You are creating a new file(overwriting any old
> stuff) every time you run your code.
> Use RandomAccesFile insted or first read the "old"
> lines first and append the new line and write the
> whole stuff.
I don't get that sense from this thread, but if the OP wants to
*append* to the end of a file (a common request with log
files, for example), then neither using a RandomAccessFile nor
reading in the previous lines only to write them out is the best way
to go. Instead, open the file in append mode, and take it from there:
FileOutputStream out = new FileOutputStream(file, true /* append */);
//or
FileWriter out = new FileWriter(file, true /* append */);
//etc...