Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Reading and writing files in a JApplet

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading and writing files in a JApplet

    I want to create a file manager for a web server that will run in the browser. I started by making a simple interface to create a directory in the folder that the java class is run.

    When run, the applet creates the folder fine but when I upload it and run it on a web server it doesn't create the folder.

    Here is my directory creating code:

    File f = new File(System.getProperty("user.dir")+"\\"+folderName+"\\");
    f.mkdir();

    I have had a look at using URL instead of files but it kept throwing exceptions and I would have no idea how to do it that way. Any help would be very much appreciated.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Reading and writing files in a JApplet

    Never, ever, ever use string concatenation on file paths.

    Create a File object that represents the user dir, then use the File(File, String) constructor to create a File object that represents a directory in the user dir, then use mkdir.

    "\\" is a literal '\' in Java. A 'path separator' is a platform defined entity that may not even exist on all Java platforms. Try to break the habit of using them explicitly.

    Just to check that your code is doing what you want it to do, print the return value of File.getAbsolutePath() for the two File objects (user dir and new sub dir) to make sure they're what you expect.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading and writing files in a JApplet

    But if I want to create a directory them I cant specify a file because the directory won't even exist. This will then throw an exception saying the file does not exist.

    The rest of the code is hear to help spot what may need changing:

    package classes;
     
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
     
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
     
     
    @SuppressWarnings("serial")
    public class CreateDir extends JPanel
    implements ActionListener
    {
    	JTextField TextAreaCreateDir;
     
    	public CreateDir()
    	{
    		this.removeAll();
    		this.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
     
    		c.gridx = 0;
    		c.gridy = 0;
    		c.gridwidth = GridBagConstraints.REMAINDER;
    		c.gridheight = 1;
    		c.gridwidth = 1;
    		c.fill = GridBagConstraints.BOTH;
    		TextAreaCreateDir = new JTextField("");
    		add(new JLabel("Name: "), c);
     
    		c.gridx = 1;
    		c.gridy = 0;
    		c.gridwidth = GridBagConstraints.REMAINDER;
    		c.gridheight = 1;
    		c.gridwidth = 2;
    		c.fill = GridBagConstraints.BOTH;
    		TextAreaCreateDir = new JTextField("");
    		this.add(TextAreaCreateDir, c);
     
    		JButton ButtonCreateDir = new JButton("Create Directory");
    		ButtonCreateDir.setActionCommand(TextAreaCreateDir.getText());
    		ButtonCreateDir.setName("CreateDir");
    		ButtonCreateDir.addActionListener(this);
     
    		c.gridx = 1;
    		c.gridy = 1;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		c.gridwidth = 1;
    		c.fill = GridBagConstraints.NONE;
    		this.add(ButtonCreateDir,c);
    	}
     
    	public void CreateDirectory(String folderName)
    	{
     
    		if( folderName.replaceAll("[\\w-_]", "").equals(""))
    		{		
     
    			File f = new File(System.getProperty("user.dir")+"\\"+folderName+"\\");
    			f.mkdir();
     
    			if(f.isDirectory())
    				JOptionPane.showMessageDialog(this, "Directory Created");
    			else
    				JOptionPane.showMessageDialog(this, "Failed to Create Directory");
    		}
    		else
    			JOptionPane.showMessageDialog(this, "Only letters, numbers and _- allowed");
     
    	}
     
    	public void actionPerformed(ActionEvent e)
    	{
    		if( "CreateDir".equals(e.getActionCommand()));
    		{
    			CreateDirectory(TextAreaCreateDir.getText());
    		}
    	}
     
    }

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Reading and writing files in a JApplet

    Post your exceptions. You are running this as an applet in a user browser? Where do you expect the directory to be created - on the web server, or on the user's PC? If the former, you need something like a servlet. If the latter, then I suspect your applet would have to be signed to get access to the local filestore.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading and writing files in a JApplet

    Quote Originally Posted by Sean4u View Post
    Post your exceptions. You are running this as an applet in a user browser? Where do you expect the directory to be created - on the web server, or on the user's PC? If the former, you need something like a servlet. If the latter, then I suspect your applet would have to be signed to get access to the local filestore.
    I need the directory to be created on the web server. There are no exeptions thrown when I run.

    When I run the applet through eclipse, it will create the directory fine, but if I run the applet embedded in a browser on a local system it wont work. Surely they should both do the same if they are both run on the same computer locally?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Reading and writing files in a JApplet

    Like you've already been told- if you want to create files on the server, you're going to need to use something like a servlet. Applets don't run on the server's side, just the client side.

    If you want to create files on the client machine, you're going to need to sign the applet.

    What exactly do you mean by "it won't work"?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading and writing files in a JApplet

    Quote Originally Posted by KevinWorkman View Post
    Like you've already been told- if you want to create files on the server, you're going to need to use something like a servlet. Applets don't run on the server's side, just the client side.

    If you want to create files on the client machine, you're going to need to sign the applet.

    What exactly do you mean by "it won't work"?

    OK, does this mean that when the program is run through eclipse, it allows all permissions? But then when you run through the browser it is not allowed unless it is signed?

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Reading and writing files in a JApplet

    Quote Originally Posted by Majora94 View Post
    OK, does this mean that when the program is run through eclipse, it allows all permissions? But then when you run through the browser it is not allowed unless it is signed?

    Seems like a reasonable enough description to me. I would check out your Java Console, I would not be surprised if you found some errors there. What does the applet do when you run it in your browser? And are you sure you want to run this from an applet? From what you described, you should be using something like a servlet instead of an applet.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Reading and writing files in a JApplet

    You could use an Applet in a web browser for a file manager - sites like photobox.co.uk do that. If you search for "applet servlet communication" you'll find some sites that explain what needs to be done. It's reasonably straightforward, just convert your method calls to servlet requests, write those servlets and beef up your error handling a bit.

Similar Threads

  1. FileWrite Over-Writing txt Files. D:
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 8th, 2012, 04:31 PM
  2. Reading In Then Writing Out Massive Files
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: July 28th, 2011, 07:00 PM
  3. Writing to files
    By nitwit3 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 25th, 2011, 04:00 AM
  4. file reading & writing
    By macko in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 12th, 2011, 08:54 AM
  5. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:16 PM