Java save file question

Pegun

Golden Member
Jan 18, 2004
1,334
0
71
Hey all. I'm trying to program a java program that can open a txt or.java program and display the coding. It then needs to save to a file but thats the problem that I'm running into, i can't get the coding to write to a file or display the dialog box for where to save the file. I've been through the API but the writeFile() method is not explained in full, from what i understand. I have the following:

private void doSaveAs() {
// display file selection dialog
FileDialog fDialog = new FileDialog(this, "Save As ...", FileDialog.SAVE);
fDialog.setVisible(true);
// Get the file name chosen by the user
String name = fDialog.getFile();
// If user canceled file selection, return without doing anything.
if(name == null)
return;
fileName = fDialog.getDirectory() + name;

// Try to create a file writer to write file
FileWriter writer=null;
try {
writer = new FileWriter(fileName);
} catch (FileNotFoundException ex) {
MessageDialog dialog = new MessageDialog(this, "Error Message",
"File Not Found: "+fileName);
dialog.setVisible(true);
return;
}
BufferedWriter bWriter = new BufferedWriter(writer);

// Try to read from the file one line at a time.
StringBuffer textBuffer = new StringBuffer();
try {
String textLine = bWriter.writeLine(); //******For Some reason this is where the problem is. writeLine() is not recognized, where read line in the open function was! *********//
while (textLine != null) {
textBuffer.append(textLine + '\n');
textLine = bWriter.writeLine(); //******Same Problem right here***//
}
bWriter.close();
writer.close();
} catch (IOException ioe) {
MessageDialog dialog = new MessageDialog(this, "Error Message",
"Error saving file: "+ioe.toString());
dialog.setVisible(true);
return;
}
setTitle("JavaEdit: " +name); // reset frame title
text.setText(textBuffer.toString());
}

Any help is greatly appreciated

EDIT: FIxed, Sorry i was trying to base it on the opening of a file and didnt change the comments
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
I'm confused... your comments say "Try to read from the file one line at a time" and the code is basically structured to read lines from a file into a StringBuffer but instead of usuaing a BufferedReader you're using a BufferedWriter. You're supposed to use readLine() from BufferedReader if you wasnt to read the contents line by line and append them to StringBuffer.

 

lozina

Lifer
Sep 10, 2001
11,711
8
81
If instead you wanted to take the contents of a StringBuffer and write them to a file, just do:

 

Pegun

Golden Member
Jan 18, 2004
1,334
0
71
Fixed the comments. Lozina, where exactly would that coding go, i tried putting it into :
StringBuffer textBuffer = new StringBuffer();
try {
String textLine = bWriter.writeLine();

But as you can see, i'm already writing to the string in the last lines of the code. Thanks for your help.
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
Ok so this code block you posted is supposed to only write to the file right, your block of code reading from the file is elsewhere? I ask because if you already read from this file and now have it's contents in your TextEditor (whatever you have with the variable "text") you don't have to read the file again right? Nor would you even want to, because then whatever changes the person makes of course would not be saved...

If so, then relaly all you need to do is replace all this:

BufferedWriter bWriter = new BufferedWriter(writer);

// Try to read from the file one line at a time.
StringBuffer textBuffer = new StringBuffer();
try {
String textLine = bWriter.writeLine(); //******For Some reason this is where the problem is. writeLine() is not recognized, where read line in the open function was! *********//
while (textLine != null) {
textBuffer.append(textLine + '\n');
textLine = bWriter.writeLine(); //******Same Problem right here***//
}
bWriter.close();
writer.close();

with: