Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Io

Options
  • 28-10-2003 7:12pm
    #1
    Registered Users Posts: 919 ✭✭✭


    Hopefully a quick one to answer but what JavaIO class do I use to copy general files.

    What i mean is i know how to open a stream and get it to read a file and output to another.

    (just simple code, plz don't comment on too much)
    public void copy(file a, file b){
    bufferreader in = new bufferreader(a);
    bufferwriter out = new bufferwriter(b);

    while out != null
    out.append();
    }

    The problem is this is only text, like a backup program. What I want to do is be able to copy any file type i.e. .txt, .wav,.avi,.pdf,.doc ect.

    Any help welcomed good or bad,
    Timeout


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Copying a file byte for byte should work fine. Whichever Reader reads the file as bytes, and whichever Writer writes the file as bytes should do the trick.

    Alternatively, have a look at the File class, and it's associated FileReader and FileWriter classes, they may have it already written for you (and probably do).


  • Registered Users Posts: 1,038 ✭✭✭rob1891


    URL answer = google("java copy file");

    I crack myself up! Have a read of:

    http://www.oreilly.com/catalog/jenut/examples/FileCopy.java

    It's heavily commented, the action is here:
        FileInputStream from = null;  // Stream to read from source
        FileOutputStream to = null;   // Stream to write to destination
        try {
          from = new FileInputStream(from_file);  // Create input stream
          to = new FileOutputStream(to_file);     // Create output stream
          byte[] buffer = new byte[4096];         // A buffer to hold file contents
          int bytes_read;                         // How many bytes in buffer
          // Read a chunk of bytes into the buffer, then write them out, 
          // looping until we reach the end of the file (when read() returns -1).
          // Note the combination of assignment and comparison in this while
          // loop.  This is a common I/O programming idiom.
          while((bytes_read = from.read(buffer)) != -1) // Read bytes until EOF
            to.write(buffer, 0, bytes_read);            //   write bytes 
        }
        // Always close the streams, even if exceptions were thrown
        finally {
          if (from != null) try { from.close(); } catch (IOException e) { ; }
          if (to != null) try { to.close(); } catch (IOException e) { ; }
        }
    

    Rob


  • Registered Users Posts: 919 ✭✭✭timeout


    Thanks that worked fine :D

    Timeout


Advertisement