1 reads all files and folders in a directory
public static ArrayList<String> getFiles(String path) {
ArrayList<String> files = new ArrayList<String>();
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
// System.out.println("文 件:" + tempList[i]);
files.add(tempList[i].toString());
}
if (tempList[i].isDirectory()) {
// System.out.println("文件夹:" + tempList[i]);
}
}
return files;
}
2 3 ways to get the file name from the file path
package test;
import java.io.File;
public class FileName {
/**
* @param args
*/
public static void main(String[] args) {
// 举例:
String fName =" G:\\Java_Source\\navigation_tigra_menu\\demo1\\img\\lev1_arrow.gif ";
// 方法一:
File tempFile =new File( fName.trim());
String fileName = tempFile.getName();
System.out.println("fileName = " + fileName);
// 方法二:
String fName = fName.trim();
String fileName = fName.substring(fName.lastIndexOf("/")+1);
//或者
String fileName = fName.substring(fName.lastIndexOf("\\")+1);
System.out.println("fileName = " + fileName);
// 方法三:
String fName = fName.trim();
String temp[] = fName.split("\\\\"); /**split里面必须是正则表达式,"\\"的作用是对字符串转义*/
String fileName = temp[temp.length-1];
System.out.println("fileName = " + fileName);
}
}
div>
Read More:
- Java 8 Stream – Read a file line by line
- No code, a solution to the error in the path of reading CSV file by Python: filenotfounderror: [errno 2] no such file or directory: ‘XX. CSV‘
- How to search files or folders in Ubuntu
- In VS2010, the compiler cannot open the file “GL / glaux. H”: no such file or directory
- Failed to get D-Bus connection: No such file or directory
- Solution to error opening trace file: no such file or directory (2) in Android
- Clion develops STM32, adds files and compiles with an error “no such file or directory”“
- Error in header file when calling OpenGL to open obj file in vs2013: unable to open include file: “GL / glut. H”: no such file or directories
- How to batch create folders in Excel form and copy the specified files to a new folder
- Solution of header file StdAfx. H “no such file or directory” in Visual Studio
- Vue introduction path is correct, but it always reports an error: already included file name‘ ××ב differs from file name ‘ ××ב only in casing.
- Error:java.io.FileNotFoundException: Path is not a file [How to Solve]
- Error: ENOENT: no such file or directory, open ‘E:\Program Files\nodejs\tmp\upload_38b32f25bc984333c
- Linux use ls to view the file color is all white solution, and Linux file color introduction
- [tools] vscode debug report G + +. Exe no such file or directory fatal error no input files
- A series of problems caused by the option allow output folders for source folders
- How to solve the problem of “unable to open include file:” graphics. H “: no such file or directory”
- Ng Zorro antd file stream upload, access to upload file path
- Java.lang.Character . isdigit() and isletter() methods
- Error reading file cow.osg: read error (Could not find plugin to read objects from file “cow.osg“.)