可以說我有一個字符串=「你好」。我如何打開一個文本文件並檢查該文本文件中是否存在你好?該文本文件的在分隔文本中搜索字符串文件
內容:
hello:man:yeah
我嘗試使用下面的代碼。它是隻讀文件的第一行嗎?我需要它來檢查所有行,看看你是否存在,然後如果是這樣,請從中取出「man」。
try {
BufferedReader in = new BufferedReader(new FileReader("hello.txt"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
} catch (IOException e) {
System.out.println("Error.");
}
'字符串myArray的[] = str.split(「:」);'在java [String](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html)類中有許多方法這些類型的東西。 – 2012-01-11 14:46:40
你的文件只包含一行......所以readline在讀取該行時比離開循環,因爲第二次in.readline返回null – Hons 2012-01-11 14:47:14
你對BufferedReader的使用看起來是正確的。你沒有看到「hello.txt」的逐行輸出嗎? – dasblinkenlight 2012-01-11 14:47:57