-1
A
回答
0
import java.io.*;
class Record
{
String name;
String s1;
String s2;
String s3;
String s4;
String s5;
String s6;
public Record(String name, String s1, String s2, String s3, String s4, String s5, String s6){
this.name = name;
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
this.s4 = s4;
this.s5 = s5;
this.s6 = s6;
}
public static void main(String args[]){
try{
FileInputStream fstream = new FileInputStream("textfile.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] tokens = str.split(" ");
Record record = new Record(tokens[0],tokens[1],tokens[2],tokens[3],tokens[4],tokens[5],tokens[6]);//process record , etc
}
in.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
}
6
假設你有
String var = "abc 3.8229 1.0326 1 1 1.1386 1.006";
你可以使用產生一個String.split() String數組。
String[] arr = var.split(" ");
這將產生含有var
每個字的陣列arr
。
3
BufferReader br;
...
String line = br.readLine();
String[] words = line.split(" ");
1
我建議使用掃描儀。
Scanner sc = new Scanner(line);
while(sc.hasNext()){
String word = sc.next(); // Get word
}
在此有利的一面,你也可以使用的
double x = sc.nextDouble();
int i = sc.nextInt();
0
便捷的方法,您可以在您從緩衝區中讀取分裂的話:
try {
String line = "abc 3.8229 1.0326 1 1 1.1386 1.006";
List newWordSymbols = Arrays.asList(' ','\n','\r');
StringReader sr = new StringReader(line);
List wordList = new ArrayList();
StringBuilder word = new StringBuilder();
int ch;
while ((ch=sr.read())!=-1) {
char c = (char)ch;
if(newWordSymbols.contains(c)){
wordList.add(word.toString());
word= new StringBuilder();
}else{
word.append(c);
}
}
wordList.add(word.toString());
System.out.println("Word list ::: "+wordList);
} catch (IOException ex) {
ex.printStackTrace();
}
相關問題
- 1. 使用字符數組作爲字符串流緩衝區
- 2. 循環字符數組緩衝區 - c
- 3. 如何將字節[]數組轉換爲字符串緩衝區
- 4. 字節緩衝區,字符串
- 5. 預期的字符串或緩衝區
- 6. 字符串緩衝區太小
- 7. 包裝紙字符串緩衝區
- 8. 字符串緩衝區需要幫助
- 9. 升壓緩衝區麻煩字符串
- 10. ORA-19011:字符串緩衝區太小
- 11. 讀取一個字符並添加到緩衝區字符串java
- 12. 字符串和字符串緩衝區內存概念
- 13. 字符串緩衝區字符串轉換異常?
- 14. 字符串連接vs字符串緩衝區在Javascript中
- 15. C - 如何將一個字符串數組連接到一個緩衝區?
- 16. 字符緩衝區比較
- 17. java中的字符串緩衝區問題
- 18. C#將字符串複製到字節緩衝區
- 19. 將緩衝區的字符串表示形式轉換爲緩衝區
- 20. ORA-06502:PL/SQL:數字或值錯誤:字符串緩衝區
- 21. 從java到plsql的單字符字符串varchar2(1)導致'字符串緩衝區太小'
- 22. 追加1到32位數字到字符緩衝區
- 23. Node.js緩衝區到類型數組
- 24. 如何在emacs緩衝區中區分Python字符串和文檔字符串?
- 25. 字符串到字符數組Java
- 26. 在C#字符串對象之間共享字符緩衝區
- 27. 如何獲取字符串/緩衝區的字符集?
- 28. 緩衝區比較(無新行字符)與字符串
- 29. vim:跳轉到緩衝區,其中包含/字符串/
- 30. Java字節緩衝區覆蓋字節
定義一個「字」 – 2012-03-27 11:31:44