- 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
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