|
| 1 | +package io; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +/** |
| 8 | + * |
| 9 | + * @author chengfeili |
| 10 | + * Jun 13, 2017 11:03:58 PM |
| 11 | + * |
| 12 | + */ |
| 13 | +public class StreamsContinue { |
| 14 | + // FileInputStream |
| 15 | + // FileOutputStream |
| 16 | + public void fileInputOutputStream() throws IOException { |
| 17 | + File source = new File("Zoo.class"); |
| 18 | + File destination = new File("ZooCopy.class"); |
| 19 | + try (InputStream in = new FileInputStream(source); |
| 20 | + OutputStream out = new FileOutputStream(destination)) { |
| 21 | + int b; |
| 22 | + while ((b = in.read()) != -1) { |
| 23 | + out.write(b); |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Instead of reading the data one byte at a time, we use the underlying |
| 30 | + * read(byte[]) method of BufferedInputStream , which returns the number of |
| 31 | + * bytes read into the provided byte array. |
| 32 | + * |
| 33 | + * Why use the buffered classes? |
| 34 | + * |
| 35 | + * The BufferedInputStream class is capable of retrieving and storing in |
| 36 | + * memory more data than you might request with a single read() call. For |
| 37 | + * successive calls to the read() method with small byte arrays, this would |
| 38 | + * be faster in a wide variety of situations, since the data can be returned |
| 39 | + * directly from memory without going to the file system. |
| 40 | + */ |
| 41 | + public void bufferedInputOutputStream() throws IOException { |
| 42 | + File source = new File("Zoo.class"); |
| 43 | + File destination = new File("ZooCopy.class"); |
| 44 | + try (InputStream in = new BufferedInputStream(new FileInputStream(source)); |
| 45 | + OutputStream out = new BufferedOutputStream(new FileOutputStream(destination))) { |
| 46 | + byte[] buffer = new byte[1024]; |
| 47 | + int lengthRead; |
| 48 | + while ((lengthRead = in.read(buffer)) > 0) { |
| 49 | + out.write(buffer, 0, lengthRead); |
| 50 | + // Ensure that the written data actually makes it to disk before |
| 51 | + // the next buffer of data is read. |
| 52 | + out.flush(); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public void fileReaderWiter() throws IOException { |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + // BufferedReader |
| 62 | + public static List<String> readFile(File source) throws IOException { |
| 63 | + List<String> data = new ArrayList<String>(); |
| 64 | + try (BufferedReader reader = new BufferedReader(new FileReader(source))) { |
| 65 | + String s; |
| 66 | + while ((s = reader.readLine()) != null) { |
| 67 | + data.add(s); |
| 68 | + } |
| 69 | + } |
| 70 | + return data; |
| 71 | + } |
| 72 | + |
| 73 | + // BufferedWriter |
| 74 | + public static void writeFile(List<String> data, File destination) throws IOException { |
| 75 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter(destination))) { |
| 76 | + for (String s : data) { |
| 77 | + writer.write(s); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static void main(String[] args) throws IOException { |
| 83 | + |
| 84 | + } |
| 85 | +} |
0 commit comments