2010-03-26 60 views
1

我從C++背景的,所以善待我的n00bish查詢...捕獲數據流的Java

我想從一個輸入文件中讀取數據並將其存儲在一個stringstream。我可以使用stringstreams以簡單的方式在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),並將所有數據寫入/傳輸回另一個文件。

+0

究竟是一個字符串流?任何原因你不能只讀閱讀器中的字符? – 2010-03-26 02:54:40

+0

如果你願意,你可以將整個東西存儲在一個StringBuffer中,但是如果你可以對你的數據做些什麼的話,我們可以給你一個更好的想法,讀。 – Kylar 2010-03-26 02:56:09

+0

http://www.cplusplus.com/reference/iostream/stringstream/ - >單個字符串數據流? 你的回答可能是有意義的,但我不知道該怎麼辦呢? 哦,我需要做到這一點:閱讀所有的數據,所有的數據寫回到另一個文件.. 沒錯, StringBuffer就是我所追求的。我以爲我會讀取所有的數據到這個StringBuffer並將其轉移到另一個文件.. – halluc1nati0n 2010-03-26 02:56:57

回答

0

StringBuffer是一個答案,但是如果您只是將它寫入另一個文件,那麼您只需打開一個OutputStream並將其直接寫入另一個文件即可。在內存中保存整個文件可能不是一個好主意。

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)將爲您完成循環。

此外,你應該記得關閉流。

0

我也來自C++,我正在尋找類似於C++'StringStreamReader'的類,但是我找不到它。在我的情況下(我認爲它很簡單),我試圖逐行讀取一個文件,然後從這些行中讀取一個字符串和一個整數。我的最終解決方案是使用類java.util.Scanner中的兩個對象,以便我可以使用其中一個將文件的行直接讀取到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; 

希望有所幫助。