I got stuck in File

I got stuck in the File class

It was an incident when I was writing a program that uses the File class in Java. ..

I made a program like this

It was a program that patrolled the folder tree of a specified folder, read the text after that, and output it to CSV.

folder structure

targetFolder ├folder1 │ ├folder1_folder1 │ │ ├text1.txt │ │ ├text2.txt │ │ └text3.txt │ └folder1_folder2 │ ├text.txt │ ├text2.txt │ └text3.txt ├folder2 │ ├folder2_folder1 │ │ ├text1.txt │ │ ├text2.txt │ │ └text3.txt
The following is omitted

The method to go around is as follows

private static List<File> getFileInstanceList(String filePathStr) throws IOException {
    try (Stream<Path> path = Files.list(Paths.get(filePathStr))) {
        return path.map(f -> f.getFileName().toFile()).collect(Collectors.toList());
    }
}

If you pass the path of the folder as an argument, the File instance of the folder in that folder will be returned as a list.

List<File> list = getFileInstanceList("C:\Users\hoge\Desktop\targetFile\");

At the read source, the returned list was looped with For-each, and the file path of the child folder was passed to the above method again, but ...

An error has occurred

There was a problem with the File instance returned by this method When I got the path of the child folder, I could only get the file name

When I checked the JavaDoc of the File class, I found the following description.

Returns a File object that represents this path. If this Path is associated with the default provider, this method is equivalent to returning a File object constructed with a String representation of this Path. If this path is created by calling the File to Path method, there is no guarantee that the File object returned by this method will be equal to the original File.

Equivalent to returning a File object constructed with a String representation? ??

I don't know what it is, but I think it's like this if it's easy to understand. File class has a constructor that takes a String as an argument When the instance generated at that time specifies the full path of File In some cases, only the file name is specified, but the latter cannot retain the actual state of the file correctly.

Well, "C: \ Users \ hoge \ Desktop \ targetFile \ folder1" If there is a File instance created by "folder1", the former would be more realistic.

Improved method

private static List<File> getFileInstanceList(String filePathStr) throws IOException {
   try (Stream<Path> path = Files.list(Paths.get(filePathStr))) {
        return path.map(f -> new File(f.toUri())).collect(Collectors.toList());
   }
}

Since it is enough to pass the full path to generate a File, I changed it to instantiate a File with Uri of Path as an argument.

I'm just not sure even if I read JavaDoc, so I wonder if API is used to it.

Recommended Posts

I got stuck in File
Where I got stuck in today's "rails tutorial" (2020/10/05)
Where I got stuck in today's "rails tutorial" (2020/10/06)
Where I got stuck in today's "rails tutorial" (2020/10/04)
Where I got stuck in today's "rails tutorial" (2020/10/07)
I got stuck in a clone of a two-dimensional array
What I got into @Transactional in Spring
I got stuck trying to write a where in clause in ActiveRecord
[JAVA] Project Euler, I got stuck in Q8, so make a note
I got stuck while studying Java SE 7/8 Bronze
I got stuck when port forwarding with VBox
Where I got stuck with Swift Package Manager support
I got a cannot resolve symbol in Android Studio
I got an error "undefined method` create'" in RSpec
When I switched to IntelliJ, I got a lot of differences in the encoding of the properties file.
I made roulette in Java.
I read module-info.java in java.base
I want to create a Parquet file even in Ruby
I got an ActionController :: MissingExactTemplate even though I have a file
I got stuck in launching Unreal Enigine 4 on an AWS Ubuntu instance, so I summarized it
I tried metaprogramming in Java
I participated in Kubernetes Internal # 2
About what I did when creating a .clj file in Clojure
I got a Permission Denied error when I put Laravel in Docker
I sent an email in Java
Read Java properties file in C #
I created a PDF in Java.
I participated in JJUG CCC 2019 Spring
Unzip the zip file in Java
Log output to file in Java
I wrote Goldbach's theorem in java
I tried putting Domino11 in CentOS7
[Java] What got caught in encapsulation
I participated in JJUG CCC 2019 fall
I made an annotation in Java.
I tried using JWT in Java
Include image resources in jar file
About file copy processing in Java
Try file locking in Ruby directory
Get stuck in a Java primer
I participated in JJUG CCC 2019 Fall.
I built a rails environment with docker and mysql, but I got stuck
I examined the file created when selecting Empty Activity in Android Studio
I get got errors: User must exist in the unit model test
I checked the package name referenced by the class contained in the JAR file
In WSL2, when I did `docker-compose up`, I got an error saying that the sh file was not found.