It seems there are different ways to read and write data of files in Java.

I want to read ASCII data from a file. What are the possible ways and their differences?

derek5. This is the simplest way to do this: mkyong.com/java/…
5 年多之前 回复
lrony* it's so interesting that there is nothing like "read()" in python , to read the whole file to a string

MAO-EYE Without loops: {{{ Scanner sc = new Scanner(file, "UTF-8"); sc.useDelimiter("\$^"); // regex matching nothing String text = sc.next(); sc.close(); }}}
6 年多之前 回复

6 年多之前 回复

24个回答

ASCII is a TEXT file so you would use Readers for reading. Java also supports reading from a binary file using InputStreams. If the files being read are huge then you would want to use a BufferedReader on top of a FileReader to improve read performance.

In Java 7:

new String(Files.readAllBytes(...))


(docs) or

Files.readAllLines(...)


(docs)

In Java 8:

Files.lines(..).forEach(...)


(docs)

python小菜 by the way the link to the book "Thinking In Java" is no longer working!
2 年多之前 回复

4 年多之前 回复
from.. this is interesting, but can you quote from the docs where it says that Files.lines(...).forEach(...) is executed in parallel? I thought this was only the case when you explicitly make the stream parallel using Files.lines(...).parallel().forEach(...).
4 年多之前 回复

5 年多之前 回复

derek5. Will the line order be preserved when using "Files.lines(..).forEach(...)". My understanding is that the order will be arbitrary after this operation.

MAO-EYE I guess @Vlad is referring to the method of reading entire file into memory explained here java2s.com/Tutorial/Java/0180__File/…

lrony* Picking a Reader really depends on what you need the content of the file for. If the file is small(ish) and you need it all, it's faster (benchmarked by us: 1.8-2x) to just use a FileReader and read everything (or at least large enough chunks). If you're processing it line by line then go for the BufferedReader.

My favorite way to read a small file is to use a BufferedReader and a StringBuilder. It is very simple and to the point (though not particularly effective, but good enough for most cases):

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();

while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
}
String everything = sb.toString();
} finally {
br.close();
}


Some has pointed out that after Java 7 you should use try-with-resources (i.e. auto close) features:

try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
StringBuilder sb = new StringBuilder();

while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
}
String everything = sb.toString();
}


When I read strings like this, I usually want to do some string handling per line anyways, so then I go for this implementation.

Though if I want to actually just read a file into a String, I always use Apache Commons IO with the class IOUtils.toString() method. You can have a look at the source here:

http://www.docjar.com/html/api/org/apache/commons/io/IOUtils.java.html

FileInputStream inputStream = new FileInputStream("foo.txt");
try {
String everything = IOUtils.toString(inputStream);
} finally {
inputStream.close();
}


And even simpler with Java 7:

try(FileInputStream inputStream = new FileInputStream("foo.txt")) {
String everything = IOUtils.toString(inputStream);
// do something with everything string
}


3 年多之前 回复
10.24 shouldnt we close file input stream in last one you showed? I think its better to close the file input since there might be starters also here.

YaoRaoLov there is no need to use readers directly and also no need for ioutils. java7 has built in methods to read an entire file/all lines: See docs.oracle.com/javase/7/docs/api/java/nio/file/… and docs.oracle.com/javase/7/docs/api/java/nio/file/…
6 年多之前 回复
ℙℕℤℝ I haven't added exception handling intentionally as you as the developer should always do the error handling that is proper for your application. Your compiler will assist those that does not see this themselves.
6 年多之前 回复

6 年多之前 回复

6 年多之前 回复

hurriedly% For performance always call sb.append('\n') in preference to sb.append("\n") as a char is appended to the StringBuilder faster than a String

hurriedly% Similar to Apache Common IO IOUtils#toString() is sun.misc.IOUtils#readFully(), which is included in the Sun/Oracle JREs.

7*4 I've made a small adjustment to stop adding a newline ( \n ) if the last line is reached. code while (line != null) { sb.append(line); line = br.readLine(); // Only add new line when curline is NOT the last line.. if(line != null) { sb.append("\n"); } }code
7 年多之前 回复

The easiest way is to use the Scanner class in Java and the FileReader object. Simple example:

Scanner in = new Scanner(new FileReader("filename.txt"));


Scanner has several methods for reading in strings, numbers, etc... You can look for more information on this on the Java documentation page.

For example reading the whole content into a String:

StringBuilder sb = new StringBuilder();
while(in.hasNext()) {
sb.append(in.next());
}
in.close();
outString = sb.toString();


Also if you need a specific encoding you can use this instead of FileReader:

new InputStreamReader(new FileInputStream(fileUtf8), StandardCharsets.UTF_8)


derek5. not so efficient like BufferedReader

larry*wei while (in.hasNext()) { System.out.println (in.next()); }
6 年多之前 回复

Here are the three working and tested methods:

### Using BufferedReader

package io;
import java.io.*;
public static void main(String[] args)throws Exception {
File file = new File("C:\\Users\\pankaj\\Desktop\\test.java");
String st;
System.out.println(st);
}
}
}


### Using Scanner

package io;

import java.io.File;
import java.util.Scanner;

public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\pankaj\\Desktop\\test.java");
Scanner sc = new Scanner(file);
while(sc.hasNextLine()){
System.out.println(sc.nextLine());
}
}
}


### Using FileReader

package io;
import java.io.*;

public static void main(String[] args) throws Exception {
int i;
System.out.print((char) i);
}
}
}


### Read the entire file without a loop using the Scanner class

package io;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public static void main(String[] args) throws FileNotFoundException {
File file = new File("C:\\Users\\pankaj\\Desktop\\test.java");
Scanner sc = new Scanner(file);
sc.useDelimiter("\\Z");
System.out.println(sc.next());
}
}


7*4 How to give path if the folders are present inside the project?
3 年多之前 回复

This is basically the exact same as Jesus Ramos' answer, except with File instead of FileReader plus iteration to step through the contents of the file.

Scanner in = new Scanner(new File("filename.txt"));

while (in.hasNext()) { // Iterates each line in the file
String line = in.nextLine();
// Do something with line
}

in.close(); // Don't forget to close resource leaks


... throws FileNotFoundException

3 年多之前 回复

Here is a simple solution:

String content;


℡Wang Yan The best and simplest.

YaoRaoLov Jr, elegant and simple
2 年多之前 回复
from.. This is mentioned in the accepted answer.
5 年多之前 回复

This might not be the exact answer to the question. It's just another way of reading a file where you do not explicitly specify the path to your file in your Java code and instead, you read it as a command-line argument.

With the following code,

import java.io.BufferedReader;
import java.io.IOException;

public static void main(String[] args)throws IOException{
String s="";
System.out.println(s);
}
}
}


just go ahead and run it with:

java InputReader < input.txt


This would read the contents of the input.txt and print it to the your console.

You can also make your System.out.println() to write to a specific file through the command line as follows:

java InputReader < input.txt > output.txt


This would read from input.txt and write to output.txt.

You can use readAllLines and the join method to get whole file content in one line:

String str = String.join("\n",Files.readAllLines(Paths.get("e:\\text.txt")));


It uses UTF-8 encoding by default, which reads ASCII data correctly.

String str = new String(Files.readAllBytes(Paths.get("e:\\text.txt")), StandardCharsets.UTF_8);


I think readAllBytes is faster and more precise, because it does not replace new line with \n and also new line may be \r\n. It is depending on your needs which one is suitable.

The methods within org.apache.commons.io.FileUtils may also be very handy, e.g.:

/**
* Reads the contents of a file line by line to a List
* of Strings using the default encoding for the VM.
*/

3 年多之前 回复
10.24 or you simply use the built in method to get all lines: docs.oracle.com/javase/7/docs/api/java/nio/file/…
6 年多之前 回复

6 年多之前 回复

Below is a one-liner of doing it in the Java 8 way. Assuming text.txt file is in the root of the project directory of the Eclipse.

Files.lines(Paths.get("text.txt")).collect(Collectors.toList());
`