I've been using the idiom below for some time now. And it seems to be the most wide-spread, at least on the sites I've visited.
Is there a better/different way to read a file into a string in Java?
private String readFile(String file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader (file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
try {
while((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
return stringBuilder.toString();
} finally {
reader.close();
}
}
转载于:https://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file
Commons FileUtils.readFileToString
:
public static String readFileToString(File file) throws IOException
Reads the contents of a file into a String using the default encoding for the VM. The file is always closed.
Parameters:
file
- the file to read, must not be nullReturns: the file contents, never null
Throws: -
IOException
- in case of an I/O errorSince: Commons IO 1.3.1
The code used (indirectly) by that class is:
IOUtils.java under Apache Licence 2.0.
public static long copyLarge(InputStream input, OutputStream output)
throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
It is very similar to the one used by Ritche_W.
If you're looking for an alternative that doesn't involve a third-party library (e.g. Commons I/O), you can use the Scanner class:
private String readFile(String pathname) throws IOException {
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int)file.length());
Scanner scanner = new Scanner(file);
String lineSeparator = System.getProperty("line.separator");
try {
while(scanner.hasNextLine()) {
fileContents.append(scanner.nextLine() + lineSeparator);
}
return fileContents.toString();
} finally {
scanner.close();
}
}
If you're looking for an alternative that doesn't involve a third-party library (e.g. Commons I/O), you can use the Scanner class:
private String readFile(String pathname) throws IOException {
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int)file.length());
Scanner scanner = new Scanner(file);
String lineSeparator = System.getProperty("line.separator");
try {
while(scanner.hasNextLine()) {
fileContents.append(scanner.nextLine() + lineSeparator);
}
return fileContents.toString();
} finally {
scanner.close();
}
}
From this page a very lean solution:
Scanner scanner = new Scanner( new File("poem.txt") );
String text = scanner.useDelimiter("\\A").next();
scanner.close(); // Put this call in a finally block
or
Scanner scanner = new Scanner( new File("poem.txt"), "UTF-8" );
String text = scanner.useDelimiter("\\A").next();
scanner.close(); // Put this call in a finally block
If you want to set the charset
String content = new String(Files.readAllBytes(Paths.get("readMe.txt")), "UTF-8");
since java 7 you can do it this way.