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 3 of 3

Thread: How to move files from one directory to another considering its size

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to move files from one directory to another considering its size

    Hello, i am newbie in this forum, forgive me in case if i give my query in wrong place,

    My query is i have a directory consists of few files with different memory size, now i want to move the files of les than 10mb toanother directory path

    i have written the code, please lemme know whats wrong there in it, i am getting an exception ..Exception in thread "main" java.io.FileNotFoundException: /home/dev06/temporaryfolder/home/dev06/sourcefolder/b (No such file or directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.j ava:212)
    at java.io.FileOutputStream.<init>(FileOutputStream.j ava:104)
    at App.checking(App.java:22)
    at main.main(main.java:6)

    my i ve writen the code .. i do have the files name a, b

    public class App {
    int count = 0;
    File source = new File("/home/dev06/sourcefolder/");
    File destination = new File("/home/dev06/temporaryfolder");

    public void checking() throws IOException {
    for (File files : source.listFiles()) {
    if (files.isFile()) {
    // count++;
    long filesize = files.length();
    if (filesize > 10) {
    //System.out.println(files.getName() + " and the size is "
    // + filesize + "bytes");
    InputStream in = new FileInputStream(files);
    OutputStream out = new FileOutputStream(destination + "/"
    + files);
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
    }
    }
    }
    }
    //System.out.println("no of files " + count);
    }
    }


    Thank you


  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to move files from one directory to another considering its size

    i am sorry, i found the logic

    that is


    public class App1 {

    File source = new File("/home/dev06/sourcefolder");
    File destination = new File("/home/dev06/temporaryfolder");

    public void checking() throws IOException {
    for (File files : source.listFiles()) {
    System.out.println(files.getName() + " : " + files.length());
    if(files.length()>50){
    System.out.println(files.getName() + " : " + files.length());
    files.renameTo(destination);
    files.delete();
    }
    }
    }

    public static void main(String[] args) throws Exception {
    App1 a = new App1();
    a.checking();
    }
    }

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to move files from one directory to another considering its size

    Good Luck and Mark it as SOLVED.

Similar Threads

  1. Java program which can list all files in a given directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 8
    Last Post: July 9th, 2013, 04:38 AM
  2. Moving files in a Directory
    By Sai in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 23rd, 2010, 05:49 AM
  3. How to read from all files in a directory & plot the contents?
    By 123 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2010, 12:43 PM
  4. Java program which can list all files in a given directory
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 5
    Last Post: November 3rd, 2009, 09:42 AM