Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java Io

  • 28-10-2003 07:12PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 68,173 ✭✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 919 ✭✭✭timeout


    Thanks that worked fine :D

    Timeout


Advertisement