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
............
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….. 🙂
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();
Nice mkyong
Thanks its good
Thanks.. its good..
hmmmmmmmmm very nice and interesting article carry on man…
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)
Hi sam,
Thanks for sharing your FileUtils tip , i admit it’s a very handy class for Java IO. Looking into the “readFileToString” source, it copy the char buffer to Writer and convert it to StringWriter. Nice trick~