|
| 1 | +--- |
| 2 | +layout:post |
| 3 | +title:"How To Read File Using BufferReader In Java?" |
| 4 | +author:gaurav |
| 5 | +image:assets/images/2021-01-03/read-file-using-bufferreader.png |
| 6 | +categories:[Java, Java File IO] |
| 7 | +description:"In this article we will see how to read a file using the `BufferReader` class in Java." |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +In this article we will see how to read a file using the`BufferReader`class in Java. |
| 12 | + |
| 13 | +`BufferReader` class reads text from a character-input stream. Because of buffering characters it provides an efficient way to read characters, arrays, and lines. |
| 14 | + |
| 15 | +`BufferReader` provides two important methods to read from the file. i.e`read()` and`readLine()`. |
| 16 | + |
| 17 | +You can specify the bufferSize in`BufferReader`constructer. But as[motioned in the docs](https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html), |
| 18 | +>The default is large enough for most purposes. |
| 19 | +
|
| 20 | +##BufferReader`read()` method |
| 21 | + |
| 22 | +`BufferReader``read()` method reads a single character. IT returns the`int` representation of the char in range of 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached. |
| 23 | + |
| 24 | +We can cast`int` value returned by`read()` method to`char` to get the character value. |
| 25 | + |
| 26 | +I have given an example to read a file character by character using the`read()` method of the`BufferReader` class |
| 27 | +```java |
| 28 | +packagecom.coderolls; |
| 29 | + |
| 30 | +importjava.io.*; |
| 31 | + |
| 32 | +/** |
| 33 | + * A java program to read file character by character using the |
| 34 | + * read() method of the BufferReader Class. |
| 35 | + * |
| 36 | + *@author Gaurav Kukade at coderolls.com |
| 37 | +*/ |
| 38 | +publicclassBufferReaderReadMethodExample { |
| 39 | + |
| 40 | +publicstaticvoidmain(String[]args) { |
| 41 | + |
| 42 | +BufferedReader bufferedReader=null; |
| 43 | +try { |
| 44 | +bufferedReader=newBufferedReader(newFileReader("F:\\sample-text.txt")); |
| 45 | + |
| 46 | +int i; |
| 47 | +//read each character using read() method and print it |
| 48 | +while((i=bufferedReader.read())!=-1){ |
| 49 | +System.out.print((char)i); |
| 50 | +} |
| 51 | + |
| 52 | +}catch (IOException e) { |
| 53 | +e.printStackTrace(); |
| 54 | +}finally { |
| 55 | +try { |
| 56 | +bufferedReader.close(); |
| 57 | +}catch (IOException e) { |
| 58 | +e.printStackTrace(); |
| 59 | +} |
| 60 | +} |
| 61 | +} |
| 62 | +} |
| 63 | +``` |
| 64 | +Output |
| 65 | +``` |
| 66 | +Welcome to coderolls.com! |
| 67 | +``` |
| 68 | +See[this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferReaderReadMethodExample.java). |
| 69 | + |
| 70 | +##BufferReader`readLine()` method |
| 71 | +As specified in the name, this method reads a line of text. |
| 72 | + |
| 73 | +A line is considered to be terminated by any one of a line feed ('\n') or a carriage return ('\r'). |
| 74 | + |
| 75 | +The`readLine()` method returns the content of a line as string except the line terminating character (i.e.`\n` or`\r`). Or it will return`null` if the end of the stream has been reached. |
| 76 | + |
| 77 | +I have given below an example to read file line by line using the`readLine()` method |
| 78 | + |
| 79 | +```java |
| 80 | +packagecom.coderolls; |
| 81 | + |
| 82 | +importjava.io.*; |
| 83 | + |
| 84 | +/** |
| 85 | + * A java program to read file line by line using the |
| 86 | + * readLine() method of the BufferReader Class. |
| 87 | + * |
| 88 | + *@author Gaurav Kukade at coderolls.com |
| 89 | + * |
| 90 | +*/ |
| 91 | +publicclassBufferReaderReadLineMethodExample { |
| 92 | + |
| 93 | +publicstaticvoidmain(String[]args) { |
| 94 | + |
| 95 | +BufferedReader bufferedReader=null; |
| 96 | +try { |
| 97 | +bufferedReader=newBufferedReader(newFileReader("F:\\sample-text-two-lines.txt")); |
| 98 | + |
| 99 | +String line; |
| 100 | +//read each line using readLine() method and print it |
| 101 | +while((line= bufferedReader.readLine())!=null){ |
| 102 | +System.out.println(line); |
| 103 | +} |
| 104 | + |
| 105 | +}catch (IOException e) { |
| 106 | +e.printStackTrace(); |
| 107 | +}finally { |
| 108 | +try { |
| 109 | +bufferedReader.close(); |
| 110 | +}catch (IOException e) { |
| 111 | +e.printStackTrace(); |
| 112 | +} |
| 113 | +} |
| 114 | +} |
| 115 | +} |
| 116 | +``` |
| 117 | +Output |
| 118 | +```java |
| 119 | +Welcome to coderolls.com! |
| 120 | + |
| 121 | +Visit coderolls to read more coding tutorials! |
| 122 | +``` |
| 123 | +See[this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferReaderReadLineMethodExample.java). |
| 124 | + |
| 125 | +I have given below a combine example of the Java`BufferReader``read()` and`readLine()` method below |
| 126 | + |
| 127 | +```java |
| 128 | +packagecom.coderolls; |
| 129 | + |
| 130 | +importjava.io.*; |
| 131 | + |
| 132 | +publicclassBufferReaderExanple { |
| 133 | + |
| 134 | +publicstaticvoidmain(String[]args) { |
| 135 | +BufferedReader bufferedReader=null; |
| 136 | +try { |
| 137 | +bufferedReader=newBufferedReader(newFileReader("F:\\sample-text-two-lines.txt")); |
| 138 | + |
| 139 | +}catch (FileNotFoundException e) { |
| 140 | +// TODO Auto-generated catch block |
| 141 | +e.printStackTrace(); |
| 142 | +} |
| 143 | + |
| 144 | +readFileCharacterByCharacter(bufferedReader); |
| 145 | + |
| 146 | +readFileLineByLine(bufferedReader); |
| 147 | + |
| 148 | +try { |
| 149 | +bufferedReader.close(); |
| 150 | +}catch (IOException e) { |
| 151 | +e.printStackTrace(); |
| 152 | +} |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * A method to read file content character by character using the BufferReader |
| 157 | + * read() method |
| 158 | + * |
| 159 | + *@param bufferedReader |
| 160 | +*/ |
| 161 | +publicstaticvoidreadFileCharacterByCharacter(BufferedReaderbufferedReader) { |
| 162 | +try { |
| 163 | +int i; |
| 164 | +//read each character using read() method and print it |
| 165 | +while((i=bufferedReader.read())!=-1){ |
| 166 | +System.out.print((char)i); |
| 167 | +} |
| 168 | +}catch (IOException e) { |
| 169 | +e.printStackTrace(); |
| 170 | +} |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * A method to read file content line by line using the BufferReader |
| 175 | + * readLine() method |
| 176 | + * |
| 177 | + *@param bufferedReader |
| 178 | +*/ |
| 179 | +publicstaticvoidreadFileLineByLine(BufferedReaderbufferedReader) { |
| 180 | + |
| 181 | +try { |
| 182 | +String line; |
| 183 | +//read each line using readLine() method and print it |
| 184 | +while((line= bufferedReader.readLine())!=null){ |
| 185 | +System.out.println(line); |
| 186 | +} |
| 187 | +}catch (IOException e) { |
| 188 | +e.printStackTrace(); |
| 189 | +} |
| 190 | +} |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +See[this example on GitHub](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-files-io/BufferReaderExanple.java). |
| 195 | + |
| 196 | +##`newBufferedReader()` method in Java 8 |
| 197 | + |
| 198 | +In Java 1.8 and above you can get a`BufferReader` instance using the`newBufferedReader()` method of the`java.nio.file.Files` class. |
| 199 | + |
| 200 | +##Conclusion |
| 201 | + |
| 202 | +You can read file character by character using the`read()` method of the`BufferReader`Class. |
| 203 | + |
| 204 | +`read()` method returns an integer value, you have to cast it to`char` to get character value. |
| 205 | + |
| 206 | +Also, you can read file line by line using the`readLine()` method of the`BufferReader`Class |
| 207 | + |
| 208 | +`readLine()` methods returns the line content as string, except the line terminating character |
| 209 | + |
| 210 | +You can visit my[YouTube channel 'coderolls'](https://www.youtube.com/channel/UCl31HHUdQbSHOQfc9L-wo3w?view_as=subscriber?sub_confirmation=1) to find more video tutorials. |
| 211 | + |
| 212 | +-------------- |
| 213 | + |
| 214 | +You can support me by[giving a cup of Coffee ☕](https://www.paypal.me/GauravKukade) |
| 215 | + |
| 216 | +####Related Articles |
| 217 | + |
| 218 | +-[8 Basic GIT Commands Every Newbie Developer Must Know](https://coderolls.com/basic-git-commands/) |
| 219 | + |
| 220 | +-[How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/) |
| 221 | + |
| 222 | +-[How To Create UUID in Java?](https://coderolls.com/create-uuid-in-java/) |
| 223 | + |
| 224 | +-[Learn About Java String Pool And intern() Method](https://coderolls.com/java-string-pool-and-intern-method/) |
| 225 | +-[How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/) |
| 226 | + |
| 227 | +-[Compare Two Strings Lexicographically In Java](https://coderolls.com/compare-two-strings-lexicographically-in-java/) |
| 228 | +-[Difference Between StringBuffer and StringBuilder class](https://coderolls.com/difference-between-stringbuffer-and-stringbuilder/) |
| 229 | + |
| 230 | +-[8 Basic GIT Commands Every Newbie Developer Must Know](https://coderolls.com/basic-git-commands/) |
| 231 | + |
| 232 | +-[How To Create UUID in Java?](https://coderolls.com/create-uuid-in-java/) |