Java IO Tutorial

How to assign file content into a variable in Java

Most people will read the file content and assign to StringBuffer or String line by line. Here’s another trick that may interest you – how to assign whole file content into a variable with one Java’s statement, try it 🙂

Example

In this example, you will use DataInputStreamto convert all the content into bytes, and create a String variable with the converted bytes.


package com.mkyong.io;

import java.io.DataInputStream;
import java.io.FileInputStream;

public class App{

	public static void main (String args[]) {

	try{
		
	         DataInputStream dis = 
		    new DataInputStream (
		    	 new FileInputStream ("c:\\logging.log"));
		       
		 byte[] datainBytes = new byte[dis.available()];
		 dis.readFully(datainBytes);
		 dis.close();
		       
		 String content = new String(datainBytes, 0, datainBytes.length);
		     
		 System.out.println(content);
		 
	}catch(Exception ex){
		ex.printStackTrace();
	}
		
  }
}

Output

This will print out all the “logging.log” file content.


10:21:29,425  INFO Version:15 - Hibernate Annotations 3.3.0.GA
10:21:29,441  INFO Environment:509 - Hibernate 3.2.3
10:21:29,441  INFO Environment:542 - hibernate.properties not found
10:21:29,456  INFO Environment:676 - Bytecode provider name : cglib
10:21:29,456  INFO Environment:593 - using JDK 1.4 java.sql.Timestamp handling
............

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Haseena Ahmed
9 years ago

I’m doing an assignment for my uni and was stuck in the file I/o part for weeks…. n finally with d help f ur article im moving forward…. thanx alooooot for this….. 🙂

woo37830
9 years ago

I have a problem that should be simple. I must be missing something fundamental. I create a String viz.
String text = “u0391u039Au03C1u1F83uA978u0370u03D8u03C0”;
If I use that as the argument to a JLabel with the default font, it prints Greek characters as I’d like.
However, IF I put that same string, with no quotes, of course, into a text file and read that line in and assign the line to a JLabel, I get the “uninterpreted” string. It looks just like it is in the file. I’ve tried using different encodings to read the file like ISO-8859-1, UTF-8, UTF-16, etc. to no avail.
How do I get the read in text to be just like an assigned piece of text?
Try
String text = “u0391u039Au03C1u1F83uA978u0370u03D8u03C0”;
System.out.println( “As ascii text = ” + text );
and
String label = new String( readLabel( directory + File.separator
+ “testReadLabel.txt” ) );
System.out.println( “As ascii label = ” + label );

When testReadLabel.txt contains the text above and the read is a standard

BufferedReader br = new BufferedReader( new InputStreamReader(
new FileInputStream( fileName ), “ISO-8859-1” ) );

return br.readLine();

Rahulsingh
10 years ago

Nice mkyong

balakrishnagajam
11 years ago

Thanks its good

balakrishna gajam
11 years ago

Thanks.. its good..

Nommi
14 years ago

hmmmmmmmmm very nice and interesting article carry on man…

Sam
14 years ago

That’s not really what the DataInputStream is for. Reading the javadoc is worthwhile.

I’d also suggest using jakarta commons io, which provides this in the form of:
FileUtils.readFileToString(File file)
or
FileUtils.readFileToString(File file, String encoding)