File and byte stream in Java

File case:

Requirement: input a string from the console, which is the folder path to calculate the size of this folder
/*
 * A method of obtaining encapsulation and determining whether it is a path
 */
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;
        }
    }
}
/*
 * Calculate folder size
 */
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;
}
/*
 * test
 */
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
/*
 * Delete this folder according to its path
 */
public static void deleteDir(File file) {
    File[] listFiles = file.listFiles();
    for (File subFile : listFiles) {
        if (subFile.isFile()) {
            subFile.delete();
        }else {
            deleteDir(subFile);
        }           
    }
    file.delete();
}
/*
 * test
 */
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:

/*
 * Three write methods
 */
public static void fun() throws IOException {
    File file = new File("/Users/lanou/Desktop/Test/jian.txt");
    FileOutputStream oStream = new FileOutputStream(file);
    //  Write data
    oStream.write(49);
    //  Write byte array
    byte[] b = {65, 66, 67, 68};
    oStream.write(b);
    //  Index and length write of byte array
    oStream.write(b, 1, 2);
    oStream.close();
}
/*
 * File renewal and line feed
 */
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();
}
/*
 * Exception handling (manual)
 */
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:

/*
 * Read method
 * Note: - 1 is returned when reading to the end of the file (equivalent to the end of reading)
 */
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();
}
/*
 * Circular reading
 */
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();
}
/*
 * Use byte array to read files
 */
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); // 2
    System.out.println(new String(b));// ab
    iStream.close();
}

Posted by sellfisch on Thu, 30 Apr 2020 16:41:12 -0700