将从文件读取的数据捕获到字符串流Java中

时间:2010-03-26 02:50:53

标签: java stringstream

我来自C ++背景,所以要善待我的n00bish查询...

我想从输入文件中读取数据并将其存储在字符串流中。我可以使用stringstream在C ++中以简单的方式完成此任务。我试图在Java中做同样的事情,我有点失落。

以下是我开发的原始代码/方式,我将数据逐行存储在字符串数组中。我需要使用字符串流来捕获我的数据(而不是使用字符串数组)..任何帮助?

    char dataCharArray[] = new char[2];
    int marker=0;
    String inputLine;
    String temp_to_write_data[] = new String[100];

    // Now, read from output_x into stringstream

    FileInputStream fstream = new FileInputStream("output_" + dataCharArray[0]);

    // Convert our input stream to a BufferedReader
    BufferedReader in = new BufferedReader (new InputStreamReader(fstream));

    // Continue to read lines while there are still some left to read
    while ((inputLine = in.readLine()) != null )
    {
        // Print file line to screen
        // System.out.println (inputLine);
        temp_to_write_data[marker] = inputLine;
        marker++;
    }

修改

我认为我真正想要的是一个StringBuffer。 我需要从文件读取数据(可能是StringBuffer)并将所有数据写入/传输回另一个文件。

4 个答案:

答案 0 :(得分:1)

在Java中,应首先考虑从图书馆购买代码:

http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html http://commons.apache.org/io/api-1.4/org/apache/commons/io/FileUtils.html

简而言之,您需要的是:

FileUtils.readFileToString(File file)

答案 1 :(得分:0)

StringBuffer是一个答案,但如果您只是将其写入另一个文件,那么您只需打开一个OutputStream并将其直接写入另一个文件即可。将整个文件保存在内存中可能不是一个好主意。

答案 2 :(得分:0)

在你只是想读一个文件并写另一个文件:

BufferedInputStream in = new BufferedInputStream( new FileInputStream( "in.txt" ) );
BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream( "out.txt" ) );
int b;
while ( (b = in.read()) != -1 ) {
    out.write( b );
}

如果要将文件读入字符串:

StringWriter out = new StringWriter();
BufferedReader in = new BufferedReader( new FileReader( "in.txt" ) );
int c;
while ( (c = in.read()) != -1 ) {
    out.write( c );
}
StringBuffer buf = out.getBuffer();

如果使用字节数组读取,则可以提高效率。但我建议你使用优秀的apache common-io。 IOUtils(http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html)会为你做循环。

另外,你应该记得关闭流。

答案 3 :(得分:0)

我也来自C ++,我正在寻找类似于C ++'StringStreamReader'的类,但我找不到它。在我的情况下(我认为非常简单),我试图逐行读取一个文件,然后从这些行中读取一个字符串和一个整数。我的最终解决方案是使用类java.util.Scanner的两个对象,以便我可以使用其中一个将文件的行直接读取到String并使用第二个来重新读取每行的内容(现在在String中)变量(一个新的String和一个正的'int')。这是我的代码:

try {
    //"path" is a String containing the path of the file we want to read
    Scanner sc = new Scanner(new BufferedReader(new FileReader(new File(path))));
    while (sc.hasNextLine()) { //while the file isn't over
        Scanner scLine = new Scanner(sc.nextLine());
        //sc.nextLine() returns the next line of the file into a String
        //scLine will now proceed to scan (i.e. analyze) the content of the string
        //and identify the string and the positive 'int' (what in C++ would be an 'unsigned int')
        String s = scLine.next(); //this returns the string wanted
        int x;
        if (!scLine.hasNextInt() || (x = scLine.nextInt()) < 0) return false;
        //scLine.hasNextInt() analyzes if the following pattern can be interpreted as an int
        //scLine.nextInt() reads the int, and then we check if it is positive or not
        //AT THIS POINT, WE ALREADY HAVE THE VARIABLES WANTED AND WE CAN DO
        //WHATEVER WE WANT WITH THEM
        //in my case, I put them into a HashMap called 'hm'
        hm.put(s, x);
    }
    sc.close();
    //we finally close the scanner to point out that we won't need it again 'till the next time
} catch (Exception e) {
    return false;
}
return true;

希望有所帮助。