1

私は C++ のバックグラウンドを持っているので、n00bish クエリには親切にしてください...

入力ファイルからデータを読み取り、文字列ストリームに保存したいと思います。文字列ストリームを使用して、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

4 に答える 4

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)
于 2010-03-26T15:15:35.213 に答える
0

StringBuffer は 1 つの答えですが、別のファイルに書き込むだけの場合は、OutputStream を開いて、別のファイルに直接書き込むことができます。ファイル全体をメモリに保持することは、おそらく良い考えではありません。

于 2010-03-26T03:03:22.423 に答える
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 ) がループを実行します。

また、ストリームを閉じることを忘れないでください。

于 2010-03-26T03:10:14.733 に答える
0

私も C++ 出身で、C++ の「StringStreamReader」に似たクラスを探していましたが、見つかりませんでした。私の場合(非常に単純だと思います)、ファイルを1行ずつ読み取ってから、これらの各行から文字列と整数を読み取ろうとしていました。私の最終的な解決策は、クラスjava.util.Scannerの2つのオブジェクトを使用することでした。これにより、そのうちの1つを使用してファイルの行を文字列に直接読み取り、2番目のオブジェクトを使用して各行のコンテンツを再読み取りできます(現在は文字列) を変数 (新しい文字列と正の '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;

それが役に立ったことを願っています。

于 2013-08-14T23:47:16.970 に答える