File.renameTo method issue

I have a Java program which processes upto 10000 text files a day. The average size of these files are 1KB to 2KB and the java program parses the contents of the text file, validates and loads them into a database table.
The program outline is as follows
File[] filesToProcess = sourceFolder.listFiles();
int fileCount = filesToProcess.length;
for (int i=0;i<fileCount;i++) {
File currentFile = filesToProcess;
MsgParser fileParser = new MsgParser(currentFile);
if ( fileParser.isMessageValid()) {
boolean uploadSuccess = fileParser.uploadToDB();
if (uploadSuccess) {
if (currentFile.renameTo(parseSuccessArchiveFile))
logger.info("File successfully moved to archive folder);
else
logger.info("File cannot be moved to archive folder);
} else {
if (currentFile.renameTo(parseFailureArchiveFile)
logger.info("Parser failed file moved to archive folder);
else
logger.info("Parser failed file cannot be moved to archive folder);
The above program outline works well. However the renameTo method is not renaming the parsed files successfully on all occasions. There is no performance issue involved here as the files keep streaming in throughout the day and the program is able to handle the parsing of the file(s) in a fraction of second.
The issue for me is that I keep picking the files to parse from the source folder at regular intervals ( after a full iteration of the files list, wait for 10 seconds before calling for a new set of files ) and cannot afford to have a failure in moving the file to the archive folder as otherwise, I end up parsing the same file multiple number of times, which I want to avoid.
Can anyone shed some light on the behavior of File.renameTo and how to make it work successfully?
Sundar

I am making the code more readable for you all and corrected some typo errors...
File[] filesToProcess = sourceFolder.listFiles();
int fileCount = filesToProcess.length;
for (int i=0;i<fileCount;i++) {
      File currentFile = filesToProcess;
      MsgParser fileParser = new MsgParser(currentFile);
      boolean uploadSuccess = false;
      if ( fileParser.isMessageValid())
           uploadSuccess = fileParser.uploadToDB();
      if (uploadSuccess) {
            if (currentFile.renameTo(parseSuccessArchiveFile))
                  logger.info("File successfully moved to archive folder);
            else
                  logger.info("File cannot be moved to archive folder);
      } else {
            if (currentFile.renameTo(parseFailureArchiveFile)
                  logger.info("Parser failed file moved to archive folder);
            else
                  logger.
                  info("Parser failed file cannot be moved to archive folder);
}

Similar Messages

  • Java.io.File renameTo does not work on Solaris

    Hi Experts,
    I have a code-piece which tries to move files from one directory to another on the SAME FILE SYSTEM using java.io.File.renameTo method.
    It works fine when there less no. of files in the source directory. But the renameTo does not work as expected when there are very huge no. of files(~40K) present in the source directory.
    I am aware that there is a known issue when one tries to use this method to move files across file systems OR if the file exists at the destination directory.
    But in my case the file system is the same and the there is no such file in the destination directory.
    I suspect some inode related issues. But not sure what it is exactly.
    Is there any limitations of renameTo?
    Please help.

    OK, we all now understand you have a problem in doing this, but without the specifics of the problem that you are experiencing, then it is fairly difficult to give you anything other than a guess. If you would like some specific and relevent answers, please supply specifics about how exactly the operation differs from your expected results. You should also post example code as it is almost always asked for eventually.

  • Problem with File obj renameTo() method

    I need help, I can't get the renameTo() method of the File object to work.
    I have 2 File objects, one is an existing file, and one I just performed the .delete()
    method on. Now I want to rename the existing file to the name of the one I
    just deleted. According the the java documentation the syntax is
    FileObject.renameTo(FileObject with desired name);
    This method is acting peculiar. The file is getting renamed, however
    after the rename, if i run the File object method .getName()
    it returns its old name before it was renamed. So the file is sitting in
    my filesystem newly renamed, but its getName() method returns the
    name that it held before I renamed it.
    WHy is this ?
    This is happening in the context of a JPanel, and I am stuffing the name
    into the title, since it fails, the title after the rename is of the original "temp"
    file.
    Here is my code.
    // tmp file is created and populated
    // old file is deleted
    // now rename tmp file to old file
    // Rename tmp file to correct name
                                    newTmpFile.renameTo(newFile); // Rename tmp file to file object selected from dialog
                                    originalFileName = newTmpFile.getName(); // Now grab the name
                                    setTitle("Editing - " + originalFileName); // Update the title

    Here's the entire routine...
            public void saveAsFileIO() {
                    // (pull up Save As Dialog) Prompt for new File Name
                    final JFileChooser fcSaveAs = new JFileChooser();
                    fcSaveAs.addChoosableFileFilter(new CDBFileFilter());
                    int returnVal = fcSaveAs.showSaveDialog(this);
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                            File newFile = fcSaveAs.getSelectedFile(); // Grab file from dialog
                            String strNewTmpPath = newFile.getParent() + "\\save.tmp"; // Use its path but add a different filename
                            File newTmpFile = new File(strNewTmpPath); // Create a new tmp file from this new path and name
                            int x, y;
                            try {
                                    FileWriter cdbWriter = new FileWriter(newTmpFile);
                                    BufferedWriter br = new BufferedWriter(cdbWriter);
                                    for (x = 0; x < cellList.listModel.getSize(); x++) {
                                            // FOR EACH Item in JList
                                            String[] arCells = (String[]) cellArrayList.get(x); // Grab array of values
                                            for (y = 0; y < arCells.length; y++) {
                                                    // Create string for this JList item
                                                    if (line == "") {
                                                            line = arCells[y]  + ",";
                                                    } else {
                                                            line += arCells[y] + ",";
                                            //System.out.print(line + "\n");
                                            // Write string to tmp file
                                            br.write(line);
                                            br.newLine();
                                            line = "";
                                    br.close();
                                    // Delete old file
                                    if (originalFileName != "") { // When saving an exisiting file as a new one, this runs
                                            String strOldPath = originalPath + "\\" + originalFileName;
                                            File oldFile = new File(strOldPath);
                                            oldFile.delete();
                                    // Rename tmp file to correct name
                                    newTmpFile.renameTo(newFile); // Rename tmp file to file object selected from dialog
                                    originalFileName = newTmpFile.getName(); // Now grab the name
                                    setTitle("CDB Editor - " + originalFileName); // Update the title
                            } catch (FileNotFoundException fnf) {
                                    System.err.println("Unable to open file for writing: " + fnf.getMessage());
                            } catch (IOException ioe) {
                                    System.err.println("unable to buffer write file: " + ioe.getMessage());
            }

  • RenameTo() method in File.class

    I want to know in which platforms the renameTo() method in the File class actually move the file to the detination directory. I have checked with Windows. It does move the file to the targetted directory. What about other platforms??

    YoungWinston wrote:
    supratim wrote:
    My concern is if I am sure that that the renameTo() succeeds in doing its thing[By doing a simple renameTo() on a file and check if it is getting moved],
    can it supply me any perfomrmance boost compared to normal stream based copying and then deleting the file.I think you'll have to describe what you're trying to do, and what you think your alternatives are, a bit better.
    You never know, somebody may have a '3rd way' which is superior to either.
    WinstonMost certainly there may be a lot more ways. All I want to know does the renameTo() method works more faster than the normal stram based copying[which includes creating InputStream  and OutputStream] which looks ugly[catching FileNotFoundException,IOException etc]. Well don't take the "ugly" word otherwise, this just waht I think.
    I have learned that if FileChannel s are used it, it can boost up the copying speed compared to the stream based copying. But channels are normally used where a bigger size of file needs to be transferred. Should I use channels for faster io.
    I am so concerned about the speed because it is preciously what I need to do in my current work. I have to do a lot of io[file moving,copying etc] and my files are also not very big[max 3MB,min 3KB], and I want to know the best way to achieve this in a perfect and of course faster way.

  • Behaviour of  renameTo(File f) method

    Hi ,
    I have to move some files from one directory to another and I found the renameTo(...) method very useful.
    The question is: does renameTo() method perform an atomic operation?
    What would happen if the application crashes before the method returns?
    Thanks for your help,
    Paul

    What would happen if your harddrive dies? What if a meteor hits you?
    The question whether it's atomic or not probably depends on the underlying file syste, Java can't do anything but to relay the command to the OS.

  • Error while transferring file :: Error while issuing ssh command.

    Hi All,
    I am having a JSP based website application.
    I am getting the following exception in my logs.
    [ERROR] 05/04/2007 03:43:44 - Error while transferring file :: Error while issuing ssh command.
    java.net.ConnectException: Connection timed out
                at java.net.PlainSocketImpl.socketConnect(Native Method)
                at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
                at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
                at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
                at java.net.Socket.connect(Socket.java:464)
                at java.net.Socket.connect(Socket.java:414)
                at java.net.Socket.<init>(Socket.java:310)
                at java.net.Socket.<init>(Socket.java:125)
                at com.sshtools.j2ssh.net.SocketTransportProvider.<init>(Unknown Source)
                at com.sshtools.j2ssh.net.TransportProviderFactory.connectTransportProvider(Unknown Source)
                at com.sshtools.j2ssh.SshClient.connect(Unknown Source)
                at com.sshtools.j2ssh.SshClient.connect(Unknown Source)
                at com.sshtools.j2ssh.SshClient.connect(Unknown Source)
                at com.novartis.util.DataExporter.transportFile(DataExporter.java:127)
                at com.novartis.util.DataExporter.export(DataExporter.java:101)
                at com.novartis.businessmanagers.SandosSignupManagerPojoImpl.export(SandosSignupManagerPojoImpl.java:103)
                at com.novartis.util.DataExportJob.process(DataExportJob.java:48)
                at com.novartis.util.ExclusiveJob.execute(ExclusiveJob.java:35)
                at org.quartz.core.JobRunShell.run(JobRunShell.java:191)
                at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:516)Any help would be appreciated.
    Thanks.

    This means that the SSH server you're trying to connect to isn't listening. Is the SSH server running? Are you trying to connect to the right machine?

  • Problem with the renameTO method in the Linux environment

    Hi
    I got a problem with the renameTO method in the Linux environment. The file is not moving.
    This method is returning false. the same code executed successfully in Windows environment.
    Can anyone give some fix to this one or an alternate solution to move the files in both windows and Linux.
    boolean success;
    File root = new File(tempPath);
                   File f = new File(root, phyFileName);
                   File dest = new File(targetPath);
    success = f.renameTo(new File(dest, actualFileName));actualFileName = 400.doc
    dest = /home/jboss-4.0.3/axsscm_1.0/axsscmDocuments/xchange/fileup/fshare/PO/1786

    JITHENDRA wrote:
    Thanks for the prompt replyNo problem.
    >
    Can u solve the below doubt.
    Will renameTo method wont work in Linux? If so why?Did you not read what I said? I suspect you are trying to rename a file so that it actually has to be moved to a different volume (partition or hard disk) so it won't work. One would have the same problem on Windows trying to rename a file on the c: drive to a name on the d: drive.
    >
    >
    Can u give a sample or good link to do the above work which works fine in all environments.?Just follow the pseudo code I gave. 15 minutes work.

  • File.renameTo

    Hi,
    Does file.renameTo work on AIX Server,and how much time will this method take to move a file of 35 million records?
    Regards
    KK

    NAME
    rename - rename a file
    SYNOPSIS
       #include <stdio.h>
        int rename(const char old, const char new);
    DESCRIPTION
    The rename() function changes the name of a file.
    The old argument points to the pathname of the file to be renamed.
    The new argument points to the new pathname of the file.
    posman@proli:~/ivan> df -k . /tmp
    Filesystem           1K-blocks      Used Available Use% Mounted on
    /dev/cciss/c0d0p3     16682556  14755300   1927256  89% /home
    /dev/cciss/c0d1p2     35539772  33619432   1920340  95% /
    posman@proli:~/ivan> cat x.c
    #include <stdio.h>
    int main() {
    int r =  rename("movethis","/tmp");
    if (r !=0) perror("rename");
    return r;
    }posman@proli:~/ivan> gcc x.c -o x
    posman@proli:~/ivan> ./x
    rename: Invalid cross-device link
    Edited by: BIJ001 on Oct 1, 2007 9:01 AM

  • Writing a universal file sizeof() method.

    Hi,
    I am writing an application that needs to compute the exact size of a file. I need for this function to work in Windows and UNIX.
    One of my issues is that the length() method provided by the File class does not return the exact size of all files.
    The essence of my problem is that I cannot properly compute the size of a directory or symbolic links in UNIX.
    File.class() returns 0 for the size of directories regardless of OS. This appears to be correct on Windows (right-click -> properties on an empty directory in Windows). However this seems to be incorrect according to Linux.
    When compared to the output of ls, the File.length() method will not return the proper length of directories (whether on a FAT or reiserfs file system) and symbolic links in UNIX. When the windows partition is mounted in Linux, the size of the directories on the windows partition are reported as being 4096 by file system utilities ls and du.
    This contrasts the value of zero reported by the Microsoft 98 Windows Explorer. Moreover, the size of directories on reiserfs file systems seem to be unpredictable. (Whereas the size of a symbolic link is the length of the filename it is pointing to.)
    Detailed examples of my problem can be found at the following thread: http://forum.java.sun.com/thread.jspa?threadID=574755
    How can I compute a function that will be able to compute the size of any file under any operating system?
    I would like to avoid his path:
        1. Determine if the class is being run in Windows or UNIX
        2. If running in UNIX,
               a. If a directory of symbolic link: execute : du -sb [file] | awk '{print $1}'
                  or ls -al [file ] | awk ' { print  $5 }'
               b. Otherwise: File.length()
        3. If running Windows, simply class File.length() (?)By taking this route I need to perform an if() statement every time the size of any file is being computed, and also execute an expensive command for some special cases (which will common).
    Thanks in advance for reading the entire article and thinking about potential solutions.

    I am writing an application that will take an arbitrary number of directories, and split the data into the appropriately size chunks to be burned onto CDs.
    The directories are passed as strings, and I am currently using Java classes to compute the size of the files. The computed size of the files must be accurate, as I am passing the segmented files into mkisofs (to create an image), and then burn that image onto a CD using cdrecord.
    If the sizes are not computed properly, too many files may be passed to mkisofs (and the CD will be bad); or that the code is not entirely efficient (or correct).
    As a "correct" value to use against validation, I use du on UNIX and the "properties" command on Windows.
    My problem arises on UNIX where the File.length() method returns 0. My computed value is always less than the value given by du, since the values of directories are not properly computed because of the length() method.
    This also makes the application difficult to debug. Specifially, verifying that all files passed in are actually all there. IE, are small directories missing or are there small files missing?

  • JFileChooser and File.renameTo()

    Hi,
    Anyone experienced strange behaviour from the JFileChooser returned
    File Object. In my case it's an instance of:
    sun.awt.shell.Win32ShellFolder2
    Unfortunatly the File.renameTo(File file) method doesn't work
    properly with this File instance.
    Anyhelp is welcome / dzone

    overwrite the approveSelection() method of your JFileChooser. in there you can do your checks before executing super.approveSelection().
    tthomas

  • Renameto method

    I use "renameTo" method to cut and paste a file from one location to another, it is not working sometimes if I try to do it through my web application. How can I fix this.

    Ok. please check my code below.
    File src = new File("C:/test.zip");
    File dest = new File("D:/chk");
    boolean transfer = src.renameTo(dest);When I use the above code the file doenot get transfeered from the source to destination.

  • File.renameTo(..) does not work with NFS?

    I have some code the essentially does this:
    1. Create Destination Directories
    file.mkdirs(..);
    2. For each file..'rename to destination'
    for (..)
    file.renameTo(..);
    * Where the rename To is 'moving' the file to the dest directory.
    This code works fine on LocalDrive to LocalDrive.
    But when I goes to the NFS...
    The Directories get created on the NFS.
    But the files never make it there..no exception is thrown...
    Has Anyone encountered such behavior before?
    The system obviosly has write permissions since the directories do get created...

    ok...something that has been bugging me..
    We all know that Java IO is dog slow (perhaps now with 1.4 it is better with non blocking io)..but..here's my gripes..
    for Java API calls where a native equivalent exists on the OS, then native equivalent should be invoked (as it is more then likey going to be faster). In other words shouldnt renameTo call mv on unix.
    now java has no copy method..so..some time ago..I implemented a 'native' copy..simply system.exec("cp ") etc..(much quick the copying the streams)
    This works..but after finding out the system.exec FORKS the entire VM, we can no longer use this, as memory requirements are strict.
    It seams to me that sun has messed up here...
    or am I missing something?
    thoughts?
    Dan

  • Class File - renameTo problem

    hi all!
    Environment:
    OS: Windows XP
    Filesystem: NFS
    JRE: jre1.5.0_04
    I have the following code:
    File logfile = new File(new File(this.lpLogDir), this.lpFileName);
              if( logfile.exists() ) {
                   if( logfile.renameTo(new File(new File(this.lpLogDir), (Calendar.getInstance()).getTimeInMillis() + "." + this.lpFileName)) ){
                        System.out.println("File renamed to " + new File(new File(this.lpLogDir), (Calendar.getInstance()).getTimeInMillis() + "." + this.lpFileName).getAbsolutePath());
                   }else {
                        System.out.println("Could not rename the file to " + new File(new File(this.lpLogDir), (Calendar.getInstance()).getTimeInMillis() + "." + this.lpFileName).getAbsolutePath());
                   System.out.println(logfile.getName());My problem: the file is not renamed after this operation. The renameTo() method returns true, but the getName() prints the old file name. Any suggestions?
    cheers
    dian

    renameTo does NOT change logfile. It still points the abstract file "this.lpLogDir "/" this.lpFileName". What it does it moves the content in the concreate file logfile to a new location.

  • The .txt file has formatting issues that need to be resolved

    Transaction ZEHS20 runs a program that assigns a CCL Group to a specification header, based on its Active Ingredients. The transaction can be run in the background if desired with output being emailed to the user.The .txt file has formatting issues that need to be resolved. can u  pls Help me to resolve this issue.
    Thanks

    What is the formatting issue like? Could you please elaborate a bit more.
    If it is because of varying lengths in the data then it can be handled by utilising a CONCATENATE RESPECTING blanks.

  • Weird file renaming permission issues

    Hi!
    We have a file rename permission issue. Here is the background:
    We created a 2008 R2 DFS namespace called UserData with Read/write share permissions for Administrators, Everyone and System. UserData has been granted NTFS permissions as follows:
    Everyone (This folder only): Traverse folder / Execute files, List folder / Read data, Read attributes, Create folders / Append data
    CREATOR OWNER (Subfolders and files only): Full control
    SYSTEM (This folder, subfolders and files): Full control
    Domain Admins (This folder, subfolders and files)
    We then enabled folder redirection for users My documents folder through GPO, setting the following:
    Setting: Basic - Redirect everyones folder to the same location
    Target folder: Create a folder for each user under the root path
    \\domain\UserData
    We also unchecked Grant the user exlusive rights to documents.
    So, now to the really weird behaviour. We logged on to a Windows 7 (x64) client computer with a user who gets this GPO settings and that is not local administrator on the client. The folder is redirected as expected and we can create, delete and write to
    files in anyway we want. We can also rename files if we choose an entirely different name and if we choose a longer or a shorter file name,
    but we cannot rename the file to something with the same letters but different casing.
    Examples of what will work:
    "test" to "testing"
    "test" to "cool"
    "test" to "COOL"
    Examples of what will NOT work:
    "test" to "Test"
    "test" to "tesT"
    "test" to "TEST"
    We the get this error: "File Access Denied. You need permission to perform this action. You require permission from S-1-5-21-220..... to make changes to this file."
    Eventhough I'm pretty sure the share and NTFS permissions of the share are correctly set we have of course checked all the permissions when logged in and the user has Full NTFS control and Read/Write Share permissions.
    We have encountered the same problem on a customer company as well, with a different domain with no links to our domain what so ever. I have also seen similar problems from other people when trying to find the answer on internet. Here is an example:
    http://social.technet.microsoft.com/Forums/en/w7itprosecurity/thread/35ced5bb-ab13-4e28-8c48-7c68ce0b775c
    Anyone have any thoughts?
    /Leyan

    Resent discoveries:
    If I log onto a Windows 7 (x86) Enterprise I face the same Issues.
    If the same user logs on to one of the DFS servers holding the namespace and accesses his folder we experience
    no problems renaming files.
    Customer company states that all is working fine when user logs on to a Windows XP with SP3.
    /Leyan

Maybe you are looking for