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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Io

  • 28-10-2003 6: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,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, 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