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 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation 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..???
    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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Where in the posted code is there any code for writing text to a file?

    Also posted at: http://www.java-forums.org/new-java/...ng-mycode.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by Norm View Post
    Where in the posted code is there any code for writing text to a file?

    Also posted at: How to save a file in directory...???PLz find below code anything wrong in mycode
    ya i done save coding like dis...now it saving file to a directory but it calling 2 times save dialog...when second time call that time only it will save...what is the error ???
     public void actionPerformed(ActionEvent e) {
    			      JFileChooser sv = new JFileChooser();
    			      int result = sv.showSaveDialog(null);
    			      FileNameExtensionFilter filter=new FileNameExtensionFilter("Text/Java Files", "txt","java");
    			      sv.setFileFilter(filter);
    			      // Demonstrate "Save" dialog:
    			    try{
    			      if (result == JFileChooser.APPROVE_OPTION) 
    			      {
    			    	  File file = sv.getSelectedFile();
    						String sname = file.getAbsolutePath(); 
    						address_tf.setText(sname);
    			    	//  filename.setText(sv.getSelectedFile().getName());
    			        // dir.setText(sv.getCurrentDirectory().toString());
    			        PrintStream outputStream = WriteSimpleFile.selectTextFile();
    			        outputStream.println(window_pane.getText());
    			        outputStream.close();
     
    			      }
    			    }
    			    catch (Exception e1) {
    			    }
     
    				}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    but it calling 2 times
    The jvm will call the listener one time for each time the listener is added to a component. If the listener is added two times, the listener will be called 2 times.

    The catch block should have a call to printStackTrace() to show any errors.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: July 26th, 2013, 03:25 AM
  2. [SOLVED] PLZ HELP! Somethng wrong with code, assignment due today! Print if multiple of...
    By Rstuart970 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 16th, 2012, 02:35 AM
  3. what is wrong with this code???plz help me :(
    By kanikapant in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 17th, 2011, 06:03 PM
  4. What's wrong with my code for remembering JFileChooser's Last Directory?
    By esplanade56 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2011, 08:23 AM
  5. my code displaying some minor wrong o/p ....plz suggest me an answer !
    By naved in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 11:59 AM

Tags for this Thread