Skip to main content

Java read file (entire or line by line)

Four ways to read a file in java. I have tested an execution time of them using 1,37 MB text file with 108825 lines. Result of tests is in a first comment of each method and is specified in milliseconds.

1 Read entire file at once

1.1 FileInputStream with read()
// execution time: 7 - 8 ms
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
File file = new File("C:\\test.txt");
InputStream is = new FileInputStream(file);
byte[] temp = new byte[1024];
int read;
while ((read = is.read(temp)) >= 0) {
 buffer.write(temp, 0, read);
}
byte[] data = buffer.toByteArray();
System.out.println(new String(data));
PS: remember that read(byte[] b) doesn't do what you think it does -> link

1.2 FileInputStream with readFully()
// execution time: 11 - 12 ms
File file = new File("C:\\test.txt");
DataInput input = new DataInputStream(new FileInputStream(file));
byte[] bufferArray = new byte[(int) file.length()];
input.readFully(bufferArray);
String value = new String(bufferArray);

2 Read file line by line

2.1 BufferedReader with readLine()
// execution time: 19 - 20 ms
FileInputStream fStream = new FileInputStream("C:\\test.txt");
DataInputStream in = new DataInputStream(fStream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
 System.out.println(line);
}
2.2 Scanner with nextLine(
// execution time: 196 - 216 ms
File file = new File("c://test.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
scanner.nextLine();
 System.out.println(line);
}
scanner.close();

Comments

Popular posts from this blog

Deploying Axis2 web service as ROOT application on Tomcat

By default Axis2 services are available on http://host:8080/axis2/services/*. If you want to remove axis2/services part from the service endpoint and access your service by http://host:8080/serviceName/* 1. Overide default Axis2 servlet mapping services in web.xml file. AxisServlet /serviceName/* 2. Remove an application name value by deploying axis2 implementation (from war distribution axis2-1.X.X-war.zip ) as ROOT application. Info: War distribution contains application with above structure: axis2-web META-INF org WEB-INF     classes     conf     lib     modules     services         put here *.aar files to deploy WS     web.xml (manually created) web.xml file Apache-Axis2 AxisServlet Apache-Axis Servlet org.apache.axis2.transport.http.AxisServlet ...

Convert Source to String

import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import javax.xml.transform.Source; import javax.xml.transform.sax.SAXSource; import org.xml.sax.InputSource; public class DataSourceTest { public static void main(String[] args) throws IOException { // created Source InputSource inputSource = new InputSource(new StringReader( " \n \n \n ")); Source source = new SAXSource(inputSource); // read Source SAXSource saxSource = (SAXSource) source; InputSource inputSourceReader = saxSource.getInputSource(); Reader reader = inputSourceReader.getCharacterStream(); BufferedReader buffereRreader = new BufferedReader(reader); String read = null; while ((read = buffereRreader.readLine()) != null) { System.out.println(read); } } }