Results 1 to 12 of 12
  1. #1
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default Easiest Way to Write to File on a Server?

    I have a java applet on a web page and I would like to write to a file. What's the easiest way to do this? I've tried using java.io.PrintWriter, which worked on my pc but no file was created when I tried it on the web. Do I need to use a a different method for the web?

    Thanks

  2. #2
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Easiest Way to Write to File on a Server?

    Could you post your code in code tags. There are a couple of reasons its not working when deployed including the path and read/write permissions on the server.

  3. #3
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default Re: Easiest Way to Write to File on a Server?

    Quote Originally Posted by mwr1976 View Post
    Could you post your code in code tags. There are a couple of reasons its not working when deployed including the path and read/write permissions on the server.
    Here is the relevant part of the code.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    import acm.graphics.*;
    import acm.program.*;
    import acm.util.*;
    
    import java.applet.*;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class Road extends GraphicsProgram {
    
    final static String outFile = "out.txt";
    
    	public void run() {
    		writeToFile();
    	}
    
    	public void writeToFile() {
    		try {
    	   		wr = new PrintWriter(new FileWriter(outFile));
    	   	}
    	   	catch (IOException ex) {
    	   		System.out.println("Error: Error Writing to File");		
    	   	}
    	   
    	   	wr.println("testing");
    	   	
    	   	wr.close();	
    		}
    
    static PrintWriter wr;
    
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Easiest Way to Write to File on a Server?

    That code is not an applet. How are you executing it?

    If you are executing an html page with an applet, what error message are displayed in the browser's java console?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default Re: Easiest Way to Write to File on a Server?

    Quote Originally Posted by Norm View Post
    That code is not an applet. How are you executing it?

    If you are executing an html page with an applet, what error message are displayed in the browser's java console?
    I'm not getting any error messages, the rest of the program runs great. I am using this code in an html page to execute it:

    Java Code:
    <applet code=Code.class name=Code archive=acm.jar
    	width=700 height=750>
    	<param name="bgcolor" value="ffffff">
    	<param name="fontcolor" value="000000">
    	Your browser is not Java enabled.
    </applet>

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Easiest Way to Write to File on a Server?

    Normally code executes without error
    or you get an error message.

    Are you sure the code is executing? Add a println before and after the code you think is not working and see if both println statements print a message.

    put a call to printStackTrace in the catch block so you get the full text of any error message.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default Re: Easiest Way to Write to File on a Server?

    It is executing because it works on my desktop when I run it from eclipse. It just doesn't create the file out.txt when I run it from an html page on my desktop or when I run it on the server.

    Are Java Applets run in a browser allowed to write files to your hard drive/server?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Easiest Way to Write to File on a Server?

    Which of the two println statements executed and printed out when you executed the applet?

    Applets are not allowed to write on the PC's disk without permission.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default Re: Easiest Way to Write to File on a Server?

    Quote Originally Posted by Norm View Post
    Applets are not allowed to write on the PC's disk without permission.
    Are they allowed to write on the server side?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Easiest Way to Write to File on a Server?

    Applets do not execute on the server side. They can give something to the server and ask the server to write it.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default Re: Easiest Way to Write to File on a Server?

    How can I post a string to a server where it can be stored in a file?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Easiest Way to Write to File on a Server?

    An applet can connect to the server with a Socket or with a URLConnection and send a String to the server. When the server gets the String it can write that String to a file.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Easiest way to detect beeps in audio file
    By bill17 in forum New To Java
    Replies: 0
    Last Post: 11-26-2011, 05:46 PM
  2. Replies: 5
    Last Post: 07-06-2011, 05:24 AM
  3. applet can't write file in web server??
    By rajula in forum Java Applets
    Replies: 1
    Last Post: 05-02-2011, 03:48 PM
  4. Replies: 1
    Last Post: 04-23-2011, 07:11 AM
  5. Applet Server Write Permission
    By Look2Logic in forum New To Java
    Replies: 0
    Last Post: 04-13-2010, 10:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •