Different Ways of Reading a Text File in JavaIn Java, there are various methods for creating and accessing text files. It is necessary to do this when working with numerous apps. Java has multiple methods for reading plain text files, such as FileReader, BufferedReader, and Scanner. Each utility offers a unique feature; for example, BufferedReader buffers data for quick reading, whereas Scanner has parsing capabilities. There are different approaches for reading of text file in java. They are as follows.
In Java, we can also read a text file line by line by using Scanner and BufferReader. Next, java.util.stream.Stream, a new Stream class introduced in Java SE 8, offers a more effective and slow method of reading a file. Approach: BufferedReader class for Reading text fileText is read using this method from a character input stream. It does have a buffer to make reading characters, arrays, and lines more efficient. One can either utilise the default size of the buffer or specify the size. For most uses, the default value is sufficient. Typically, every read request issued to a reader prompts an equivalent read request to be issued to the character or byte stream underneath it. Therefore, as seen below, it is recommended to wrap any Reader, including FileReaders and InputStreamReaders, whose read() operations may be expensive, in a BufferedReader: Syntax: Implementation:FileName: ReadingTextFile.java Output: Hello World Welcome to the house Have a wonderful day Approach: FileReader class for Reading text fileA realistic course on reading character files. The constructors of this class operate under the assumption that the default byte-buffer size and character encoding are suitable. The following constructors are defined in this class:
Implementation:FileName: ReadingTextFile1.java Output: Hello World Welcome to the house Have a wonderful day Approach: Scanner class for reading text fileA straightforward text scanner that uses regular expressions to interpret strings and primitive types. By matching whitespace as the default delimiter pattern, a scanner divides its input into tokens. The many subsequent methods can then be used to transform the generated tokens into values of various sorts. Case 1: With Using loopsImplementation: FileName: ReadingTextFile2.java Output: Hello World Welcome to the house Have a wonderful day Case 2: Without Using loopsImplementation: FileName: ReadingTextFile3.java Output: Hello World Welcome to the house Have a wonderful day Approach: Reading the whole file in a ListOpen a file and read every line. By using this method, you may be guaranteed that the file will close either when all the bytes have been read or if a runtime exception or I/O fault is raised. The chosen charset is used to decode the file's bytes into characters. Syntax: Note: The following are recognised as line terminators by the above method: Implementation:FileName: ReadingTextFile4.java Output: Hello World Welcome to the house Have a wonderful day Approach: Read a text file as StringJava's File Class's readString() function is used to read content from the provided file. Syntax: Return Value: The file's content is returned by this method in String format. Note: Since Java 11 was released, files have been read into String using the File.readString() method.Implementation:FileName: ReadingTextFile5.java Output: Hello World Welcome to the house Have a wonderful day
Next TopicEmpty Array in Java
|