File case:
Requirement: input a string from the console, which is the folder path to calculate the size of this folder
public static File getDirFile() {
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("Please enter a folder path:");
String path = scanner.nextLine();
File file = new File(path);
if (!file.exists()) {
System.out.println("The path you entered does not exist, please re-enter");
}else if (file.isFile()) {
System.out.println("You have entered a file. Please re-enter it");
}else {
return file;
}
}
}
public static long getDirLengthByFile(File file) {
long sum = 0;
File[] listFiles = file.listFiles();
for (File subF : listFiles) {
if (subF.isFile()) {
sum = sum + subF.length();
}else {
sum = sum + getDirLengthByFile(subF);
}
}
return sum;
}
public static void fun() {
File file = getDirFile();
long length = getDirLengthByFile(file);
System.out.println(length);
}
Requirement: input a string from the console, which is the folder path to delete the folder
public static void deleteDir(File file) {
File[] listFiles = file.listFiles();
for (File subFile : listFiles) {
if (subFile.isFile()) {
subFile.delete();
}else {
deleteDir(subFile);
}
}
file.delete();
}
public static void fun() {
File file = getDirFile();
deleteDir(file);
}
filter:
It is an interface to operate on File objects by defining class inheritance interfaces and specifying filtering rules
Filter rules:
When filtering a file, you can see the return value of the method, return true to reserve and return false to filter
Two filter interfaces in File:
FlieFilter:
class MyFlieFilter implements FileFilter {
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return false;
}
return true;
}
}
FilenameFilter:
Two parameters of FilenameFilter:
1. The root path of the folder passed in
2. File name and folder name under the path
class MyFileNameFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String name) {
File file = new File(dir, name);
System.out.println(file);
return false;
}
}
Byte stream:
Reference: the execution procedure.
OutPut definition: program to file OutPut process
InPut definition: file to program InPut process
OutputStream (byte output stream):
An abstract class is the parent of all output streams
Method of OutputStream:
public static void fun() throws IOException {
File file = new File("/Users/lanou/Desktop/Test/jian.txt");
FileOutputStream oStream = new FileOutputStream(file);
oStream.write(49);
byte[] b = {65, 66, 67, 68};
oStream.write(b);
oStream.write(b, 1, 2);
oStream.close();
}
public static void fun() throws IOException {
File file = new File("/Users/lanou/Desktop/Test/jian.txt");
FileOutputStream oStream = new FileOutputStream(file, true);
oStream.write("holle\n".getBytes());
oStream.close();
}
public static void fun() {
File file = new File("/Users/lanou/Desktop/Test/jian.txt");
FlieOutPutStream oStream = null;
try {
oStream = new FlieOutPutStream(file);
oStream.write("holle".getBytes());
} catch (FileNotFoundException e) {
throw new RuntimeException("File not found");
} catch (IOException e) {
throw new RuntimeException("File writing failed");
} finally {
try {
if (oStream != null) {
oStream.close();
}
} catch (IOException e) {
throw new RuntimeException("Closing failed");
}
}
}
InputStream:
Is the parent of all input streams
Note: byte stream writes byte by byte and reads byte by byte
InputStream method:
public static void fun() throws IOException {
File file = new File("/Users/lanou/Desktop/Test/jian.txt");
FileInputStream iStream = new FileInputStream(file);
int i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println(i1);
i1 = iStream.read();
System.out.println(i1);
iStream.close();
}
public static void fun() throws IOException {
File file = new File("/Users/lanou/Desktop/Test/jian.txt");
FileInputStream iStream = new FileInputStream(file);
int read = 0;
while((read = iStream.read()) != -1) {
System.out.println((char)read);
}
iStream.close();
}
public static void fun() throws IOException {
File file = new File("/Users/lanou/Desktop/Test/jian.txt");
FileInputStream iStream = new FileInputStream(file);
byte[] b = new byte[1024];
int i1 = iStream.read(b);
System.out.println(i1);
System.out.println(new String(b));
iStream.close();
}