Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
System.out.println("Copied Successfully"); } catch (IOException e) { e.printStackTrace(); } } } This part of the copyFile method does the work. byte[] readData = new byte[1024]; try (InputStream inputStream = Files.newInputStream(originPath, StandardOpenOption.READ); OutputStream outputStream = Files.newOutputStream(destinationPath, StandardOpenOption.CREATE)) { int i = inputStream.read(readData); while (i != -1) { outputStream.write(readData, 0, i); i = inputStream.read(readData); } } catch (IOException e) { throw e; } The readData byte array is used to store the data read from the InputStream. The number of bytes read is assigned to i. The code then calls the write method on the OutputStream, passing the byte array and i as the third argument. outputStream.write(readData, 0, i); Writing Text (Characters) The abstract class Writer defines a stream used for writing characters. Figure 13.3 shows the implementations of Writer.