如何在java中读取多个文本文件以供GUI使用 – 没有找到答案

伙计们我是java的新手,我看过去的问题,但我没有找到我理解的答案..我想阅读一些文本文件,并使用ArrayLists在多个JcomboBox和Jlists中使用它们我希望得到的同时然后我想用一个监听器单独到达每个人让我说我有3个文本文件(学校,class级,stundents)和2个JcomboBox中的2个和Jlist中的第3个…我希望得到当我选择学校时,combobox中的课程……以及当我选择课程等时让学生进入Jlist ..我知道如何阅读文本文件但是有任何简单的方法来阅读多个同一时间 ?? 或者我需要为每个代码写这个代码:

fr = new FileReader("c.txt"); br = new BufferedReader(fr); list = new ArrayList(); while ((s = br.readLine()) !=null){ list.add(s); } 

这是一个方法,向您展示如何读取目录中的某些文件,然后使用一个名为HashMap的漂亮数据结构。 此HashMap包含文件名和键值作为键,它具有指定文件的内容。 它是用Java 8制作的,因此您应该根据这些规范进行规范。 除此之外,您只需在项目的根目录中创建一些文件,然后运行此代码。 完成上述代码的运行后,您将看到一些输出用于测试目的。

祝你有个美好的一天,我希望你可以根据自己的需要调整这些代码……也就是说…不使用main方法,而是在应用程序启动时调用它…然后在你的UI中使用对HashMap的引用你可以填充您的列表。

 package filereader; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; public class FileReader { /** * Run this class in a project setup where you have some files in the root * dir of the project. This main method will then read these files and * create a HashMap with key(filename)->val(ArrayList content) assignments. * @param args the command line arguments */ public static void main(String[] args) { try { //this hashmap will store filename->content //you can access it by fileMap.get(filename) and it will return //the corresponding ArrayList that you can then use to retrieve data //off of it by. Tha data is the deserialized content of the file. HashMap> fileMap = new HashMap<>(); List files; Files.list(new File(".").toPath()). filter((path) -> { //add more filters here return path.toFile().isFile(); } ).forEach(path -> { try { ArrayList content = (ArrayList) Files.readAllLines(path); //if necessary, add some behaviour as to how the content should be formatted //here! fileMap.put(path.getFileName().toString(), content); } catch (IOException ex) { System.out.println("This hideous man is a hero! He maked IOEXCeptian ;-("); Logger.getLogger(FileReader.class.getName()).log(Level.SEVERE, null, ex); } }); //lookie lookie content. just for testing. fileMap.forEach((k,v) -> { System.out.println(k); System.out.println(v); }); } catch (IOException ex) { Logger.getLogger(FileReader.class.getName()).log(Level.SEVERE, null, ex); } } 

}

将文件放入某种Collection中 ,然后使用例如for循环遍历它们。

这样您就不必一遍又一遍地重新键入相同的代码。 另外,请仔细阅读Oracle上的教程,特别是章节和章节。

尝试

 File dir = new File(directoryPath); File[] files = dir.listFiles(); 

将您的目录路径作为directoryPath传递,它将使File[]使用此文件数组进行进一步操作。

读取文件

  for(File file : files){ System.out.println(getFileContentsByFileObject(file)); //contents of your file } 

getFileContentsByFileObject(File file) api将文件内容作为String返回

  public static String getFileContentsByFileObject(File file){ BufferedReader br = null; // comment StringBuilder comment = new StringBuilder(); try{ br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { comment.append(line); } }catch (Exception e) { e.printStackTrace(); } return comment.toString(); }