Tech Support Guy banner

Solved: JAVA: Issues in IO

1317 Views 5 Replies 2 Participants Last post by  sepala
I have created a small program in JAVA but it shows some errors in "reNameTo()" method. Here is my code

Code:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;

public class FileGUI2 extends JFrame implements ActionListener{

    /*Create the File Section*/

    JLabel lName;

    JTextField tName;

    JButton button1, button2;

    JTabbedPane main;
    
    File f1;


    /*Rename the File Section*/

    JLabel lReName;

    JTextField tReName;

    JButton button3, button4;

    File f2;

    public FileGUI2()
    {
        /*Declaring the Create the File Section*/

        lName = new JLabel("Name");
        tName = new JTextField(10);
        button1 = new JButton("Create");
        button1.addActionListener(this);
        button2 = new JButton("Cancel");
        button2.addActionListener(this);

        main = new JTabbedPane();

        JPanel panel1 = new JPanel();
        panel1.setLayout(new FlowLayout());
        panel1.add(lName);
        panel1.add(tName);
        panel1.add(button1);
        panel1.add(button2);

        /*Declaring the Rename the File Section*/

        lReName = new JLabel("Rename");
        tReName = new JTextField(10);
        button3 = new JButton("ReName");
        button4 = new JButton("Cancel");

        JPanel panel2 = new JPanel();
        panel2.setLayout(new FlowLayout());
        panel2.add(lReName);
        panel2.add(tReName);
        panel2.add(button3);
        panel2.add(button4);

        /*Setting up the main window*/

        main.addTab("Create the File",panel1);
        main.addTab("ReName the File",panel2);

        JPanel mPanel = (JPanel)getContentPane();
        mPanel.setLayout(new BorderLayout());
        mPanel.add(main);
    }

    public void actionPerformed(ActionEvent ae)
    {
          String text = tName.getText();

        if(ae.getSource().equals(button1))
        {
            String dirName="C:/javay/Test";

            try
            {
                 f1 = new File(dirName,text);
                f1.createNewFile();
            }
            catch(IOException io)
            {
                JOptionPane.showMessageDialog(null,"Invalid");
            }
            JOptionPane.showMessageDialog(null,"File Created in "+dirName);
        }

        else if(ae.getSource().equals(button2))
        {
            System.exit(0);
        }

    }

    class ReName implements ActionListener{

           String ReName = tReName.getText();

        public void actionPerformed(ActionEvent ae)
        {
             String dirName="C:/javay/Test";

             if(ae.getSource().equals(button3))
             {


             try
             {
                 f2 = new File(dirName,ReName);

                 f1.reNameTo(f2);
             }
             catch(IOException io)
             {

             }
            }

             else if(ae.getSource().equals(button4))
             {
                 System.exit(0);
             }
        }


    }

    public static void main(String[]args)
    {
        FileGUI2 fg = new FileGUI2();
        fg.setVisible(true);
        fg.setSize(600,200);
        fg.validate();
    }
}
There is another problem here, that is, in the class ReName,I have to give "Exception" in try catch blog rather than giving "IOException". If I give IOException it shows this error "Exception java.io.IOException is never thrown in body of corresponding try statement". This error appears sometimes, sometimes won't. I am so confused. Please help.

PS: I am using netBeans
See less See more
Status
Not open for further replies.
1 - 6 of 6 Posts
I don't see a method called reNameTo anywhere...
I don't see a method called reNameTo anywhere...
Hi andy. That method is here

try
{
f2 = new File(dirName,ReName);

f1.reNameTo(f2);
}

This is inside the ReName class
Hi andy,

I managed to solve that problem by my self. Here what I have done

Code:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;

public class FileGUI3 extends JFrame implements ActionListener{

    /*Create the File Section*/

    JLabel lName;

    JTextField tName;

    JButton button1, button2;

    JTabbedPane main;

    File f1;


    /*Rename the File Section*/

    JLabel lReName;

    JTextField tReName;

    JButton button3, button4;



 


    /*Clasess references*/
    ReNameClass rn = new ReNameClass();
    


    public FileGUI3 ()
    {
        
        /*Declaring the Create the File Section*/

        lName = new JLabel("Name");
        tName = new JTextField(10);
        button1 = new JButton("Create");
        button1.addActionListener(this);
        button2 = new JButton("Cancel");
        button2.addActionListener(this);

        main = new JTabbedPane();

        JPanel panel1 = new JPanel();
        panel1.setLayout(new FlowLayout());
        panel1.add(lName);
        panel1.add(tName);
        panel1.add(button1);
        panel1.add(button2);

        /*Declaring the Rename the File Section*/

        lReName = new JLabel("Rename");
        tReName = new JTextField(10);
        button3 = new JButton("ReName");
        button3.addActionListener(rn);
        button4 = new JButton("Cancel");
        button4.addActionListener(rn);



        JPanel panel2 = new JPanel();
        panel2.setLayout(new FlowLayout());
        panel2.add(lReName);
        panel2.add(tReName);
        panel2.add(button3);
        panel2.add(button4);

        /*Setting up the main window*/

        main.addTab("Create the File",panel1);
        main.addTab("ReName the File",panel2);

        JPanel mPanel = (JPanel)getContentPane();
        mPanel.setLayout(new BorderLayout());
        mPanel.add(main);
    }

    public void actionPerformed(ActionEvent ae)
    {
          String text = tName.getText();

        if(ae.getSource().equals(button1))
        {
            String dirName="C:/javay/Test";

            try
            {
                 f1 = new File(dirName,text);
                f1.createNewFile();
            }
            catch(IOException io)
            {
                JOptionPane.showMessageDialog(null,"Invalid");
            }
            JOptionPane.showMessageDialog(null,"File Created in "+dirName);
        }

        else if(ae.getSource().equals(button2))
        {
            System.exit(0);
        }

    }

    class ReNameClass implements ActionListener{

          

        public void actionPerformed(ActionEvent ae)
        {
               String ReName = tReName.getText();
             String dirNames="C:/javay/Test";

             if(ae.getSource().equals(button3))
             {

             try
             {
                   File  f2 = new File(dirNames,ReName);

                 f1.renameTo(f2);
             }
             catch(Exception io)
             {
                JOptionPane.showMessageDialog(null,"Invalid");
             }
             JOptionPane.showMessageDialog(null,"Your file has been renamed to: "+ReName);
            }

             else if(ae.getSource().equals(button4))
             {
                 System.exit(0);
             }
        }


    }

    public static void main(String[]args) 
    {
        FileGUI3 fg = new FileGUI3();
        fg.setVisible(true);
        fg.setSize(600,200);
        fg.validate();
    }
}
Thanks for trying to help. Much appreciated. Thanks again...
See less See more
I must be blind! Completely didn't see that :)
I must be blind! Completely didn't see that :)
hahaha....No worries andy. You have helped me a lot in lots of Java issues.:):up:
1 - 6 of 6 Posts
Status
Not open for further replies.
Top