How to read multiple text files in java to use graphical interface - could not find an answer

Guys, I'm all new to java, and I looked at past questions, but I did not find an answer that I understand. I want to read some text files and use them in several JcomboBox and Jlists, using ArrayLists I am looking to get at the same time, and then I want to reach each in private using Listener to say that I have 3 text files (schools , classes, chests) and 2 of them in 2 JcomboBox and 3 in Jlist. and I want to get classes in combobox when I choose school .. and to get students in Jlist when I choose class, etc. well i know how to read a text file, but is there any easy way to read more than one at a time? or I need to write for each of this code:

fr = new FileReader("c.txt"); br = new BufferedReader(fr); list = new ArrayList<String>(); while ((s = br.readLine()) !=null){ list.add(s); } 
0
source share
3 answers

Here is a method that shows you how you can read some files in a directory and then use a good data structure called HashMap. This HashMap contains the file name as the key, and the contents of the specified file as the value. This is done with Java 8, and therefore you must have the specifications specified in accordance with these specifications. Other than this, you only need to create a few files in the root directory of the project, and then run this code. After completing the above code, you are shown the result for testing.

I have a good day, and I hope that you can adapt this code to your needs ... otherwise ... not using the main method, and instead calling it when you start the application ... and then using the HashMap links from within your user-defined interface you can fill in your lists.

 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<String> 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<String, ArrayList<String>> fileMap = new HashMap<>(); List<Path> files; Files.list(new File(".").toPath()). filter((path) -> { //add more filters here return path.toFile().isFile(); } ).forEach(path -> { try { ArrayList<String> content = (ArrayList<String>) 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); } } 

}

0
source

Put the files in some Collection , then pass them through, for example, for-loop .

This way you do not need to re-enter the same code again and again. Also, read the tutorials well in Oracle, especially in the Nuts and Bolts section.

+1
source

Try

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

Pass the directory path as directoryPath , it will give you File[] use this file array for further operations.

Read file

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

getFileContentsByFileObject(File file) api returns the contents of the file as a 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(); } 
0
source

Source: https://habr.com/ru/post/1235376/


All Articles