How to read multiple text files in java for GUI use - 没有找到答案
How to read multiple text files in java for GUI use - didnt find the answer
伙计们,我是 java 的新手,我查看了过去的问题,但没有找到我理解的答案..
我想读取一些文本文件并通过使用 ArrayLists 在多个 JcomboBox 和 Jlists 中使用它们
我希望同时获得,然后我想通过一个 Listener 单独联系每个人,假设我有 3 个文本文件(schools,classes,stundents),其中 2 个在 2 JcomboBox 和在 Jlist 中排名第 3 .. 我想在我 select 学校时在组合框中获得 classes .. 当我 select [=] 时让学生在 Jlist 中15=] 等等..
好吧,我知道如何阅读文本文件,但是有什么简单的方法可以同时阅读多个文本文件吗??或者我需要为每个代码编写此代码:
fr = new FileReader("c.txt");
br = new BufferedReader(fr);
list = new ArrayList<String>();
while ((s = br.readLine()) !=null){
list.add(s);
}
尝试
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 return 文件内容为 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();
}
将文件放入某种 Collection, then loop through them with, e.g., a for-loop。
这样您就不必一次又一次地重新输入相同的代码。另外,请仔细阅读 Oracle 的教程,尤其是具体细节部分。
这里有一个方法可以向您展示如何读取目录中的一些文件,然后使用一个名为 HashMap 的漂亮数据结构。
这个 HashMap 包含文件名作为键,它包含指定文件的内容作为值。它是用 Java 8 制作的,因此你应该根据这些规格来指定你的规格。除此之外,您只需在项目的根目录中创建一些文件,然后 运行 此代码。 运行上述代码完成后,您将看到一些用于测试目的的输出。
祝你有愉快的一天,我希望你能根据你的需要调整这段代码......也就是......不使用 main 方法而是在应用程序启动时调用它......然后使用对 HashMap 的引用在您的 UI 中,您可以填充您的列表。
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);
}
}
}
伙计们,我是 java 的新手,我查看了过去的问题,但没有找到我理解的答案.. 我想读取一些文本文件并通过使用 ArrayLists 在多个 JcomboBox 和 Jlists 中使用它们 我希望同时获得,然后我想通过一个 Listener 单独联系每个人,假设我有 3 个文本文件(schools,classes,stundents),其中 2 个在 2 JcomboBox 和在 Jlist 中排名第 3 .. 我想在我 select 学校时在组合框中获得 classes .. 当我 select [=] 时让学生在 Jlist 中15=] 等等.. 好吧,我知道如何阅读文本文件,但是有什么简单的方法可以同时阅读多个文本文件吗??或者我需要为每个代码编写此代码:
fr = new FileReader("c.txt");
br = new BufferedReader(fr);
list = new ArrayList<String>();
while ((s = br.readLine()) !=null){
list.add(s);
}
尝试
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 return 文件内容为 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();
}
将文件放入某种 Collection, then loop through them with, e.g., a for-loop。
这样您就不必一次又一次地重新输入相同的代码。另外,请仔细阅读 Oracle 的教程,尤其是具体细节部分。
这里有一个方法可以向您展示如何读取目录中的一些文件,然后使用一个名为 HashMap 的漂亮数据结构。 这个 HashMap 包含文件名作为键,它包含指定文件的内容作为值。它是用 Java 8 制作的,因此你应该根据这些规格来指定你的规格。除此之外,您只需在项目的根目录中创建一些文件,然后 运行 此代码。 运行上述代码完成后,您将看到一些用于测试目的的输出。
祝你有愉快的一天,我希望你能根据你的需要调整这段代码......也就是......不使用 main 方法而是在应用程序启动时调用它......然后使用对 HashMap 的引用在您的 UI 中,您可以填充您的列表。
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);
}
}
}