Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save string to file 1

Status
Not open for further replies.

yama

Programmer
Jun 28, 2001
69
SG
Hi, does anyone know how to save a string to any file type, e.g: .html, .jpg, .txt?
i have a problem with this --> I uploaded a picture from my html page, then i write all of this to a string, then i like to save the string as a *.jpg or *.gif, any ideas?
thanks
 
Create a new File, open a FileOutputStream to the File, pass the String to the FileOutputStream as an array of bytes. Simple Example:
Code:
String filename;
/* Set your fully qualified filename here */

String data = null;
/* Set your file contents into String here */

File file = new File(filename);
/* Create file, delete old file if necessary */
try {
  if (file.exists()) {
    file.delete();
  }
  file.createNewFile();
}
catch (IOException ioe) {
  /* Your error handling goes here */
}

/* Open FileOutputStream */
FileOutputStream fileOut = new FileOutputStream(file);

/* Output contents of file */
try {
  fileOut.write(data.getBytes());
}
catch (IOException ioe) {
  /* Your error handling goes here */
}
finally {
  fileOut.close();
}

I didn't test this but I see no reason why it wouldn't work. You will have in import java.io.* in your jsp page to use this code. Wushutwist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top