我想將字符串拆分爲ArrayList。 例子:根據選定的字符數量在ArrayList中拆分字符串
字符串= 「你想有你的問題的回答」 結果與量3:WOU - > ArrayList中,LD - > ArrayList中,你 - > ArrayList中,...
量是一個預定義的變量。
至今:
public static void analyze(File file) {
ArrayList<String> splittedText = new ArrayList<String>();
StringBuffer buf = new StringBuffer();
if (file.exists()) {
try {
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis,
Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(isr);
String line = "";
while ((line = reader.readLine()) != null) {
buf.append(line + "\n");
splittedText.add(line + "\n");
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
String wholeString = buf.toString();
wholeString.substring(0, 2); //here comes the string from an txt file
}
那麼什麼是你的具體問題? (編譯/運行時)錯誤?輸出錯誤? ... – 2012-04-19 19:23:15