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 ............







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~