Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Java program to take new files from one path and sent it do another path?

  1. #1
    Member
    Join Date
    May 2008
    Posts
    35
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Java program to take new files from one path and sent it do another path?

    Hi Folks,

    I am having some files(.txt) in a directory called as Unprocessed folder,i would like to take one by one file and send it to the processing path say it as..C:\processingfolder
    Each time when an UnprocessFolder get any new file it should be sent to the processing folder.

    Any Idea....thanks in advance


    -jazz


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Folder Watching

    Hey,

    Do you want to do this with a Java application?

    I think there is a way to do this with Windows...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Member
    Join Date
    May 2008
    Posts
    35
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Folder Watching

    Yeah...with Java Only...Rep me ASAP

    Thanks

  4. #4
    Banned
    Join Date
    May 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Folder Watching

    use a thread that checks for a file every 1 sec (or less) , use a file inputstream /outputstream to read/write the file and a bufferedreader/scanner etc to process the file. Shouldn't be too hard to do it depending on the "processing" requiremens it can be done in a few hours.

  5. #5
    Member
    Join Date
    May 2008
    Posts
    35
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Folder Watching

    ok thanks for ur reply...

    i can list the files but not able to load them...means i can list the files from the Folder but not able to Move them...is any new file comes no it is not able to recognize...how to do this..please rep me..

    thanks

  6. #6
    Banned
    Join Date
    May 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Folder Watching

    Quote Originally Posted by jazz2k8 View Post
    ok thanks for ur reply...

    i can list the files but not able to load them...means i can list the files from the Folder but not able to Move them...is any new file comes no it is not able to recognize...how to do this..please rep me..

    thanks
    see my reply above, you can use this class i wrote to copy files from one directory to another: Engineeringserver.com | Copy a txt file from one location to another with Java

    to process it automatcally you can use a thread that checks the unprocessed folder every second and call the method in my class to process the file or move the file to another directory.

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Folder Watching

    Hey Jazz,

    Here is a crude example for you. You will have to edit the code to check dir1 every minute or put it into a loop or something.

    Currently this will take the text files from dir1 and re-create them into dir2.

    You will also need to add code to delete the files from dir1 after creation.

    import java.io.File;
    import java.io.*;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
     
    public class ListFiles 
    { 
     public static String path1;
     public static String path2;
     public static String files;
     public static String fileName;
     
     public static void main(String[] args) throws Exception
    {
       path1 = "/dir1/"; 
       path2 = "/dir2/";
     
      File folder = new File(path1);
      File[] listOfFiles = folder.listFiles(); 
      for (int i = 0; i < listOfFiles.length; i++) 
      {
     
       if (listOfFiles[i].isFile()) 
       {
       files = listOfFiles[i].getName();
           if (files.endsWith(".txt") || files.endsWith(".TXT"))
           {
     
              System.out.println(files);
     
              try
              {
              FileInputStream in = new FileInputStream(path1 + files);
              BufferedReader br = new BufferedReader(new InputStreamReader(in));
              String strLine;
     
         Writer output;
         File file1 = new File(path2 + files);
         String newline = System.getProperty("line.separator"); 
         output = new BufferedWriter(new FileWriter(file1));
     
              while((strLine = br.readLine())!= null)
              {
               //Write contents of text file
               output.write(strLine);
               output.write(newline);
              //System.out.println(strLine);
              }
     
               output.close();
     
              }catch(Exception e){
               System.out.println(e);
              }
     
            }
           }
      }
    }
    }

    Let me know if you get stuck and ill try to finish this off. I'm away from the computer now though so thought id post this quickly.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. #8
    Banned
    Join Date
    May 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Folder Watching

    Quote Originally Posted by JavaPF View Post
    Hey Jazz,

    Here is a crude example for you. You will have to edit the code to check dir1 every minute or put it into a loop or something.

    Currently this will take the text files from dir1 and re-create them into dir2.

    You will also need to add code to delete the files from dir1 after creation.

    import java.io.File;
    import java.io.*;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
     
    public class ListFiles 
    { 
     public static String path1;
     public static String path2;
     public static String files;
     public static String fileName;
     
     public static void main(String[] args) throws Exception
    {
       path1 = "/dir1/"; 
       path2 = "/dir2/";
     
      File folder = new File(path1);
      File[] listOfFiles = folder.listFiles(); 
      for (int i = 0; i < listOfFiles.length; i++) 
      {
     
       if (listOfFiles[i].isFile()) 
       {
       files = listOfFiles[i].getName();
           if (files.endsWith(".txt") || files.endsWith(".TXT"))
           {
     
              System.out.println(files);
     
              try
              {
              FileInputStream in = new FileInputStream(path1 + files);
              BufferedReader br = new BufferedReader(new InputStreamReader(in));
              String strLine;
     
         Writer output;
         File file1 = new File(path2 + files);
         String newline = System.getProperty("line.separator"); 
         output = new BufferedWriter(new FileWriter(file1));
     
              while((strLine = br.readLine())!= null)
              {
               //Write contents of text file
               output.write(strLine);
               output.write(newline);
              //System.out.println(strLine);
              }
     
               output.close();
     
              }catch(Exception e){
               System.out.println(e);
              }
     
            }
           }
      }
    }
    }

    Let me know if you get stuck and ill try to finish this off. I'm away from the computer now though so thought id post this quickly.
    Looks good, add a thread so that it checks for a new file and process it. Not that hard to code

  9. #9
    Member
    Join Date
    May 2008
    Posts
    35
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Folder Watching

    Thanks alot....will let you know after completing this

  10. #10
    Member
    Join Date
    May 2008
    Posts
    35
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Folder Watching

    Can we try with images....i.e. i need to upload .tif image.....:-)

  11. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Folder Watching

    I'm not sure how to go about doing this with images.

    The code above looks for all the .txt files in a dir, opens them 1 by 1 and re-creates them in the new directory. This wouldnt work with .tif images.

    You can still use the above code to list all the images in a directory. Ill have a think about how to move these files and get back to you..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  12. #12
    Banned
    Join Date
    May 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Folder Watching

    Quote Originally Posted by JavaPF View Post
    I'm not sure how to go about doing this with images.

    The code above looks for all the .txt files in a dir, opens them 1 by 1 and re-creates them in the new directory. This wouldnt work with .tif images.

    You can still use the above code to list all the images in a directory. Ill have a think about how to move these files and get back to you..
    It could work if you can read the encoding for tiff images: Java Advanced Imaging API Home Page

    It's not that hard to implements it by the looks of it. (though i havent done anything with image manipulating yet, but this can be a good time to try out)