• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

saving a file on local PC

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I save a string in a text file ?
My way :
.
.
.
BufferedWriter file;
String s = new String("Test");
file = new BufferedWriter(new FileWriter("textfile.txt"));
file.write(s);
.
.
.
does'nt work !
(I wrote this sourcecode without the book, where I read that. It is possible that there are some mistakes.)
I wrote that as an applet, but the file was not created.
HELP PLEASE !!
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bonkers,
The problem is probably that you wrote your code as an applet. Applets have security restrictions imposed on them such that they cannot write files on the local machine. If you try the code as an application, it will work. See example below:
import java.io.*;
public class Test
{
public static void main(String args[])
{
String s = new String("The string to write to file.");
try
{
FileWriter fw = new FileWriter("textfile.txt");
fw.write(s);
fw.close();
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
}
}
Hope this helps.
Stephanie
 
Kay Tracid
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help ! I know it works as an application, but I need it as an applet. Is there no way ? May be it works with Java Script or Html ?
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bonkers,
I really don't think there is any way to make an unsigned applet write to local disk. I believe you have two options:
1.) Write a signed applet that will deal with the security manager to get explicit permission from the applet user to write to their local system. (I am not sure whether this will work in all cases).
2.) Write a simple server that writes to its own file system. Let the applet make read and write file requests to this server. (I know this one works because I've done it).
Hope this helps.
Stephanie
 
Kay Tracid
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! This will help me.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic