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: Copy Method screw ups

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Copy Method screw ups

    Okay my war right now before I end up ripping my hair out is my copy method. I see the error in Netbeans(the IDE I am using), the problem is in my copy method, and I just don't see where I am going wrong with this. I declare my

    FileInputStream from = null;
    FileOutputStream to = null;

    Now when I use,
    from = new FileInputStream(textField); //Stream from file
    to = new FileOutputStream(textField1); //Stream to copy file

    It's an obvious error, why is this? I'm not looking for the exact answer on fixing it. I am looking on guidance.

    package synch;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
     
    public class Synch extends JPanel {
     
     
        private JButton setSource = new JButton("Set Source");
        private JButton setTarget = new JButton("Set Target");
        private JButton copyButton = new JButton("Copy!");
        private JTextField textField = new JTextField(" ");
        private JTextField textField1 = new JTextField(" ");
        private JFileChooser fc = new JFileChooser();
     
        private JMenuItem Exit, oneWaySynch, twoWaySynch, Help, About, HelpMe;
     
     
        public Synch(){
            JFrame prog = new JFrame("Chris's file synchronization");
            prog.setSize(500,500);
            prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
     
            JMenuBar jmb = new JMenuBar();
            prog.setJMenuBar(jmb);
            JMenu fileMenu = new JMenu("File");
            jmb.add(fileMenu);
            fileMenu.add(oneWaySynch = new JMenuItem(" One way Synch "));
            fileMenu.add(twoWaySynch = new JMenuItem(" Two way Synch "));
            fileMenu.add(Exit = new JMenuItem("Exit"));
     
            JMenu helpMenu = new JMenu("Help");
            jmb.add(helpMenu);
            helpMenu.add(HelpMe = new JMenuItem("Help"));
            helpMenu.add(About = new JMenuItem("About"));      
     
     
            prog.setLayout(new BorderLayout());    
     
            JPanel outterPanel = new JPanel();              
            outterPanel.setLayout(new GridLayout(2,1));
     
            //Set up the top panel of the outer panel
            JPanel topPannel = new JPanel();      
            topPannel.add(textField);
            textField.setPreferredSize(new Dimension(300,30));
            topPannel.add(setSource);
     
            //Set up the bottom panel of the outer panel
            JPanel bottomPanel = new JPanel(); 
            bottomPanel.add(textField1);
            textField1.setPreferredSize(new Dimension(300,30));
            bottomPanel.add(setTarget);
     
            //Add the top and bottom panels to the outer panel
            outterPanel.add(topPannel);
            outterPanel.add(bottomPanel);
     
            prog.add(outterPanel, BorderLayout.NORTH);
     
            JPanel Pane3 = new JPanel();
            fc.setControlButtonsAreShown(false);
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            Pane3.add(fc, FlowLayout.LEFT);              
            prog.add(Pane3, BorderLayout.CENTER);
     
     
     
            JPanel Pane4 = new JPanel();
            Pane4.add(copyButton, FlowLayout.LEFT);
            prog.add(Pane4, BorderLayout.SOUTH);
     
     
            Exit.addActionListener(new ActionListener(){
               public void actionPerformed (ActionEvent e){
                   exitMethod();
               }
            });
     
             oneWaySynch.addActionListener(new ActionListener(){
               public void actionPerformed (ActionEvent e){
                   oneWayMethod();
               }
            });     
     
            twoWaySynch.addActionListener(new ActionListener(){
               public void actionPerformed (ActionEvent e){
                   twoWayMethod();
               }
            });
     
             HelpMe.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   helpMethod();
                }
                });
     
             About.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   aboutMethod();           
                }
                });         
     
             setSource.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   setSourceMethod();           
                }
                });
     
             setTarget.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   setTargetMethod();           
                }
                });
     
     
             copyButton.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   copyMethod();           
                }
                });  
     
            prog.setVisible(true); //Display the window to the user!      
     
        } //end of method Synch()
     
            public void exitMethod() {
                if(JOptionPane.showConfirmDialog(null,"Are you sure you want it to close?", "Confirm Exit", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
                    System.exit(0);  
            }
     
     
            public void oneWayMethod() {
                //Read the next file in the source directory
     
                //See if it exists in the target directory
     
                //  if it does, check to see if it is EXACTLY the same file
     
                //  if not copy the file
                JOptionPane.showMessageDialog(null, "One way sync has been activated.");
     
     
            }
            public void twoWayMethod() {
                JOptionPane.showMessageDialog(null, "Two way sync has been activated.");
     
            }
     
            public void aboutMethod() {
                JOptionPane.showMessageDialog(null, "This is a file synchronization program, to speed things up, and over all make it easier for you.");
                   }
     
            public void helpMethod() {
                JOptionPane.showMessageDialog(null, "If you are needing assistance please contact me via email: egamespaypal@gmail.com or by phone 1-901-619-9413.");
     
     
            }
     
            public void setSourceMethod() {
                //read the directory that the file chooser is currently sitting on
                 textField.setText(fc.getCurrentDirectory().toString());
                JOptionPane.showMessageDialog(null, "The source has been locked in!");
     
     
            }
     
     
            public void setTargetMethod() {
                //read the directory that the file chooser is currently sitting on
                textField1.setText(fc.getCurrentDirectory().toString());
     
                JOptionPane.showMessageDialog(null, "The target location has been locked in!");
     
     
            }
     
            public void copyMethod() {
                JOptionPane.showMessageDialog(null, "Copying has commenced, watch the magic happen!");
     
                FileInputStream from = null;
                FileOutputStream to  = null;
     
    //Size of buffer 
                int buffSize = 4000; 
    //Start reading
                try {    
     
                from = new FileInputStream(textField);         //Stream from file 
                to   = new FileOutputStream(textField1);      //Stream to copy file
                byte[] charBuffer = new byte[buffSize];
     
                int numRead;    //to keep track of where you are in the file
    //Loop through the file taking in buffSize bytes and copying them to _toF.
                while((numRead = from.read(charBuffer)) != -1 )     // -1 so we don't get out of bounds
                to.write(charBuffer, 0, numRead);      //This will copy our code in chunks
    } finally {
    //close all our streams
    }
     
     
            }
     
    }


    my main

    package synch;
     
     
    import javax.swing.JFrame;
    import java.awt.*;
     
    public class Main {
     
     
     
     
    public static void main(String[] args) {        
     
            try{
     
                Synch myProg = new Synch();
     
     
            }
     
            catch(Exception Ex){
     
     
        }
     
     
     
        }
     
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Copy Method screw ups

    Please show me where in the Java API the FileInputStream class has a constructor that takes a JTextField as a parameter.

    In future if code has compilation errors, copy and paste the EXACT error message.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Copy Method screw ups

    Hey Junky,

    I am a novice to Java programming, and working my way around it. So it's probably something simple that I am just not understanding.

    The error at that line is as followed

    "cannot find symbol
    symbol : constructor FileInputStream(javax.swing.JTextField)
    location: class java.io.FileInputStream"

    On both lines. I implemented import java.io.*; at the top, so shouldn't that get rid of it? This is my first time ever working with import.java.io.*; functionality so making this copy method is completely new stuff to me.

    If you can throw me some much needed knowledge, it'll be helpful!

    Thank you.

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Copy Method screw ups

    Okay I did some looking up, and made it....

    from = new FileInputStream(textField.toString()); //Stream from file
    to = new FileOutputStream(textField1.toString()); //Stream to copy file

    And I thought I nailed my error on the head, then I wind up with,

    unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown

    What's going on here?

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Copy Method screw ups

    Just as the error message states, you are not handling the exception. Whenever you do file IO there is a possiblility that the file you are trying to access does not exist, therefore a FileNotFoundException will be thrown. You need to use a try/catch statement to handle this possibility.

  6. The Following User Says Thank You to Junky For This Useful Post:

    chrisivey1980 (May 18th, 2011)

  7. #6
    Junior Member
    Join Date
    Apr 2011
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Copy Method screw ups

    Thank you so much!!

    Well that took out my errors, and so on. But now my file copy method just does not work. Can someone please tell me what I am doing wrong?

    package synch;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
     
    public class Synch extends JPanel {
     
     
        private JButton setSource = new JButton("Set Source");
        private JButton setTarget = new JButton("Set Target");
        private JButton copyButton = new JButton("Copy!");
        private JTextField textField = new JTextField(" ");
        private JTextField textField1 = new JTextField(" ");
        private JFileChooser fc = new JFileChooser();
     
        private JMenuItem Exit, oneWaySynch, twoWaySynch, Help, About, HelpMe;
     
     
        public Synch(){
            JFrame prog = new JFrame("Chris's file synchronization");
            prog.setSize(500,500);
            prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
     
            JMenuBar jmb = new JMenuBar();
            prog.setJMenuBar(jmb);
            JMenu fileMenu = new JMenu("File");
            jmb.add(fileMenu);
            fileMenu.add(oneWaySynch = new JMenuItem(" One way Synch "));
            fileMenu.add(twoWaySynch = new JMenuItem(" Two way Synch "));
            fileMenu.add(Exit = new JMenuItem("Exit"));
     
            JMenu helpMenu = new JMenu("Help");
            jmb.add(helpMenu);
            helpMenu.add(HelpMe = new JMenuItem("Help"));
            helpMenu.add(About = new JMenuItem("About"));      
     
     
            prog.setLayout(new BorderLayout());    
     
            JPanel outterPanel = new JPanel();              
            outterPanel.setLayout(new GridLayout(2,1));
     
            //Set up the top panel of the outer panel
            JPanel topPannel = new JPanel();      
            topPannel.add(textField);
            textField.setPreferredSize(new Dimension(300,30));
            topPannel.add(setSource);
     
            //Set up the bottom panel of the outer panel
            JPanel bottomPanel = new JPanel(); 
            bottomPanel.add(textField1);
            textField1.setPreferredSize(new Dimension(300,30));
            bottomPanel.add(setTarget);
     
            //Add the top and bottom panels to the outer panel
            outterPanel.add(topPannel);
            outterPanel.add(bottomPanel);
     
            prog.add(outterPanel, BorderLayout.NORTH);
     
            JPanel Pane3 = new JPanel();
            fc.setControlButtonsAreShown(false);
            fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            Pane3.add(fc, FlowLayout.LEFT);              
            prog.add(Pane3, BorderLayout.CENTER);
     
     
     
            JPanel Pane4 = new JPanel();
            Pane4.add(copyButton, FlowLayout.LEFT);
            prog.add(Pane4, BorderLayout.SOUTH);
     
     
            Exit.addActionListener(new ActionListener(){
               public void actionPerformed (ActionEvent e){
                   exitMethod();
               }
            });
     
             oneWaySynch.addActionListener(new ActionListener(){
               public void actionPerformed (ActionEvent e){
                   oneWayMethod();
               }
            });     
     
            twoWaySynch.addActionListener(new ActionListener(){
               public void actionPerformed (ActionEvent e){
                   twoWayMethod();
               }
            });
     
             HelpMe.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   helpMethod();
                }
                });
     
             About.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   aboutMethod();           
                }
                });         
     
             setSource.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   setSourceMethod();           
                }
                });
     
             setTarget.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   setTargetMethod();           
                }
                });
     
     
             copyButton.addActionListener(new ActionListener(){
                public void actionPerformed (ActionEvent e){
                   copyMethod();           
                }
                });  
     
            prog.setVisible(true); //Display the window to the user!      
     
        } //end of method Synch()
     
            public void exitMethod() {
                if(JOptionPane.showConfirmDialog(null,"Are you sure you want it to close?", "Confirm Exit", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
                    System.exit(0);  
            }
     
     
            public void oneWayMethod() {
                //Read the next file in the source directory
     
                //See if it exists in the target directory
     
                //  if it does, check to see if it is EXACTLY the same file
     
                //  if not copy the file
                JOptionPane.showMessageDialog(null, "One way sync has been activated.");
     
     
            }
            public void twoWayMethod() {
                JOptionPane.showMessageDialog(null, "Two way sync has been activated.");
     
            }
     
            public void aboutMethod() {
                JOptionPane.showMessageDialog(null, "This is a file synchronization program, to speed things up, and over all make it easier for you.");
                   }
     
            public void helpMethod() {
                JOptionPane.showMessageDialog(null, "If you are needing assistance please contact me ");
     
     
            }
     
            public void setSourceMethod() {
                //read the directory that the file chooser is currently sitting on
                 textField.setText(fc.getSelectedFile().toString());
                JOptionPane.showMessageDialog(null, "The source has been locked in!");
     
     
            }
     
     
            public void setTargetMethod() {
                //read the directory that the file chooser is currently sitting on
                textField1.setText(fc.getSelectedFile().toString());
     
                JOptionPane.showMessageDialog(null, "The target location has been locked in!");
     
     
            }
     
            public void copyMethod() {
       //         JOptionPane.showMessageDialog(null, "Copying has commenced, watch the magic happen!");
     
                FileInputStream from = null;
                FileOutputStream to  = null;
     
    //Size of buffer (too much will crash)
                int buffSize = 4000; 
    //Start 
                try {    
     
     
                from = new FileInputStream(textField.toString());         //Stream from file 
                to   = new FileOutputStream(textField1.toString());      //Stream to copy file
                byte[] charBuffer = new byte[buffSize];
     
                int numRead;    //to keep track of where you are in the file
               //Loop through the file taking in buffSize bytes and copying them to textField1
                while((numRead = from.read(charBuffer)) != -1 )     // -1 so no out of bounds
                to.write(charBuffer, 0, numRead);      //This will copy code
    } 
     
                catch(Exception Ex){
            }
    }
    }

  8. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Copy Method screw ups

    First off, that's way too much code for this problem- I had to read all of it, when really, the entire problem is in your copyMethod(). You'd have better luck in the future if you boil your problem down to an SSCCE before posting code.

    Anyway, the problem is that you're catching an Exception, but then not doing anything. A simple Ex.printStackTrace() in your catch block would help you figure out what's going wrong.
    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. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,259
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Copy Method screw ups

    Try debugging your code by adding some println()s to the method you are having problems with to show how the variables' values are changing. For example print out what is being read in: the chars and the number read.

  10. #9
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Copy Method screw ups

    The FileInputStream class has three constructors:

    FileInputStream(File file);
    FileInputStream(FileDescriptor fdObj);
    FileInputStream(String name);

    You're using the third one, correctly providing an instance of String as the argument. However, you're trying to obtain the filenames via JTextField's toString() method. You should use getText(), like so:

    FileInputStream sourcef = new FileInputStream(textField.getText());

    Whenever you have doubts about the functioning of a class, you should consult the official API. You would've found your solution by verifying the following links:

    FileInputStream (Java 2 Platform SE v1.4.2)
    JTextField (Java 2 Platform SE v1.4.2)

Similar Threads

  1. How to copy J2ME application to a mobile device
    By vishal21 in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: January 21st, 2012, 05:52 PM
  2. Deep copy of ArrayList?
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2011, 02:30 PM
  3. Copy Constructor
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2011, 11:43 PM
  4. how to copy cells to a new sheet of excel with JexcelAPI??
    By 19world in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: June 15th, 2010, 04:49 AM
  5. program a button that copy whatever in a JTextArea
    By voltaire in forum AWT / Java Swing
    Replies: 5
    Last Post: May 17th, 2010, 01:43 AM