Summary of document operations in java (dry cargo)

  • 2020-06-07 04:28:54
  • OfStack

File class introduction


package com.file;

import java.io.File;
import java.io.IOException;

/**
 * Created by elijahliu on 2017/2/10.
 */
public class filetest {
  public static void main(String[] args) {
    File file = new File("hello.txt");
    // If there is a 
    if (file.exists()) {
      // file 
      System.out.println(file.isFile());
      // Path (folder) 
      System.out.println(file.isDirectory());

      File nameto = new File("new Hello.txt");
      file.renameTo(nameto);// So here's the renaming of the file, just go ahead and create it 1 a file Object and then use renameTo Method to rename a file 

    } else {
      System.out.println(" File does not exist ");
      try {
        file.createNewFile();
        System.out.println(" The file has been created ");
      } catch (IOException e) {
        System.out.println(" File cannot be created ");
      }
    }
    if (file.exists()) {
      // Delete the file 
      file.delete();
      System.out.println(" Delete the file ");
    } else {
    }
  }
}

Folder operation


package com.file;

import java.io.File;

/**
 * Created by elijahliu on 2017/2/11.
 */
public class HelloFolder {
  public static void main(String[] args) {
    File folder = new File("my new folder");
    if (folder.mkdir()) {// Create folder   Judge success 
      System.out.println(" Folder creation completed ");
      File newfolder = new File("myn new foleder - new");
      folder.renameTo(newfolder);// I renamed the folder here   The rename of the folder is to be changed individually 1 Level of folder name   And this 1 The folder below level ii remains unchanged   Save directory structure 
      if (folder.delete()) {
        System.out.print("done");// The delete here can only delete empty folders, if there is something in the folder, then cannot delete, do not ask 372101 Delete directly 1 A non-empty folder is very irresponsible 
      } else {
        System.out.println("fail");
      }

    }else{
      if (folder.exists()) {
        System.out.println(" Folders already exist without being created ");
      }else{
        System.out.println(" Folder creation failed ");
      }
    }
    File folders = new File("my new folder/one/two/three/main");
    folders.mkdirs();// in java using mkdir Can only create 1 A, mkdirs Multiple levels of directories can be created 

  }
}

File property Settings


package com.file;

import java.io.File;

/**
 * Created by elijahliu on 2017/2/11.
 */
public class SetFileProperty {
  public static void main(String[] args){
    File file = new File("test.file");
    if (file.exists()){
      file.setWritable(true);// Can write 
      file.setReadable(true);// Can be read 
      file.setReadOnly();// read-only 
    }
  }
}

Traversal folder


  public void printFiles(File dir,int tab) {//tab Indenting for different directory structures 
    if (dir.isDirectory()) {
      File next[] = dir.listFiles();// Determine if it is a directory   Returns an array of all filenames of the directory for traversing the folder 
      for (int i = 0;i<next.length;i++) {// Hierarchical indent output 
        System.out.print("---");
      }
      for(int i = 0;i<next.length;i++) {// This USES recursion to get the directory structure 
        System.out.println(next[i].getName());
        if (next[i].isFile()) {
          printFiles(next[i],++tab);

        }
      }
    }
  }

Simple file read and write


package com.file;

import java.io.*;

/**
 * Created by elijahliu on 2017/2/11.
 */
public class ReadFile {
  public static void main(String[] args) {
    File file = new File("new Hello.txt");
    if(file.exists()){
      System.err.print("exsit");
      try (FileInputStream fis = new FileInputStream(file)) {// File input stream   This is a byte stream 

        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//inputstreamReader is 1 Byte stream, byte stream and character stream conversion time, need to be specified 1 A code, or it will be messy 
        BufferedReader br = new BufferedReader(isr);// Character buffer 

        String line;
        while((line = br.readLine())!=null){// Print out the contents of the buffer if they are not empty 
          System.out.println(line);

        }
        br.close();// Finally, the individual threads are closed 
        isr.close();
        fis.close();
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    File newfile = new File("newtext.txt");
    try {
      FileOutputStream fos = new FileOutputStream(newfile);// Here if the file doesn't exist it will be created automatically 
      OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");// And read 1 So here's the byte and character streams that are converted 
      BufferedWriter bw = new BufferedWriter(osw);// Here is the write buffer 

      bw.write(" Oh my god, you rock! ");// Write string 

      bw.close();// And the above 1 sample   What's open last here is closed first   Open first and close later 
      osw.close();
      fos.close();
      System.out.println("done");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

  }
}


Related articles: