[英]Capture data read from file into string stream Java
我來自C ++背景,所以請對我的n00bish查詢好一點...
我想從輸入文件中讀取數據並將其存儲在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中),然后將所有數據寫/傳輸回另一個文件中。
在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)
我也來自C ++,我一直在尋找與C ++'StringStreamReader'類似的類,但找不到。 就我而言(我認為這很簡單),我試圖逐行讀取文件,然后從每行讀取一個String和一個Integer。 我的最終解決方案是使用類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;
希望能有所幫助。
StringBuffer是一個答案,但是如果您只是將其寫入另一個文件,則可以打開一個OutputStream並將其直接寫到另一個文件中。 在內存中保存整個文件可能不是一個好主意。
在其中,您只想讀取一個文件並編寫另一個文件:
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 )將為您完成循環。
另外,您應該記得關閉流。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.