[英]How to display a filenames (.txt) in JTextArea?
此方法僅在控制台中顯示文件名。
JTextArea area = new JTextArea(20,40);
public void readContent(){
KreatorPytan kp = new KreatorPytan();
File file = new File("D:\\IT\\JAVA\\zadanie\\Testy");
File[] files = file.listFiles();
for(int i = 0; i < files.length; i++){
try {
BufferedReader reader = null;
reader = new BufferedReader(new FileReader(files[i]));
if(files[i].isFile()){
System.out.println(files[i].getName());
area.read(reader, "File");
}
} catch (FileNotFoundException ex) {
Logger.getLogger(WyborPytan.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(WyborPytan.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我不知道為什么這些文件名沒有保存到我的 jtextarea。 你能幫助我嗎?
此代碼將文件讀取到 JTextArea 並用下一個重寫每個以前的文件內容。 因此,您將看到最后一個文件內容。 如果你想在 JTextArea 中獲取文件名,你不需要讀取文件,只需要像這樣
for (int i = 0; i < files.length; i++) {
area.append(files[i].getName() + '\n');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.