当前位置:网站首页>Problems and solutions in the learning process of file class
Problems and solutions in the learning process of file class
2022-07-26 12:17:00 【Wang Xiaoya】
1. utilize File Constructors ,new A file directory file 1) Create multiple files and directories in it , Test what you have learned File Class method 2) Write the method manually , Implementation delete file Operation of the specified file in 2. Determine whether there is a suffix named .jpg The file of , If there is , Just output the file name 3. Traverse the specified directory all file names , Include files in the subdirectory ( Pay attention to this sentence , Classical recursive method , If you don't know the way of thinking, ask directly ). 4. Create a and a.txt The file is another file in the same directory b.txt 5. operation D On the plate my.txt file 1) Judge my.txt Does the file exist 2) Delete if present ; Created if it does not exist 6.File What is the function of class ? Is it a flow ? 7. Use File Class to delete a folder ( for example D On the plate temp Folder ) All the files and folders under : 1) Judge temp File type under folder , If it is a file, delete it directly 2) If it is a folder, get the sub files and folders under the folder 3) Use recursion to delete all temp Files and folders under folders ( May refer to 3 topic )
1. utilize File Constructors ,new A file directory file
1) Create multiple files and directories in it , Test what you have learned File Class method
2) Write the method manually , Implementation delete file Operation of the specified file in */
1.1 Code :
package com.B.IOStream_14.FileDemo.Ask;
import java.io.File;
import java.io.IOException;
public class FileA1 {
public static void main(String[] args) throws IOException {
// Create objects 、 file
File f1 = new File("E:\\JavaDemo\\Demo03\\Web");
System.out.println(f1.mkdirs()); // Create multi-level directory
File f2 = new File("E:\\JavaDemo\\Demo04");
System.out.println(f2.mkdir()); // Create a single level directory
File f3 = new File("E:\\JavaDemo\\Demo04\\ Today Friday is very happy .txt");
System.out.println(f3.createNewFile()); // create a file
File f4 = new File("E:\\JavaDemo\\Demo04\\ Tomorrow and Saturday will be happier .txt");
System.out.println(f4.createNewFile()); // create a file
File f5 = new File("E:\\JavaDemo\\Demo04\\ Happy Sunday the day after tomorrow .txt");
System.out.println(f5.createNewFile()); // create a file
System.out.println("-----------");
// Delete file f5
f5.delete();
// Create a... Based on the given path File object
File file = new File("E:\\JavaDemo");
// Calling method
textFile(file);
// Output file path
System.out.println(file);
}
// Compiling method , Implementation delete file Operation of the specified file in
public static void textFile(File file) {
// file The files in the object are stored in File File array
File[] fileArrary = file.listFiles();
if (fileArrary != null) { // Add a judgment statement that is not empty , Increase robustness
for (File f : fileArrary) { // enhance for Cyclic ergodic set , first for Delete content
// Determine whether the file is a file directory It's recursion No delete
if (f.isDirectory()) {
// yes : Recursively call
textFile(f);
} else
// No : Get the absolute path output on the console
System.out.println(f.getAbsoluteFile());
}
}
}
}Run the following :
The runtime found that other files could not be output ,debug During debugging, these contents appear but are not output ,
create a file :
Delete file :
Determine whether there is a suffix named .jpg The file of , If there is , Just output the file name
Code :
package com.B.IOStream_14.FileDemo.Ask; import java.io.File; import java.io.IOException; //2. Determine whether there is a suffix named .jpg The file of , If there is , Just output the file name public class FileA2 { public static void main(String[] args) throws IOException { //1. Create a path where the current file is located File file = new File("E:\\JavaDemo\\Demo04"); File f1 = new File("E:\\JavaDemo\\Demo04\\ I'm so happy to buy a big orange dress .txt"); System.out.println(f1.createNewFile()); // create a file File f2 = new File("E:\\JavaDemo\\Demo04\\ No money. QAQ.txt"); System.out.println(f2.createNewFile()); // create a file File f3 = new File("E:\\JavaDemo\\Demo04\\ Mura baby .jpg"); System.out.println(f3.createNewFile()); // create a file //2. Store the file name in the current directory into the character array String[] fileNames = file.list(); for (String fileName : fileNames) { //Java endsWith() Method if (fileName.endsWith(".jpg")) { System.out.println(fileName); } } //endsWith() Method to test whether the string ends with the specified suffix . // grammar //public boolean endsWith(String suffix) // Parameters //suffix -- Specified suffix . // Return value // If the character sequence represented by the parameter is the suffix of the character sequence represented by this object , Then return to true; Otherwise return to false. Be careful , If the argument is an empty string , Or equal to this String object ( use equals(Object) Method determination ), The result is true. } }expand :
Introduced JavaendsWith() Method
endsWith() Method to test whether the string ends with the specified suffix .
grammar
public boolean endsWith(String suffix)
Parameters
suffix -- Specified suffix .
Such as :
for (String fileName : fileNames) { //Java endsWith() Method if (fileName.endsWith(".jpg")) { System.out.println(fileName); } }
Return value
If the character sequence represented by the parameter is the suffix of the character sequence represented by this object , Then return to true; Otherwise return to false. Be careful , If the argument is an empty string , Or equal to this String object ( use equals(Object) Method determination ), The result is true.
function :

Traverse the specified directory all file names , Include files in the subdirectory ( Pay attention to this sentence , Classical recursive method , If you don't know the way of thinking, ask directly ).
Code :
package com.B.IOStream_14.FileDemo.Ask; import java.io.File; import java.io.IOException; //3. Traverse the specified directory all file names , Include files in the subdirectory ( Pay attention to this sentence , Classical recursive method ) public class FileA3 { public static void main(String[] args) throws IOException { //1. Create a path where the current file is located File file = new File("E:\\JavaDemo"); // Calling method textFile(file); System.out.println(file); } // Compiling method , Implementation delete file Operation of the specified file in public static void textFile(File file) { // file The files in the object are stored in File File array File[] fileArrary = file.listFiles(); if (fileArrary != null) { // Add a judgment statement that is not empty , Increase robustness for (File f : fileArrary) { // enhance for Cyclic ergodic set , first for Delete content // Determine whether the file is a file directory It's recursion No delete if (f.isDirectory()) { // yes : Recursively call textFile(f); } else // No : Get the absolute path output on the console System.out.println(f.getAbsoluteFile()); } } } }
function :

6.File What is the function of class ? Is it a flow ?
File Class is used to implement java Classes that interact with file data , It's not flow .
7. Use File Class to delete a folder ( for example D On the plate temp Folder ) All the files and folders under : 1) Judge temp File type under folder , If it is a file, delete it directly 2) If it is a folder, get the sub files and folders under the folder 3) Use recursion to delete all temp Files and folders under folders ( May refer to 3 topic )
package com.B.IOStream_14.FileDemo.Ask;
import java.io.File;
import java.io.IOException;
//7. Use File Class to delete a folder ( for example D On the plate temp Folder ) All the files and folders under :
//1) Judge temp File type under folder , If it is a file, delete it directly
//2) If it is a folder, get the sub files and folders under the folder
//3) Use recursion to delete all temp Files and folders under folders ( May refer to 3 topic )
public class FileA7 {
public static void main(String[] args) throws IOException {
//1. Create a path where the current file is located
File file = new File("D:\\temp");
System.out.println(file.mkdir());
// create a file
File f3 = new File("D:\\temp\\ Today Friday is very happy .txt");
System.out.println(f3.createNewFile()); // create a file
File f4 = new File("D:\\temp\\ Tomorrow and Saturday will be happier .txt");
System.out.println(f4.createNewFile()); // create a file
File f5 = new File("D:\\temp\\ Happy Sunday the day after tomorrow .txt");
System.out.println(f5.createNewFile()); // create a file
System.out.println("-----------");
// Calling method
textFile(file);
System.out.println(file);
System.out.println("-----------");
file.delete();
System.out.println(file);
}
// Compiling method , Implementation delete file Operation of the specified file in
public static void textFile(File file) {
// file The files in the object are stored in File File array
File[] fileArrary = file.listFiles();
if (fileArrary != null) { // Add a judgment statement that is not empty , Increase robustness
for (File f : fileArrary) { // enhance for Cyclic ergodic set , first for Delete content
// Determine whether the file is a file directory It's recursion No delete
if (f.isDirectory()) {
// yes : Recursively call
textFile(f);
} else
// No : Get the absolute path output on the console
System.out.println(f.getAbsoluteFile());
}
}
}
}Create file directory temp And the content of the document :

Execute delete and print :

边栏推荐
- Pytest interface automated test framework | fixture call fixture
- Some common writing methods and skills
- Pytest interface automated testing framework | pytest obtains execution data, and pytest disables plug-ins
- Audio and video technology development weekly | 255
- QT入门引导 及其 案例讲解
- Ssj-21b time relay
- Network protocol: tcp/ip protocol
- 2022.7.23-----leetcode.剑指offer.115
- .eslintrc.js配置说明
- 10. 509. Introduction to PKCs file format
猜你喜欢
![[countdown 10 days] Tencent cloud audio and video special is about to meet, and the thousand yuan prize is waiting for you!](/img/a0/4910970a089cab198875944c7ae88c.png)
[countdown 10 days] Tencent cloud audio and video special is about to meet, and the thousand yuan prize is waiting for you!

Li Kai: the interesting and cutting-edge audio and video industry has always attracted me

Flink's real-time data analysis practice in iFLYTEK AI marketing business

Ssj-21b time relay

什么是回调函数,对于“回”字的理解

HTAP comes at a price

Hou Peixin, chairman of the openharmony Working Committee of the open atom open source foundation, sent a message to the openatom openharmony sub forum

The difference between JVM memory overflow and memory leak

【2243】module_param.m

MySQL组合索引(多列索引)使用与优化
随机推荐
JSJ-3/AC220V时间继电器
Introduction to FPGA (III) - 38 decoder
On the construction and management of low code technology in logistics transportation platform
Transactional事务传播行为?
Hit the blackboard and draw the key points: a detailed explanation of seven common "distributed transactions"
Here blog: running a large language model in a production environment - overview of the reasoning framework
Customize browser default right-click menu bar
【微信小程序】一文读懂,数据请求
DS-24C/DC220V时间继电器
You Yuxi recommends vite to beginners [why use vite]
RFID的工作原理
Detailed explanation of Legendre transformation and conjugate function
Sim900a based on STM32 sends short messages in Chinese and English
pytest接口自动化测试框架 | 使用多个fixture
Digital intelligence transformation, management first | jnpf strives to build a "full life cycle management" platform
el-form 每行显示两列,底部按钮居中
How RFID works
字节流习题遇到的问题及解决方法
Map函数统计字符出现的次数
pytest接口自动化测试框架 | 重新运行失败用例

