在Java中读取文件或输入流内容的最简洁方法是什么?我是否总是需要创建一个缓冲区,逐行读取(最多),或者有更简洁的方法?我希望我能做到这在Java中读取文件/输入流内容的最简洁的方法是什么?
String content = new File("test.txt").readFully();
在Java中读取文件或输入流内容的最简洁方法是什么?我是否总是需要创建一个缓冲区,逐行读取(最多),或者有更简洁的方法?我希望我能做到这在Java中读取文件/输入流内容的最简洁的方法是什么?
String content = new File("test.txt").readFully();
使用Apache Commons IOUtils包。特别是IOUtils
类提供了一组方法,从流,读者等读取并处理所有的异常等
例如
InputStream is = ...
String contents = IOUtils.toString(is);
// or
List lines = IOUtils.readLines(is)
String content = (new RandomAccessFile(new File("test.txt"))).readUTF();
不幸的是Java是对源文件是有效的UTF8非常挑剔,虽然,否则你会得到一个EOFException类或UTFDataFormatException。
我想你必须创建自己的函数。问题是,Java的读例程(我知道,至少)通常采用给定长度的缓冲区参数。
我看到的解决方案是获取文件的大小,创建一个这种大小的缓冲区并立即读取文件。希望该文件不是一个千兆字节日志或XML文件...
通常的方法是有一个固定大小的缓冲区或使用readLine并将结果连接在一个StringBuffer/StringBuilder中。
辅助功能。我基本上是用他们几个,视情况而定
要么推出自己的或使用的东西出来的commons-io的或您的首选实用程序库。
为了让这样的辅助函数的例子:
String[] lines = NioUtils.readInFile(componentxml);
,关键是要尝试关闭即使抛出IOException的BufferedReader中。
/**
* Read lines in a file. <br />
* File must exist
* @param f file to be read
* @return array of lines, empty if file empty
* @throws IOException if prb during access or closing of the file
*/
public static String[] readInFile(final File f) throws IOException
{
final ArrayList lines = new ArrayList();
IOException anioe = null;
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(f));
String line;
line = br.readLine();
while(line != null)
{
lines.add(line);
line = br.readLine();
}
br.close();
br = null;
}
catch (final IOException e)
{
anioe = e;
}
finally
{
if(br != null)
{
try {
br.close();
} catch (final IOException e) {
anioe = e;
}
}
if(anioe != null)
{
throw anioe;
}
}
final String[] myStrings = new String[lines.size()];
//myStrings = lines.toArray(myStrings);
System.arraycopy(lines.toArray(), 0, myStrings, 0, lines.size());
return myStrings;
}
(如果你只想要一个字符串,改变功能在Java5的每行一个StringBuffer(或StringBuilder的追加或6)
我不认为使用的BufferedReader阅读是一个好主意,因为BufferedReader只返回没有分隔符的行的内容,当行只包含换行符时,BR将返回一个空值,尽管它仍然没有到达流的末尾。
号当看到BufferedReader中连续两个分隔的readLine()返回一个空的字符串。它只在到达文件结尾时才返回null。 – 2009-02-23 03:57:57
String org.apache.commons。 io.FileUtils.readFileToString(文件文件)
我想用扫描仪是关于Java的车载工具简洁相当OK:
Scanner s = new Scanner(new File("file"));
StringBuilder builder = new StringBuilder();
while(s.hasNextLine()) builder.append(s.nextLine());
此外,它是相当灵活的,太(如正则表达式支持,数字解析)。
从这里挑一个。
How do I create a Java string from the contents of a file?
最喜欢的是:
private static String readFile(String path) throws IOException {
FileInputStream stream = new FileInputStream(new File(path));
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
/* Instead of using default, pass in a decoder. */
return CharSet.defaultCharset().decode(bb).toString();
}
finally {
stream.close();
}
}
发布者erickson
或Java 8路:
try {
String str = new String(Files.readAllBytes(Paths.get("myfile.txt")));
...
} catch (IOException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
人们可以通过适当的字符集的String构造。
你缺少一个右引号那里... – itsadok 2008-10-22 09:56:29
我不认为这是一个事一个Java是“挑剔”关于你的readUTF(阅读文本),它只是你用错了。阅读:http://java.sun.com/javase/6/docs/api/java/io/DataInput.html#modified-utf-8 – 2008-10-22 10:01:03