Memo: [Java] Check the contents of the directory

background

I wanted to check and extract the data in the directory, so I will write down the code at that time.

こんなイメージ.png

** Export all the contents of the directory **

This time, check the inside of the directory called tedkuma / BOX on the C drive. This file is included. 1.png

test.java


import java.io.File;

public class test {

  public static void main(String[] args) {

    File dir = new File("C:/tedkuma/BOX");  //Create an object of File class and specify the target directory
    File[] list = dir.listFiles();          //Get a list of files using listFiles
    for(int i=0; i<list.length; i++) {
      // System.out.println(list[i].toString());     //full path
      System.out.println(list[i].getName());      //File name only
    }
  }
}

When I run the above code ... it's in the BOX directory All directories such as csv files, png files, and new folders have been exported. 2.png

** System.out.println (list [i] .getName ()); ** If you rewrite the **. getName () ** part of ** to **. ToString () ** You can get the file name with the full path like this. 3.png

** Export only the file **

This time, do not write the directory, just the file. If it's a file, it's done. If it's a directory, it's done. ** if (list [i] .isFile ()) {** and ** if (list [i] .isDirectory ()) {** have been added. Since nothing is written in if (list [i] .isDirectory ()) {, the directory is not written.

test.java


import java.io.File;

public class test {

  public static void main(String[] args) {
    
    File dir = new File("C:/tedkuma/BOX");
    File[] list = dir.listFiles();
    for(int i=0; i<list.length; i++) {
      if(list[i].isFile()) {          //For files
        System.out.println(list[i].getName());
      }
      else if(list[i].isDirectory()) { //For directories
        //do nothing
      }
    }
  }
}

I will try it. Outputs other than the directory. 4.png

** Export only CSV file **

You can use contains () to find out if a string is included. Since the file name can be obtained by **. GetName () ** above, add ** contains () ** after it to search for the character string. I want to find CSV, so I wrote it as if **. Csv ** was included.

test.java


import java.io.File;

public class test {

  public static void main(String[] args) {

    File dir = new File("C:/tedkuma/BOX");
    File[] list = dir.listFiles();
    for(int i=0; i<list.length; i++) {
      if(list[i].getName().contains(".csv")) {
        System.out.println(list[i].getName());
      }else{
        //do nothing
      }
    }
  }
}

I will try it. No pngs or directories are written, only csv files are written. 5.png

** Export files with ★ in the file name **

It's exactly the same as above, but just in case ... I just changed ** ".csv" ** to ** "★" **.

test.java


import java.io.File;

public class test {

  public static void main(String[] args) {

    File dir = new File("C:/tedkuma/BOX");
    File[] list = dir.listFiles();
    for(int i=0; i<list.length; i++) {
      if(list[i].getName().contains("★")) {
        System.out.println(list[i].getName());
      }else{
        //do nothing
      }
    }
  }
}

I will try it. Only files with a star in the file name have been exported ~: grinning: 6.png

This time is over.

Recommended Posts

Memo: [Java] Check the contents of the directory
Check the contents of the Java certificate store
[Rails] Check the contents of the object
Check the contents of params with pry
[Java] Check the number of occurrences of characters
How to check for the contents of a java fixed-length string
JAVA: jar, aar, view the contents of the file
[Java] Check the JDK version of the built war file
Check the behavior of Java Intrinsic Locks with bpftrace
Java: Use Stream to sort the contents of the collection
Command to check the number and status of Java threads
Check the status of Java application without using monitoring tool
[Java] Delete the elements of List
Replace the contents of the Jar file
[Java version] The story of serialization
Check the version of Cent OS
Check the migration status of rails
[Java] Reference / update of Active Directory
micronaut document table of contents memo
The origin of Java lambda expressions
[Ruby] Display the contents of variables
Java memo
Get the result of POST in Java
Examine the memory usage of Java elements
[Java] Get the day of the specific day of the week
Folding and unfolding the contents of the Recyclerview
Organized memo in the head (Java --Array)
Compare the elements of an array (Java)
[Java] How to get the current directory
[day: 5] I summarized the basics of Java
[Ruby] Cut off the contents of twitter-ads
What are the updated features of java 13
Easily measure the size of Java Objects
Directory change monitoring method memo (Java, Kotlin)
Looking back on the basics of Java
Check the processing contents with [rails] binding.pry
Output of the book "Introduction to Java"
Acquisition of input contents using Scanner (Java)
The story of writing Java in Emacs
Format the contents of LocalDate with DateTimeFormatter
Check the version of the standard Web software.
[Java8] Search the directory and get the file
The contents of the data saved by CarrierWave.
[Java] [Spring] Test the behavior of the logger
[Java] Contents of Collection interface and List interface
Check the operation of the interface through threads
Memo: [Java] If a file is in the monitored directory, process it.
How to get the absolute path of a directory running in Java
Check the domain by checking the MX record of the email address with java
[Java] Check if the character string is composed only of blanks (= Blank)
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
java anything memo
Check the version of the JDK installed and the version of the JDK enabled
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
Check the options set for the running Java process
[Android] [Java] Manage the state of CheckBox of ListView
Verify the contents of the argument object with Mockito
Inspect the contents of log output during Java unit test (no mock used)
About the description order of Java system properties
Customize how to divide the contents of Recyclerview