[英]How to read line by line from a text area
我已經弄清楚如何逐行閱讀並逐行顯示文本文檔的內容到jtextarea我已經想出如何逐行寫出字符串數組到文本文檔。 我只是很難從textarea獲得每一行,只要我能將每一行都放到一個數組中我就好了。 下面是我將用於將每行寫入文件的代碼...
public class FileWrite {
public static void FileClear(String FileName) throws IOException{
FileWriter fstream = new FileWriter(FileName,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("");
}
public static void FileWriters(String FileName, String Content) throws IOException
{
FileWriter fstream = new FileWriter(FileName,true);
BufferedWriter out = new BufferedWriter(fstream);
out.append(Content);
out.newLine();
}
}
謝謝
C
你從TextArea
得到的只是一個字符串。 將它拆分為換行符,你就得到了你的String []。
for (String line : textArea.getText().split("\\n")) doStuffWithLine(line);
我試圖使用JTextArea類提供的方法來回答這個問題。
希望這有助於某人,因為當我用Google搜索時我找不到答案。 您現在需要做的就是實現方法processLine(String lineStr)
int lines = textArea.getLineCount();
try{// Traverse the text in the JTextArea line by line
for(int i = 0; i < lines; i ++){
int start = textArea.getLineStartOffset(i);
int end = texttArea.getLineEndOffset(i);
// Implement method processLine
processLine(textArea.getText(start, end-start));
}
}catch(BadLocationException e){
// Handle exception as you see fit
}
在這里查看類的定義JTextArea Java 1.7
快樂的編碼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.