-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. C#字符串數組緩衝區
- 2. 字符串緩衝區 - OutOfMemoryError
- 3. 使用字符數組作爲字符串流緩衝區
- 4. 字節緩衝區,字符緩衝區,字符串和字符集
- 5. 循環字符數組緩衝區 - c
- 6. 如何將字節[]數組轉換爲字符串緩衝區
- 7. 字節緩衝區,字符串
- 8. 字節緩衝區爲字符串GWT
- 9. 在協議緩衝區中添加字符串數組
- 10. 緩衝區不會打印字符串
- 11. 預期字符串或緩衝區
- 12. 比較緩衝區與C字符串
- 13. 「預期字符串或緩衝區」
- 14. 在緩衝區中查找字符串
- 15. 升壓緩衝區麻煩字符串
- 16. 的XMLType字符串緩衝區太小
- 17. 字符串緩衝區聲明不足
- 18. 字符串緩衝區需要幫助
- 19. 字符串緩衝區太小
- 20. 字符串緩衝區太小錯誤
- 21. 包裝紙字符串緩衝區
- 22. 從緩衝區中提取字符串
- 23. Node.js緩衝區字符串序列化
- 24. ORA-19011:字符串緩衝區太小
- 25. 預期的字符串或緩衝區
- 26. java套接字緩衝區和字符串
- 27. 讀取一個字符並添加到緩衝區字符串java
- 28. 字符串和字符串緩衝區內存概念
- 29. 字符串緩衝區字符串轉換異常?
- 30. 字符串連接vs字符串緩衝區在Javascript中
定義一個「字」 – 2012-03-27 11:31:44