- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Create String from Contents of a File
The java.io.BufferedReader.readline() method read a line of text. A line is considered to be terminated by any one of a line feed ('
'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Example
Following program reads the contents of a file to a string variable −
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; public class BufferedReaderDemo { public static void main(String[] args) throws Exception { String thisLine = null; try { // open input stream test.txt for reading purpose. BufferedReader br = new BufferedReader("c:/test.txt"); while ((thisLine = br.readLine()) != null) { System.out.println(thisLine); } } catch(Exception e) { e.printStackTrace(); } } }
Assuming we have a text file c:/test.txt, which has the following content. This file will be used as an input for our example program −
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
Output
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
- Related Articles
- How do I create a Java string from the contents of a file?
- How do we create a string from the contents of a file in java?
- C# Program to read contents of a file into a string at once
- Golang Program to Read the Contents of a File
- Java Program to Convert contents of a file to byte array and Vice-Versa
- Java Program to create a boolean variable from string
- Java Program to create Stream from a String/Byte Array
- C program to copy the contents of one file to another file?
- Java Program to create a BigDecimal from a string type value
- How to read the contents of a JSON file using Java?
- Java Program to create Character Array from String Objects
- How to write contents of a file to byte array in Java?
- How to store the contents of arrays in a file using Java?
- C Program for copying the contents of one file into another file
- Python program to create a dictionary from a string

Advertisements