Problem copy file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eyeofsoul
    New Member
    • Sep 2007
    • 31

    Problem copy file

    i am working in copying file to other destination.my code doesnt work as expected.can anybody help me.i got this error message --->
    java.io.FileNot FoundException: C:\Documents and Settings\XXXXXX X\My Documents (Access is denied)
    at java.io.FileOut putStream.open( Native Method)
    at java.io.FileOut putStream.<init >(FileOutputStr eam.java:179)
    at java.io.FileOut putStream.<init >(FileOutputStr eam.java:131)
    at test1.PilihFail $Salin2.run(Pil ihFail.java:111 )

    the code is:
    private int currentValue;

    public void run(){

    BufferedInputSt ream in = null;
    BufferedOutputS tream out = null;

    try{
    File inFile = new File(jtfOpen.ge tText().trim()) ;
    in = new BufferedInputSt ream(new FileInputStream (inFile));

    File outFile = new File(jtfSave.ge tText().trim()) ;
    out = new BufferedOutputS tream(new FileOutputStrea m(outFile));

    long totalBytes = in.available();

    jpb.setValue(0) ;
    jpb.setMaximum( 100);

    int r;
    long readBytes=0;
    byte[] b = new byte[10];
    while((r = in.read(b,0,b.l ength)) != -1){
    out.write(b,0,r );
    readBytes += r;
    currentValue = (int)(readBytes * 100 / totalBytes);

    jpb.setValue(cu rrentValue);
    }
    }

    catch(FileNotFo undException ex){
    ex.printStackTr ace();
    }

    catch(IOExcepti on ex){
    ex.printStackTr ace();
    }

    finally{
    try{
    if (in != null) in.close();
    if (out != null) out.close();
    }
    catch(Exception ex){}
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by eyeofsoul
    i am working in copying file to other destination.my code doesnt work as expected.can anybody help me.i got this error message --->
    java.io.FileNot FoundException: C:\Documents and Settings\XXXXXX X\My Documents (Access is denied)
    at java.io.FileOut putStream.open( Native Method)
    at java.io.FileOut putStream.<init >(FileOutputStr eam.java:179)
    at java.io.FileOut putStream.<init >(FileOutputStr eam.java:131)
    at test1.PilihFail $Salin2.run(Pil ihFail.java:111 )

    the code is:
    private int currentValue;

    public void run(){

    BufferedInputSt ream in = null;
    BufferedOutputS tream out = null;

    try{
    File inFile = new File(jtfOpen.ge tText().trim()) ;
    in = new BufferedInputSt ream(new FileInputStream (inFile));

    File outFile = new File(jtfSave.ge tText().trim()) ;
    out = new BufferedOutputS tream(new FileOutputStrea m(outFile));

    long totalBytes = in.available();

    jpb.setValue(0) ;
    jpb.setMaximum( 100);

    int r;
    long readBytes=0;
    byte[] b = new byte[10];
    while((r = in.read(b,0,b.l ength)) != -1){
    out.write(b,0,r );
    readBytes += r;
    currentValue = (int)(readBytes * 100 / totalBytes);

    jpb.setValue(cu rrentValue);
    }
    }

    catch(FileNotFo undException ex){
    ex.printStackTr ace();
    }

    catch(IOExcepti on ex){
    ex.printStackTr ace();
    }

    finally{
    try{
    if (in != null) in.close();
    if (out != null) out.close();
    }
    catch(Exception ex){}
    }
    }
    1.) Please use code tags when posting code.
    2.) Access denied is a security issue. Do you have rights to the file you are trying to access?

    Comment

    • eyeofsoul
      New Member
      • Sep 2007
      • 31

      #3
      of course.what makes that things happens..i am really confused..

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by eyeofsoul
        java.io.FileNot FoundException: C:\Documents and Settings\XXXXXX X\My Documents (Access is denied)
        at java.io.FileOut putStream.open( Native Method)
        at java.io.FileOut putStream.<init >(FileOutputStr eam.java:179)
        at java.io.FileOut putStream.<init >(FileOutputStr eam.java:131)
        at test1.PilihFail $Salin2.run(Pil ihFail.java:111 )
        Which of the lines is line 111 in the code? The problem seems to be there somewhere...

        Greetings,
        Nepomuk

        Comment

        • eyeofsoul
          New Member
          • Sep 2007
          • 31

          #5
          this is the whole code..
          [CODE=java]
          package test1;

          import java.awt.*;
          import java.awt.event. *;
          import javax.swing.*;
          import javax.swing.bor der.*;
          import java.io.*;
          import javax.swing.Swi ngUtilities;
          import javax.swing.fil echooser.*;


          public class PilihFail extends JFrame implements ActionListener {

          private JProgressBar jpb = new JProgressBar();
          private JButton openButton = new JButton("Browse ");
          private JButton saveButton = new JButton("Dir");
          private JButton Copy = new JButton("Copy") ;
          private JTextField jtfOpen = new JTextField();
          private JTextField jtfSave = new JTextField();
          private JFileChooser fc = new JFileChooser();
          private JFileChooser fc2 = new JFileChooser();

          public PilihFail(){

          JPanel soul = new JPanel();
          soul.setLayout( new BorderLayout()) ;
          soul.setBorder( new TitledBorder("D ari"));
          soul.add(jtfOpe n, BorderLayout.CE NTER);
          soul.add(openBu tton,BorderLayo ut.EAST);

          JPanel soul2 = new JPanel();
          soul2.setLayout (new BorderLayout()) ;
          soul2.setBorder (new TitledBorder("S ini"));
          soul2.add(jtfSa ve, BorderLayout.CE NTER);
          soul2.add(saveB utton,BorderLay out.EAST);

          JPanel soul3 = new JPanel();
          soul3.setLayout (new GridLayout(2,1) );
          soul3.add(soul) ;
          soul3.add(soul2 );

          JPanel soul4 = new JPanel();
          soul4.add(Copy) ;

          this.getContent Pane().add(soul 3,BorderLayout. CENTER);
          this.getContent Pane().add(soul 4,BorderLayout. SOUTH);
          this.getContent Pane().add(jpb, BorderLayout.NO RTH);

          saveButton.addA ctionListener(t his);
          openButton.addA ctionListener(t his);
          Copy.addActionL istener(this);
          }

          public void actionPerformed (ActionEvent e){

          //Handle open button action.
          if (e.getSource() == openButton) {
          int returnVal = fc.showOpenDial og(PilihFail.th is);

          if (returnVal == JFileChooser.AP PROVE_OPTION) {
          File file = fc.getSelectedF ile();
          //This is where a real application would open the file.
          jtfOpen.setText (file.getAbsolu tePath());
          } else {
          // log.append("Ope n command cancelled by user." + newline);
          }
          // log.setCaretPos ition(log.getDo cument().getLen gth());


          }
          //Handle save button action.
          else if (e.getSource() == saveButton) {
          fc2.setFileSele ctionMode(JFile Chooser.DIRECTO RIES_ONLY);
          int returnVal = fc2.showSaveDia log(PilihFail.t his);
          if (returnVal == JFileChooser.AP PROVE_OPTION) {
          File file = fc2.getSelected File();
          //This is where a real application would save the file.
          jtfSave.setText (file.getAbsolu tePath());
          } else {
          // log.append("Sav e command cancelled by user." + newline);
          }
          // log.setCaretPos ition(log.getDo cument().getLen gth());
          }

          else if (e.getSource() == Copy) {
          new Salin2().start( );
          }
          }

          public static void main (String[] args){
          PilihFail frame = new PilihFail();
          frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
          frame.setTitle( "Mari Pilih Fail");
          frame.setSize(4 00, 180);
          frame.setVisibl e(true);
          }

          class Salin2 extends Thread{
          private int currentValue;

          public void run(){

          BufferedInputSt ream in = null;
          BufferedOutputS tream out = null;

          try{
          File inFile = new File(jtfOpen.ge tText().trim()) ;
          in = new BufferedInputSt ream(new FileInputStream (inFile));

          File outFile = new File(jtfSave.ge tText().trim()) ;
          out = new BufferedOutputS tream(new FileOutputStrea m(outFile));

          long totalBytes = in.available();

          jpb.setValue(0) ;
          jpb.setMaximum( 100);

          int r;
          long readBytes=0;
          byte[] b = new byte[10];
          while((r = in.read(b,0,10) ) != -1){
          out.write(b,0,r );
          readBytes += r;
          currentValue = (int)(readBytes * 100 / totalBytes);

          jpb.setValue(cu rrentValue);
          }
          }

          catch(FileNotFo undException ex){
          ex.printStackTr ace();
          }

          catch(IOExcepti on ex){
          ex.printStackTr ace();
          }

          finally{
          try{
          if (in != null) in.close();
          if (out != null) out.close();
          }
          catch(Exception ex){}
          }
          }

          }
          }
          [/CODE]
          Last edited by r035198x; Sep 18 '07, 03:48 PM. Reason: added the code type to the code tags

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            So the relevant line 111 is
            [CODE=java]
            out = new BufferedOutputS tream(new FileOutputStrea m(outFile));
            [/CODE]right? Make sure, that you're not trying to write information to a folder - that will not be allowed. If outFile is a folder, change that and try again.

            Greetings,
            Nepomuk

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              If you want to copy a file called test.zip to the myFolder directory, then your destination path should be
              /myFolder/test.zip not just /myFolder.

              That's what's wrong with your code. You basically need to get the file name (including extension) and append that to the destination path.

              Comment

              • eyeofsoul
                New Member
                • Sep 2007
                • 31

                #8
                okay..i think i got the idea..but how do i combine the destination path with the real name of the file that want to be copied?

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  Originally posted by eyeofsoul
                  okay..i think i got the idea..but how do i combine the destination path with the real name of the file that want to be copied?
                  You can get the original file name with
                  [CODE=java]
                  inFile.getName( )
                  [/CODE]Use it, when setting the path to the new file.

                  Greetings,
                  Nepomuk

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by eyeofsoul
                    okay..i think i got the idea..but how do i combine the destination path with the real name of the file that want to be copied?
                    You read the source file's path and then use the getName method of the File class.
                    There are quite a few useful methods and fields in that File class that you can make use of as well.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by nepomuk
                      You can get the original file name with
                      [CODE=java]
                      inFile.getName( )
                      [/CODE]Use it, when setting the path to the new file.

                      Greetings,
                      Nepomuk
                      You're awefully fast today.

                      Comment

                      • eyeofsoul
                        New Member
                        • Sep 2007
                        • 31

                        #12
                        as i mention in the code.the file that want to be copied is in browse button and the dir button is in the other "if".hope you understand.i cant write clearly.

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by eyeofsoul
                          i got a problem here.
                          Code:
                          String s = "\";
                          unclosed string literal.
                          i dont understand this.i want to declare s as "\" but the line were underline saying unclosed string literal.
                          Check the API for the File class like I said. You may find that there is no need to declare a string with that value at all.

                          To make it work you would need to escape it because it's a special character so you can do
                          [CODE=java]String slash = "\\";[/CODE]

                          Comment

                          • eyeofsoul
                            New Member
                            • Sep 2007
                            • 31

                            #14
                            Code:
                            #
                            if (e.getSource() == openButton) {
                            #
                                        int returnVal = fc.showOpenDialog(PilihFail.this);
                            #
                             
                            #
                                        if (returnVal == JFileChooser.APPROVE_OPTION) {
                            #
                                            File file = fc.getSelectedFile();
                            #
                                            //This is where a real application would open the file.
                            #
                                           jtfOpen.setText(file.getAbsolutePath());
                            #
                                        } else {
                            #
                                          //  log.append("Open command cancelled by user." + newline);
                            #
                                        }
                            #
                                       // log.setCaretPosition(log.getDocument().getLength()  );
                            #
                             
                            #
                                   
                            #
                                    }
                            #
                                    //Handle save button action.
                            #
                                    else if (e.getSource() == saveButton) {
                            #
                                        fc2.setFileSelectionMode(JFileChooser.DIRECTORIES_  ONLY);
                            #
                                        int returnVal = fc2.showSaveDialog(PilihFail.this);
                            #
                                        if (returnVal == JFileChooser.APPROVE_OPTION) {
                            #
                                            File file = fc2.getSelectedFile();
                            #
                                            //This is where a real application would save the file.
                            #
                                          jtfSave.setText(file.getAbsolutePath());
                            #
                                        } else {
                            #
                                         //   log.append("Save command cancelled by user." + newline);
                            #
                                        }
                            #
                                     //   log.setCaretPosition(log.getDocument().getLength()  );
                            #
                                    }
                            #
                                   
                            #
                                    else if (e.getSource() == Copy) {
                            #
                                       new Salin2().start();
                            #
                                    }
                            as you can see.the destination path is in file2.how do i get the file.getName() in the first if to be called in the second if?

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by eyeofsoul
                              Code:
                              #
                              if (e.getSource() == openButton) {
                              #
                                          int returnVal = fc.showOpenDialog(PilihFail.this);
                              #
                               
                              #
                                          if (returnVal == JFileChooser.APPROVE_OPTION) {
                              #
                                              File file = fc.getSelectedFile();
                              #
                                              //This is where a real application would open the file.
                              #
                                             jtfOpen.setText(file.getAbsolutePath());
                              #
                                          } else {
                              #
                                            //  log.append("Open command cancelled by user." + newline);
                              #
                                          }
                              #
                                         // log.setCaretPosition(log.getDocument().getLength()  );
                              #
                               
                              #
                                     
                              #
                                      }
                              #
                                      //Handle save button action.
                              #
                                      else if (e.getSource() == saveButton) {
                              #
                                          fc2.setFileSelectionMode(JFileChooser.DIRECTORIES_  ONLY);
                              #
                                          int returnVal = fc2.showSaveDialog(PilihFail.this);
                              #
                                          if (returnVal == JFileChooser.APPROVE_OPTION) {
                              #
                                              File file = fc2.getSelectedFile();
                              #
                                              //This is where a real application would save the file.
                              #
                                            jtfSave.setText(file.getAbsolutePath());
                              #
                                          } else {
                              #
                                           //   log.append("Save command cancelled by user." + newline);
                              #
                                          }
                              #
                                       //   log.setCaretPosition(log.getDocument().getLength()  );
                              #
                                      }
                              #
                                     
                              #
                                      else if (e.getSource() == Copy) {
                              #
                                         new Salin2().start();
                              #
                                      }
                              as you can see.the destination path is in file2.how do i get the file.getName() in the first if to be called in the second if?
                              Get the destination File's path using some method in the File class (you really need to open the page for that class) and then append the name of the file you want to put there. You may need to include a file separator (also available in the file class)

                              Comment

                              Working...