Results 1 to 2 of 2

Thread: java file copy

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

    Default java file copy

    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();
    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 = > 0) {
    fos.write(buf, 0, len);


    } catch (IOException e) {



    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 ( Connection refused: connect) but this is not what i want, i want file not found exception. anyone help me on this. thanks.


  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Colombo, Sri Lanka
    Blog Entries
    Rep Power

Similar Threads

  1. Replies: 2
    Last Post: 10-05-2008, 07:13 PM
  2. java copy paste cut and undo functions
    By Mr tuition in forum AWT / Swing
    Replies: 1
    Last Post: 12-09-2007, 01:02 AM
  3. copy image/imageicon into a file on disk
    By archanajathan in forum Advanced Java
    Replies: 2
    Last Post: 11-22-2007, 07:21 AM
  4. Problem with array Copy
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08: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, 04: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