[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

using java JFileChooser Action performed to put file in specific folder

Posted on 2009-12-21
10
Medium Priority
?
894 Views
Last Modified: 2013-12-14
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
Comment
Question by:blossompark
  • 5
  • 4
10 Comments
 

Expert Comment

by:andref123
ID: 26095598
use the file method renameTo();



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

Open in new window

0
 

Author Comment

by:blossompark
ID: 26096622
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
 

Author Comment

by:blossompark
ID: 26096640
maybe should be
  file.renameTo("C\\ANT\\Source\\"+file.getName());
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:blossompark
ID: 26096681
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
 
LVL 11

Expert Comment

by:anilallewar
ID: 26102500
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
 
LVL 11

Expert Comment

by:anilallewar
ID: 26102507
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
 

Author Comment

by:blossompark
ID: 26124189
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
 
LVL 11

Accepted Solution

by:
anilallewar earned 2000 total points
ID: 26137299
@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
 
LVL 11

Assisted Solution

by:anilallewar
anilallewar earned 2000 total points
ID: 26137311
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
 

Author Closing Comment

by:blossompark
ID: 31668494
thanks for your efforts much appreciated...will implement your code...thank you
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question