以下程序從名爲tes.txt
的文件中讀取文本,並將純英文字符串與整個文件中相同的Urdu字符串分開。它在每個英語單詞之後都起着郵票的作用。 文件看起來像:(烏爾都語字符串遵循英文字符串)當我期望英文字符串時得到一個奇怪的輸出
سٹیمپ ختم ہو جاتی ہے
suhail
سٹیمپ ختم ہو جاتی ہے
gupta
سٹیمپ ختم ہو جاتی ہے
ghazal
سٹیمپ ختم ہو جاتی ہے
在使用我編譯下面的程序窗口:
import java.io.*;
class checker {
public static void main(String args[]) {
try {
File f = new File("C:/Users/user/Desktop/tes.txt");
FileReader reader = new FileReader(f);
char buffer[] = new char[1024];
String text = "";
while(reader.read(buffer) > 0) {
text += buffer.toString();
}
String splits[] = text.split("سٹیمپ ختم ہو جاتی ہے");
for(int i=0;i<splits.length;i++) {
System.out.println(splits[i]);
}
} catch(Exception exc) {
exc.printStackTrace();
}
}
}
爲javac -encoding UTF-8 checker.java
。但當我運行這個程序我得到輸出爲[[email protected]
。爲什麼是這樣 ?此外,它只打印數組中的一個字符串。我也檢查了緩衝區陣列的長度,結果是一個。爲什麼一個(文件中有多個字符串在將其從正則表達式中分離出來後會進入緩衝區)?我在哪裏犯了一個錯誤?
首先,你不想被調用'buffer.toString()',而是用緩衝區的內容創建一個新的字符串 - 如果內存服務的話,就像'new String(buffer,charset)'。可能還有其他問題 - 我會稍後再嘗試。 – lifelongcoug