- 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
How do we create a string from the contents of a file in java?
In Java you can read the contents of a file in several ways one way is to read it to a string using the java.util.Scanner class, to do so,
Instantiate the Scanner class, with the path of the file to be read, as a parameter to its constructor.
Create an empty String buffer.
Start a while loop with condition, if the Scanner has next line. i.e. hasNextLine() at while.
Within the loop append each line of the file to the StringBuffer object using the append() method.
Convert the contents of the contents of the buffer to String using the toString() method.
Example
Create a file with name sample.txt in the C directory in your system, copy and paste the following content in it.
Tutorials Point is an E-learning company that set out on its journey to provide knowledge to that class of readers that responds better to online content. With Tutorials Point, you can learn at your own pace, in your own space. After a successful journey of providing the best learning content at tutorialspoint.com, we created our subscription based premium product called Tutorix to provide Simply Easy Learning in the best personalized way for K-12 students, and aspirants of competitive exams like IIT/JEE and NEET.
Following Java program reads the contents of the file sample.txt in to a String and prints it.
import java.io.File; import java.io.IOException; import java.util.Scanner; public class FileToString { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(new File("E://test//sample.txt")); String input; StringBuffer sb = new StringBuffer(); while (sc.hasNextLine()) { input = sc.nextLine(); sb.append(" "+input); } System.out.println("Contents of the file are: "+sb.toString()); } }
Output
Contents of the file are: Tutorials Point is an E-learning company that set out on its journey to provide knowledge to that class of readers that responds better to online content. With Tutorials Point, you can learn at your own pace, in your own space. After a successful journey of providing the best learning content at tutorialspoint.com, we created our subscription based premium product called Tutorix to provide Simply Easy Learning in the best personalized way for K-12 students, and aspirants of competitive exams like IIT/JEE and NEET.
- Related Articles
- How do I create a Java string from the contents of a file?
- Java Program to Create String from Contents of a File
- How do we create python string from list?
- How to store the contents of arrays in a file using Java?
- How to read the contents of a JSON file using Java?
- Can we use readUTF() to read a string from a .txt file in Java?
- How to write contents of a file to byte array in Java?
- How to read the contents of a webpage into a string in java?
- How to create a string from a Java ArrayList?
- How to create a string from a Java Array?
- How do we compare String in Java
- Print contents of a file in C
- C# Program to read contents of a file into a string at once
- How do you create a list from a set in Java?
- How to overwrite a file to hide file contents, and make original contents unrecoverable in Linux?
