• Post Reply Bookmark Topic Watch Topic
  • New Topic

Saving a File

 
Nikki Parker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
How would i write code to save a file. Example, i want the code to be able to take the data just manipulated and save it to a file.
Ideally i want the screen to have a popup menu that can let you type in a file name and hit save...this will save your file to your destination. How would i code this saving process.
thank you in advance, Nikki
 
Omar IRAQI
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer the first part of the question :
assume that you have your data stored in a string aString, if it is not, you can always find a way to map it into a string representation.
Now you can do :
import java.io.*;
try{
RandomAccessFile rafOut = new RandomAccessFile("whateverPath" + File.separator + "whateverFileName", "rw");
rafOut.writeBytes(aString);
rafOut.close();
}catch(IOException ex){
ex.printStackTrace();
}
or
FileOutputStream fileOut = new FileOutputStream("whateverPath etc...");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fileOut));
writer.write(aString, 0, aString.length());
writer.flush();
writer.close();
fileOut.close();
}catch(IOException etc){
}
Now about the second question : see javax.swing.JFileChooser
good luck
[This message has been edited by Omar IRAQI (edited July 18, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!