English 中文(简体)
归还档案案文,作为证据? [复制]
原标题:Return the text of a file as a string? [duplicate]
This question already has answers here:
Closed 12 years ago.

Possible Duplicate:
How to create a Java String from the contents of a file

Is it possible to process a multi-lined text file and return its contents as a string?

如果有可能,请向我说明情况。


如果你们需要更多的信息,我会与I/O玩.。 我想开一个文本文件,处理其内容,把这个文件作为“努力”,并将案文内容定在其中。

请像文字编辑。

最佳回答

Check the java tutorial here - http://download.oracle.com/javase/tutorial/essential/io/file.html

Path file = ...;
InputStream in = null;
StringBuffer cBuf = new StringBuffer();
try {
    in = file.newInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String line = null;

    while ((line = reader.readLine()) != null) {
        System.out.println(line);
        cBuf.append("
");
        cBuf.append(line);
    }
} catch (IOException x) {
    System.err.println(x);
} finally {
    if (in != null) in.close();
}
// cBuf.toString() will contain the entire file contents
return cBuf.toString();
问题回答

http://commons.apache.org/io/api-1.4/org/apache/commons/io/FileUtils.html#readFileToString%28java.io rel=“nofollow”

大致如下:

String result = "";

try {
  fis = new FileInputStream(file);
  bis = new BufferedInputStream(fis);
  dis = new DataInputStream(bis);

  while (dis.available() != 0) {

    // Here s where you get the lines from your file

    result += dis.readLine() + "
";
  }

  fis.close();
  bis.close();
  dis.close();

} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

return result;
String data = "";
try {
    BufferedReader in = new BufferedReader(new FileReader(new File("some_file.txt")));
    StringBuilder string = new StringBuilder();
    for (String line = ""; line = in.readLine(); line != null)
        string.append(line).append("
");
    in.close();
    data = line.toString();
}
catch (IOException ioe) {
    System.err.println("Oops: " + ioe.getMessage());
}

import java.io.*

This will replace all newlines in the file with , because I don t think there is any way to get the separator used in the file.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签