Results 1 to 8 of 8
  1. #1
    chetu7845 is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default How to save a file in directory...???PLz find below code anything wrong in mycode

    Here my code am trying to open text file in window_pane it will open when i click on save it will ask destination to save file if i save example like Read.txt it will save...when i go and check in that folder Read.txt file won't found...what is the error please help me..???

    Java Code:
    import javax.swing.*;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.filechooser.FileNameExtensionFilter;
     
    import java.io.*;
    import java.net.MalformedURLException;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Read extends JFrame 
    {
    	private JTextField filename = new JTextField(),dir = new JTextField();
    	 private JButton open = new JButton("Open"), save = new JButton("Save");
    	public JPanel  window_panel;//address_panel,
    	public JLabel address_label;
    	public JTextField address_tf;
    	public JEditorPane window_pane,tree_pane,attr_pane;
    	public JScrollPane window_scroll,tree_scroll,attr_scroll;
    	TextArea t1,t2;
    	JPanel pane;
    	public JButton address_b, browse;
    	public JLabel l,m;
    	//private Go go = new Go();
    	JFrame f ;
    	public Read() throws IOException
    	{
     
    	f= new JFrame("Web browser");
    	f.setSize(1000,700);
     
    	pane=new JPanel();
    	pane.setVisible(true);
    	pane.setLayout(null);
    	f.setContentPane(pane);
     
     
    	address_label = new JLabel(" Path: ", SwingConstants.CENTER);
    	address_label.setBounds(10, 10, 70, 30);
    	pane.add(address_label);
     
    	address_tf = new JTextField("",25);
    	address_tf.setBounds(80,10,250,30);
    	pane.add(address_tf);
     
    	browse = new JButton("Browse");
    	browse.setBounds(340, 10, 140, 30);
    	browse.addActionListener(new Open());
    	pane.add(browse);
     
    	save = new JButton("Save");
    	save.setBounds(500, 10, 150, 30);
    	save.addActionListener(new Save());
    	pane.add(save);
     
    	window_pane=new JEditorPane();
    	window_pane.setBounds(10, 50, 970, 600);
    	pane.add(window_pane);
     
    	f.setVisible(true);
    	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
    }
     class Open implements ActionListener
    {
    		public void actionPerformed(ActionEvent ae)
    		{
    			JFileChooser fc = new JFileChooser();
     
    			int result = fc.showOpenDialog(null);
    			if (result == JFileChooser.APPROVE_OPTION)
    			{
    				File file = fc.getSelectedFile();
    				String sname = file.getAbsolutePath(); 
    				address_tf.setText(sname);
    				String ext=getFileExtension(sname);
     
     
    				try 
    				{
     
    						window_pane.setPage(file.toURI().toURL());
    						System.out.println("hi");
     
    				} 
    				catch (MalformedURLException e) 
    				{  
    					e.printStackTrace();
     
    				}
    				catch (IOException e)
    				{  
    					e.printStackTrace();
     
    				}
    			}
    		}
    }
    class Save implements ActionListener {
    			    public void actionPerformed(ActionEvent e) {
    			      JFileChooser c = new JFileChooser();
    			      // Demonstrate "Save" dialog:
    			      int rVal = c.showSaveDialog(Read.this);
    			      if (rVal == JFileChooser.APPROVE_OPTION) {
    			    	  filename.setText(c.getSelectedFile().getName());
    			        dir.setText(c.getCurrentDirectory().toString());
    			      }
     
    			    }
    			  }
     
       public String getFileExtension(String filename)
        {
    	if (filename == null)
    	{
                return null;
    	}
    	int lastUnixPos = filename.lastIndexOf('/');
    	int lastWindowsPos = filename.lastIndexOf('\\');
    	int indexOfLastSeparator = Math.max(lastUnixPos, lastWindowsPos);
    	int extensionPos = filename.lastIndexOf('.');
    	int lastSeparator = indexOfLastSeparator;
    	int indexOfExtension = lastSeparator > extensionPos ? -1 : extensionPos;
    	int index = indexOfExtension;
    	if (index == -1) 
    	{
    		return "";
    	} 
    	else
    	{
    		return filename.substring(index + 1);
    	}
    }
     
    public static void main(String args[]) throws IOException
    {
      Read wb = new Read();
     
    }
    }

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How to save a file in directory...???PLz find below code anything wrong in mycode

    Hi,

    Your code only shows the dialog and then sets the text of two controls. If you want to create a file with content then you have to write code for that, too.

  3. #3
    chetu7845 is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Re: How to save a file in directory...???PLz find below code anything wrong in mycode

    Quote Originally Posted by kneitzel View Post
    Hi,

    Your code only shows the dialog and then sets the text of two controls. If you want to create a file with content then you have to write code for that, too.
    Hi am new for java...can you please help me

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How to save a file in directory...???PLz find below code anything wrong in mycode

    You can find a simple example at Saving Text from an Editor Pane

    But I would suggest to read about streams so you understand the basics behind that.

  5. #5
    chetu7845 is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Re: How to save a file in directory...???PLz find below code anything wrong in mycode

    Hi the above link code is not work please help me...

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: How to save a file in directory...???PLz find below code anything wrong in mycode

    Nothing better than the source.

    Google: "java basic io"
    Result:

    Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)

    More specifically because there are lots if links in there and you are not the type to take the time and read through that:

    Character Streams (The Java™ Tutorials > Essential Classes > Basic I/O)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How to save a file in directory...???PLz find below code anything wrong in mycode

    Hi,
    The link is working fine for me. But it simply shows some code that:
    - opens a stream to a file
    - writes the result of get text from the JEditorPane to it.
    But Google gave much more hits on Save JEditorPane to file - maybe you want to do such a search, too?

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

    Default Re: How to save a file in directory...???PLz find below code anything wrong in mycode

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. how to find files in given directory
    By cecily in forum New To Java
    Replies: 7
    Last Post: 07-04-2014, 01:03 PM
  2. Need help with java save directory
    By anuraagkb in forum New To Java
    Replies: 1
    Last Post: 01-29-2012, 05:11 PM
  3. Replies: 1
    Last Post: 09-28-2011, 08:30 PM
  4. Wrong current working directory
    By tux008 in forum Advanced Java
    Replies: 10
    Last Post: 07-12-2011, 09:22 AM
  5. Cant find myself in my companies ldap directory
    By richierich in forum Advanced Java
    Replies: 1
    Last Post: 04-05-2011, 06:03 AM

Tags for this Thread

Posting Permissions

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