• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 901
  • Last Modified:

using java JFileChooser Action performed to put file in specific folder

Hi,
 the attached application uses JFileChooser to open a file. That is working fine.
What I want  to happen when  "open" is  selected for  the file is  that  the file is put into a specific folder ie C:\\ANT\\Source\\
I have been trying to implement this using the jMenuItemActionPerformed method ( see code snippet below) but end up getting compiler errors.
The current implementation gets no compiler errors,
Can you help put   the chosen file in  the specified folder?
thanks
ps in  the attached file  the method starts at line 107
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
JFileChooser jfc = new JFileChooser();
        if(jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            //code to handle choosed file here.
                   // TODO add your handling code here:
                   File file = jfc.getSelectedFile();
                   FileInputStream in = null;
        FileOutputStream out = null;
        File folder = new File("C:\\ANT\\Source\\");
        
    File[] listOfFiles = folder.listFiles();
                   try {
                  
                   in = new FileInputStream(file);
                   
                   }
                   catch(FileNotFoundException e){}
                                   
    }
    }

Open in new window

JFrame.txt
0
blossompark
Asked:
blossompark
  • 5
  • 4
2 Solutions
 
andref123Commented:
use the file method renameTo();



file.renameTo("C\\ANT\\Source\"+file.getName());

Open in new window

0
 
blossomparkAuthor Commented:
hi..thanks for that...something wrong with  that syntax?
file.renameTo("C\\ANT\\Source\"+file.getName());

getting "unclosed string literal" message from compiler when i add this line
0
 
blossomparkAuthor Commented:
maybe should be
  file.renameTo("C\\ANT\\Source\\"+file.getName());
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
blossomparkAuthor Commented:
ok.. in NetBeans now getting message
" method renameTo in class java.io.File cannot be applied to given types
required; java.io.File
found:java.lang.String "
0
 
anilallewarCommented:
The rename to method has the following signature. You have to pass a File parameter to the method.

public boolean renameTo(File dest)

Can you tell us what is the compilation error you are getting? You should be fin with creating a new file in the destination directory and writing the contents.

Here is a sample program I had created which is similar. See if that helps you out.
FileLister.java
0
 
anilallewarCommented:
The File API also provides a way to create a file in specified directory.

public File(String parent,String child)

Example would be

File target = new File("C\\ANT\\Source\",file.getName());
0
 
blossomparkAuthor Commented:
Hi anilallewar:,
firstly thanks for your posts and secondly apologies for  the delayed response....the christmas festivities took precedence the last few days!!

using your example
File target = new File("C\\ANT\\Source\",file.getName());

i have modified the jMenuItem1ActionPerformed method as follows;
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
JFileChooser jfc = new JFileChooser();
        if(jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            //code to handle choosed file here.
            File file = jfc.getSelectedFile();
            jTextArea2.append(file.getName() + " " + "\n");
            File target = new File("C\\ANT\\Source\\",file.getName());
  }

the java file that the method is contained in (attached) compiles and runs fine but it does not populate  the C\\ANT\\Source folder with the files chosen.
My expectation is  that once a file is chosen (File file = jfc.getSelectedFile();), two things should happen.
1/ the text area displays the name of  the chosen file    ( jTextArea2.append(file.getName() + " " + "\n");) ...this is happening successfully.
2/ the folder C\\ANT\\Source\\ ....(File target = new File("C\\ANT\\Source\\",file.getName());)....should now contain the chosen file...this is not happening, the folder remains empty
JFrame1.java
0
 
anilallewarCommented:
@blossompark:

You would need to write code to get the file from the source to the target folder. I have added the necessary code and tested it successfully on my M/C.

Let me know if you still face any issues.
JFrame1.java
0
 
anilallewarCommented:
Additionally you would have to code the Progress bar stuff to show the progress while the file transfer is going on. I see that you have not coded for showing the progress in your jMenuItem1ActionPerformed() method; you should do that in the transferFile() method.
0
 
blossomparkAuthor Commented:
thanks for your efforts much appreciated...will implement your code...thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now