Results 1 to 2 of 2

Thread: java file copy

  1. #1
    hknyo is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Default java file copy

    hello,
    I am trying to copy files from network to local disk. I have this working using URL and inputStream. my problem is that I want to throw exception for both connection failure and filenotfound . I tested with different scenerios and I could not get filenotfound exception. it keeps saying that connection error , here is a breif list of what i am doing

    -get file using url
    -check if the connection fail, if fail return error message
    -if connection is good, proceed and get file
    -if file does not exist on the source machine, throw filenotfound and return error message
    -if every goes well, copy over the file to localdisk.

    here is partial code

    ...
    host = "file:\\\\sourceMachine\\content\\BIGfile.big" ;
    URL url = new URL(host);
    URLConnection urlC = url.openConnection();
    // Copy resource to local file
    InputStream is = url.openStream();
    System.out.flush();
    FileOutputStream fos = null;

    String localFile = null;

    // Get only file name
    StringTokenizer st = new StringTokenizer(url.getFile(), "/");
    while (st.hasMoreTokens()) {
    localFile = st.nextToken();
    }

    fos = new FileOutputStream(localFile);

    byte[] buf = new byte[32768];
    int len;
    while ((len = is.read(buf)) > 0) {
    fos.write(buf, 0, len);
    }

    is.close();
    fos.close();

    } catch (IOException e) {

    e.printStackTrace()

    }

    everytime, i change the invalid machine name, it gives me unknownhost error(which is correct), but when I tried to change invalid filename it gives me conenction error (java.net.ConnectException: Connection refused: connect) but this is not what i want, i want file not found exception. anyone help me on this. thanks.

    Cheers

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 2
    Last Post: 10-05-2008, 06:13 PM
  2. java copy paste cut and undo functions
    By Mr tuition in forum AWT / Swing
    Replies: 1
    Last Post: 12-09-2007, 12:02 AM
  3. copy image/imageicon into a file on disk
    By archanajathan in forum Advanced Java
    Replies: 2
    Last Post: 11-22-2007, 06:21 AM
  4. Problem with array Copy
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:46 AM
  5. how to issue the command of Ctrl-C (copy) in Java
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-18-2007, 03:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •