copy String array of files to the destination directory and rename it


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: copy String array of files to the destination directory and rename it

Hybrid View

  1. #1
    Join Date
    Dec 2006
    Posts
    16

    copy String array of files to the destination directory and rename it

    Hi all, could anyone please help me with the following code.

    I am calling the class "filesMove" from my main class. I am passing 3 parameters:
    1. filenames - String array of filenames
    2. dstfile - destination directory, I need to copy my String array of filenames to here
    3. rename- this is the String to which I want to rename the files copied to dstfile. My "main.java" code is:

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
    {
    JFileChooser chooser=new JFileChooser();
    chooser.setCurrentDirectory(curfile);
    chooser.setMultiSelectionEnabled(true);
    int state=chooser.showOpenDialog(null);

    File[] files=chooser.getSelectedFiles();
    String[] filenames=getFilenames(files);

    File dstfile=new File("C:\\temp\\fontsInstallation");

    String rename=jTextField1.getText();

    filesMove fm=new filesMove();
    fm.copyDirectory(filenames,dstfile,rename);
    }
    File curfile=new File("D:\\fonts\\");

    private String[] getFilenames(File[] files)
    {
    String[] filenames=null;
    String sub=null;
    int numFiles=files.length;
    if(files.length>0)
    {
    filenames=new String[numFiles];
    for(int i=0; i<numFiles;i++)
    {
    filenames[i]=files[i].getPath();
    }
    }
    return filenames;
    }
    ====================================================================================
    my "filesMove.java" code is:

    public class filesMove
    {
    static String code="";
    public filesMove()
    {
    }

    void copyDirectory(String[] filenames, File dstfile, String rename)
    {
    for(int i=0;i<filenames.length;i++)
    {
    System.out.println("filenames[i]:"+filenames[i]);
    }
    System.out.println("dstfile:"+dstfile);
    System.out.println("rename:"+rename);
    }
    }

    for instance my source directory contains these many files:
    xyz.tfm
    xyz.PFM
    xyz.PFB
    abc.tfm
    abc.PFM
    abc.PFB

    I will be selecting only some files, so from the above code "filenames" will be fetching only these files. My program is working fine till here:
    abc.tfm
    abc.PFM
    abc.PFB


    Now I need to copy these files to "dstfile" directory:
    abc.tfm
    abc.PFM
    abc.PFB

    and rename it to:
    pmnr.tfm
    pmnr.PFM
    pmnr.PFB



    Thanks in advance

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    what happens (what error messages?) when you run your program?

  3. #3
    Join Date
    Dec 2006
    Posts
    16
    When I run my program, a file dialog box opens and am able to select only the files I need. This I can get it from "filenames[i]" in the "filesMove" class.

    Instead of passing all the files in the source directory to the "filesMove" class, I am passing only String array of files, because I need to pass only the selected few files and not all the files in that particular directory.

    At present I am not getting any error. But I need help to copy these selected few files from source directory to the destination directory and rename those files as mentioned earlier.

  4. #4
    Join Date
    Dec 2006
    Location
    Kerala,India
    Posts
    51
    I think, when we have the array of files, you don't want to convert this in to a String array at all.
    Using the File array you can copy from one location to another one location.
    check if this code is useful..
    Code:
    public boolean copyFile(File source, File destination) {
    	try{
            	InputStream in = new FileInputStream(source);
            	OutputStream out = new FileOutputStream(destination);
        
    	        // Transfer bytes from in to out
            	byte[] buf = new byte[1024];
    	        int len;
            	while ((len = in.read(buf)) > 0) {
            	    out.write(buf, 0, len);
            	}
            	in.close();
            	out.close();
    		return true;
        	}
    	catch(Exception e) {
    		e.printStackTrace();
    		return false;
    	}
    }
    
    public boolean renameto(File source,File destination){
    	return source.renameTo(destination);
    }

  5. #5
    Join Date
    Dec 2006
    Posts
    16
    I have already tried the code you have given. It works fine if both my source and destination are "File". But the problem is I need to send only the selected few files from my source to destination, hence I am using String arrray of files.

    Please help me with this.

  6. #6
    Join Date
    Dec 2006
    Location
    Kerala,India
    Posts
    51
    Create a class which implements java.io.FileFilter and override public boolean accept(File) method. Then call File.listFile(fileFilter). So after that the File Array contain only the required file. Your filtering logic can be written in public boolean accept(File) method. This is the standard way of doing it..

  7. #7
    Join Date
    Dec 2006
    Posts
    16
    Thanks my problem is solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center