I wanted to check and extract the data in the directory, so I will write down the code at that time.
This time, check the inside of the directory called tedkuma / BOX on the C drive. This file is included.
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.
** 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.
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.
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.
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:
This time is over.
Recommended Posts