- 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 I create a Java string from the contents of a file?
You can read contents of a file using the read() method of the class FileInputStream. To this method you need to pass a byte array as a parameter to which the contents of the file are to be read.
To convert a byte array to Sting simply pass the byte array as a parameter to the constructor of the String class.
Example
import java.io.File; import java.io.FileInputStream; public class Demo { public static void main(String args[]) throws Exception { File file = new File("data.txt"); FileInputStream fis = new FileInputStream(file); byte[] bytesArray = new byte[(int)file.length()]; fis.read(bytesArray); String s = new String(bytesArray); System.out.println(s); } }
Output
Krishna Kasyap
- Related Articles
- How do we create a string from the contents of a file in java?
- Java Program to Create String from Contents of a File
- How do I create a .pyc file in Python?
- How to read the contents of a JSON file using Java?
- How do I wrap a string in a file in Python?
- How to store the contents of arrays in a file using Java?
- PHP: How do I display the contents of a textfile on my page?
- 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 do I create a java.sql.Date object in Java?
- How to create a string from a Java ArrayList?
- How to create a string from a Java Array?
- How do I remove a substring from the end of a string in Python?
- C# Program to read contents of a file into a string at once
- How do I create a random alpha-numeric string using C++?

Advertisements