|
| 1 | +package com.mkyong.io.api.inputstream; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.nio.charset.StandardCharsets; |
| 5 | + |
| 6 | +public class FileInputStreamExample { |
| 7 | + |
| 8 | + public static void main(String[] args) { |
| 9 | + String file = "c:\\test\\file.txt"; |
| 10 | + String fileUnicode = "c:\\test\\file-unicode.txt"; |
| 11 | + |
| 12 | + readFile(file); |
| 13 | + //readFileBetterPerformance(fileUnicode); |
| 14 | + //readFileBetterInputStreamReader(fileUnicode); |
| 15 | + |
| 16 | + } |
| 17 | + |
| 18 | + private static void readFile(String fileName) { |
| 19 | + |
| 20 | + try (FileInputStream fis = new FileInputStream(new File(fileName))) { |
| 21 | + |
| 22 | + // remaining bytes that can be read |
| 23 | + System.out.println("Remaining bytes that can be read : " + fis.available()); |
| 24 | + |
| 25 | + int content; |
| 26 | + // reads a byte at a time, if end of the file, returns -1 |
| 27 | + while ((content = fis.read()) != -1) { |
| 28 | + System.out.println((char) content); |
| 29 | + |
| 30 | + System.out.println("Remaining bytes that can be read : " + fis.available()); |
| 31 | + } |
| 32 | + } catch (IOException e) { |
| 33 | + e.printStackTrace(); |
| 34 | + } |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + private static void readFileBetterPerformance(String fileName) { |
| 39 | + |
| 40 | + try (FileInputStream fis = new FileInputStream(new File(fileName))) { |
| 41 | + |
| 42 | + // remaining bytes that can be read |
| 43 | + System.out.println("Remaining bytes that can be read : " + fis.available()); |
| 44 | + |
| 45 | + // 8k a time |
| 46 | + byte[] bytes = new byte[8192]; |
| 47 | + |
| 48 | + // reads 8192 bytes at a time, if end of the file, returns -1 |
| 49 | + while (fis.read(bytes) != -1) { |
| 50 | + |
| 51 | + // convert bytes to string for demo |
| 52 | + // also convert unicode to string |
| 53 | + System.out.println(new String(bytes, StandardCharsets.UTF_8)); |
| 54 | + |
| 55 | + System.out.println("Remaining bytes that can be read : " + fis.available()); |
| 56 | + } |
| 57 | + } catch (IOException e) { |
| 58 | + e.printStackTrace(); |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private static void readFileBetterPerformance2(String fileName) { |
| 64 | + |
| 65 | + try (BufferedInputStream bis = |
| 66 | + new BufferedInputStream( |
| 67 | + new FileInputStream(new File(fileName)))) { |
| 68 | + |
| 69 | + // remaining bytes that can be read |
| 70 | + System.out.println("Remaining bytes that can be read : " + bis.available()); |
| 71 | + |
| 72 | + int content; |
| 73 | + // reads 8192 bytes at a time and buffers them until they are needed, |
| 74 | + // if end of the file, returns -1 |
| 75 | + while ((content = bis.read()) != -1) { |
| 76 | + |
| 77 | + // convert bytes to string for demo |
| 78 | + System.out.println((char) content); |
| 79 | + |
| 80 | + System.out.println("Remaining bytes that can be read : " + bis.available()); |
| 81 | + } |
| 82 | + } catch (IOException e) { |
| 83 | + e.printStackTrace(); |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + private static void readFileBetterInputStreamReader(String fileName) { |
| 89 | + |
| 90 | + try (BufferedReader br = |
| 91 | + new BufferedReader( |
| 92 | + new InputStreamReader( |
| 93 | + new FileInputStream(new File(fileName))))) { |
| 94 | + |
| 95 | + String line; |
| 96 | + while ((line = br.readLine()) != null) { |
| 97 | + System.out.println(line); |
| 98 | + } |
| 99 | + |
| 100 | + } catch (IOException e) { |
| 101 | + e.printStackTrace(); |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | +} |
0 commit comments