java以迭代方式从目录中读取文件,并将值分配给不同的ArrayList
我以迭代方式读取txt文件,我要做的是读取每个文件的内容,并将内容分别分配给ArrayList。我用于访问txt文件的代码:java以迭代方式从目录中读取文件,并将值分配给不同的ArrayList,java,file,arraylist,directory,Java,File,Arraylist,Directory,我以迭代方式读取txt文件,我要做的是读取每个文件的内容,并将内容分别分配给ArrayList。我用于访问txt文件的代码: Path basePath = Paths.get("filepath"); try (DirectoryStream<Path> pathList = Files.newDirectoryStream(basePath, "*.txt")) { for (Path path : pathList) { System.out.printl
Path basePath = Paths.get("filepath");
try (DirectoryStream<Path> pathList = Files.newDirectoryStream(basePath,
"*.txt")) {
for (Path path : pathList) {
System.out.println(path.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
public class FileToArrayLists {
public static void main(String[] args) {
Path basePath = Paths.get("C:\\temp");
try (DirectoryStream<Path> pathList = Files.newDirectoryStream(
basePath, "*.txt")) {
Map<String, List<String>> fileLists = new HashMap<>();
for (Path path : pathList) {
fileLists.put(path.toString(), Files.readAllLines(path, Charset.defaultCharset()));
}
System.out.println(fileLists);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Path basePath=Path.get(“filepath”);
try(DirectoryStream pathList=Files.newDirectoryStream(basePath,
“*.txt”)){
用于(路径:路径列表){
System.out.println(path.toString());
}
}捕获(IOE异常){
e、 printStackTrace();
}
我不确定我是否理解这个问题
public class FileToArrayLists {
public static void main(String[] args) {
Path basePath = Paths.get("C:\\temp");
try (DirectoryStream<Path> pathList = Files.newDirectoryStream(
basePath, "*.txt")) {
Map<String, List<String>> fileLists = new HashMap<>();
for (Path path : pathList) {
fileLists.put(path.toString(), Files.readAllLines(path, Charset.defaultCharset()));
}
System.out.println(fileLists);
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用BufferedReader逐行读取文件的内容,在那里可以将这些行添加到ArrayList
Path basePath = Paths.get("filepath");
try (DirectoryStream<Path> pathList = Files.newDirectoryStream(basePath,
"*.txt")) {
for (Path path : pathList) {
BufferReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
String currentLine = reader.readline();
ArrayList<String> comments = new ArrayList<>();
while( currentLine != null ) {
comments.add(currentLine);
currentLine = reader.readline();
}
}
} catch (IOException e) {
e.printStackTrace();
}
public class FileToArrayLists {
public static void main(String[] args) {
Path basePath = Paths.get("C:\\temp");
try (DirectoryStream<Path> pathList = Files.newDirectoryStream(
basePath, "*.txt")) {
Map<String, List<String>> fileLists = new HashMap<>();
for (Path path : pathList) {
fileLists.put(path.toString(), Files.readAllLines(path, Charset.defaultCharset()));
}
System.out.println(fileLists);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Path basePath=Path.get(“filepath”);
try(DirectoryStream pathList=Files.newDirectoryStream(basePath,
“*.txt”)){
用于(路径:路径列表){
BufferReader=Files.newBuffereReader(路径,StandardCharsets.UTF_8);
字符串currentLine=reader.readline();
ArrayList comments=新的ArrayList();
while(currentLine!=null){
注释。添加(当前行);
currentLine=reader.readline();
}
}
}捕获(IOE){
e、 printStackTrace();
}
如果您使用的是Java 7,则可以使用方法Files.readAllLines(路径、字符集)如列表行=Files.readAllLines(路径,Charset.defaultCharset())
public class FileToArrayLists {
public static void main(String[] args) {
Path basePath = Paths.get("C:\\temp");
try (DirectoryStream<Path> pathList = Files.newDirectoryStream(
basePath, "*.txt")) {
Map<String, List<String>> fileLists = new HashMap<>();
for (Path path : pathList) {
fileLists.put(path.toString(), Files.readAllLines(path, Charset.defaultCharset()));
}
System.out.println(fileLists);
} catch (IOException e) {
e.printStackTrace();
}
}
}
公共类文件到阵列列表{
公共静态void main(字符串[]args){
Path basePath=Path.get(“C:\\temp”);
try(DirectoryStream路径列表=Files.newDirectoryStream(
基本路径,“*.txt”)){
Map fileLists=newhashmap();
用于(路径:路径列表){
fileLists.put(path.toString(),Files.readAllLines(path,Charset.defaultCharset());
}
System.out.println(文件列表);
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
在while循环之后,我试图打印currentline数组,但得到了堆空间错误。我怎样才能修好它?例如,我必须为8个txt文件再创建8个arrayList吗?请参见编辑,很抱歉,现在无法测试代码。实际上,我想澄清的是,当我读取txt文件时,我希望将其分配给num1 arrayList,并分别为其余txt文件执行此操作。有什么方法可以做到这一点吗?在这种情况下,不要迭代路径,将for循环中的所有内容提取到一个以路径为参数的方法中,并返回一个数组列表。感谢您的回答,文件列表hashmap会将文件合并,那么我如何使它们分开hashmap呢?我看不出分开hashmap有什么意义。我只是维护了一个hashmap来保存单独的ArrayList。但是,如果希望有单独的hashmaps,请在for循环中移动映射创建逻辑。for(Path-Path:pathList){Map-fileLists=new-HashMap();fileLists.put(Path.toString(),Files.readAllLines(Path,Charset.defaultCharset());}