I. File Operation
1. The Concept of Documentation
Files are a basic form of data storage in computers. Each file is represented by a file path and file name. When accessing the file, you only need to know the path of the file and the full name of the file. In different operating system environments, the representation of file path is different. For example, in Windows operating system, the general representation is C: windows system, while in Unix, the representation is / user/my.
2. Absolute Path and Relative Path
Absolute path refers to the complete path of writing a file, such as d: java Hello.java, which contains the full path d: Java and the full name of the file Hello.java. Using this path, you can only find a file without ambiguity. However, the use of absolute paths in the representation of files is very limited, and can not run in different operating systems, because the expression of absolute paths in different operating systems is different.
Relative path refers to the partial path of writing a file, such as test Hello.java, which contains only the partial path of the file test and the full name of the file Hello.java. Partial path refers to the subpath of the current path. For example, if the current program runs under d: abc, the complete path of the file is d: abc test. Using this form, the location of representative files can be more general, and the file path can be flexible.
3. Basic usage of File class
3.1 Field Summary
public class TestFileFiled {
public static void main(String[] args) {
String pathseparator = File.pathSeparator;
System.out.println(pathseparator);
char pathseparator2 = File.pathSeparatorChar;
System.out.println(pathseparator2);
String separator = File.separator;
System.out.println(separator);
char separator2 = File.separatorChar;
System.out.println(separator2);
}
}
The output is
; ; / /
3.2 Method Summary
public class TestFileMethod {
private File file = null;
public TestFileMethod(String path) {
file = new File(path);
if (file.exists()) {
System.out.println("File exists");
System.out.println("Absolute path:" + file.getAbsolutePath());
System.out.println("Relative path:" + file.getPath());
System.out.println("file name" + file.getName());
} else {
System.out.println("file does not exist");
}
}
public static void main(String[] args) {
// new TestFileMethod("C:\\Users\\Administrator\\Desktop\\xxx.txt");
//new TestFileMethod("C:/Users/Administrator/Desktop/xxx.txt");
//Relative path to relative eclipse root directory
new TestFileMethod2("xxx.txt");
}
}
3.3 File Search
3.3.1 Function Description: Search the path path path for the number of files and folders containing key keywords.
3.3.2 Code Implementation
public class FileSearchService {
int folderNum = 0;// Number of folders
int fileNum = 0;
List<String> filesList = new ArrayList<String>();
public FileSearchService(String path, String key) {
searchKeys(path, key);
//Multi-line display area of plain text
JTextArea jTextArea = new JTextArea();
for (int i = 0; i < filesList.size(); i++) {
String string = filesList.get(i);
jTextArea.append(string + "\n\r");
}
}
public void searchKeys(String path, String key) {
File file = new File(path);
if (file.exists()) {
if (file.isDirectory()) {
// If it is a folder
folderNum++;
File[] listFiles = file.listFiles();//
for (int i = 0; i < listFiles.length; i++) {
File file2 = listFiles[i];
searchKes(file2.getAbsolutePath(), key);
}
} else {
String name = file.getName();
if (name.contains(key)) {
String absolutePath = file.getAbsolutePath();
// If it is a document
fileNum++;
filesList.add(absolutePath);
}
}
} else {
return;
}
}
public static void main(String[] args) {
FileSearchService fileSearchService = new FileSearchService(
"E:\\Data Installation Package\\File\\Curriculum plan\\Robot\\New folder", "aa");
System.out.println(fileSearchService.fileNum);
System.out.println(fileSearchService.folderNum);
}
}