2008-10-22 57 views

回答

7

使用Apache Commons IOUtils包。特别是IOUtils类提供了一组方法,从流,读者等读取并处理所有的异常等

例如

InputStream is = ... 
String contents = IOUtils.toString(is); 
// or 
List lines = IOUtils.readLines(is) 
1
String content = (new RandomAccessFile(new File("test.txt"))).readUTF(); 

不幸的是Java是对源文件是有效的UTF8非常挑剔,虽然,否则你会得到一个EOFException类或UTFDataFormatException。

+0

你缺少一个右引号那里... – itsadok 2008-10-22 09:56:29

+0

我不认为这是一个事一个Java是“挑剔”关于你的readUTF(阅读文本),它只是你用错了。阅读:http://java.sun.com/javase/6/docs/api/java/io/DataInput.html#modified-utf-8 – 2008-10-22 10:01:03

0

我想你必须创建自己的函数。问题是,Java的读例程(我知道,至少)通常采用给定长度的缓冲区参数。

我看到的解决方案是获取文件的大小,创建一个这种大小的缓冲区并立即读取文件。希望该文件不是一个千兆字节日志或XML文件...

通常的方法是有一个固定大小的缓冲区或使用readLine并将结果连接在一个StringBuffer/StringBuilder中。

2

辅助功能。我基本上是用他们几个,视情况而定

  • 猫方法管道的InputStream到OutputStream
  • 方法调用猫一个ByteArrayOutputStream和提取字节数组,使整个文件的快速阅读到字节数组
  • 迭代器的实现<字符串>使用Reader构造;它把它包装在一个BufferedReader和readline的旁边()
  • ...

要么推出自己的或使用的东西出来的commons-io的或您的首选实用程序库。

1

为了让这样的辅助函数的例子:

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)

0

我不认为使用的BufferedReader阅读是一个好主意,因为BufferedReader只返回没有分隔符的行的内容,当行只包含换行符时,BR将返回一个空值,尽管它仍然没有到达流的末尾。

+0

号当看到BufferedReader中连续两个分隔的readLine()返回一个空的字符串。它只在到达文件结尾时才返回null。 – 2009-02-23 03:57:57

0

String org.apache.commons。 io.FileUtils.readFileToString(文件文件)

5

我想用扫描仪是关于Java的车载工具简洁相当OK:

Scanner s = new Scanner(new File("file")); 
StringBuilder builder = new StringBuilder(); 
while(s.hasNextLine()) builder.append(s.nextLine()); 

此外,它是相当灵活的,太(如正则表达式支持,数字解析)。

0

从这里挑一个。

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

0

或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构造。

相关问题