Java written file manager code sharing

  • 2020-04-01 03:45:57
  • OfStack

Suitable for beginners. There's still a little bit of a logic problem. Can be used to learn Java file manipulation

Download address: (link: http://yun.baidu.com/share/link? Shareid = 4184742416 & UK = 1312160419)

The following is the main JAVA file manipulation code

FileHelp. Java


package self.yy.filesystem.fileutil;
 
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
 

public class FileHelp {
  private static final String TAG = "FileHelp";
 
  public static final String JPG = ".jpg";
  public static final String PNG = ".png";
 
  public static final String MP3 = ".mp3";
  public static final String MP4 = ".mp4";
  public static final String APK = ".apk";
 
  //context
  private static Context context;
 
  
  public static int ISTXT = 0;
 
  private static String TXT = ".txt";
 
  
  public static boolean deletfile(File file) {
    if (file.isDirectory()) {
      if (file.listFiles().length > 0) {
        for (File i : file.listFiles()) {
          deletfile(i);
        }
      } else {
        file.delete();
      }
    } else {
      file.delete();
    }
    file.delete();
    return true;
  }
 
  
  public static boolean creatFile(String filename, String path) {
    File file = new File(path + File.separator + filename);
    if (file.exists()) {
      return false;
    } else {
      file.mkdir();
      return true;
    }
  }
 
  
  public static boolean creatFile(String filename, String path, int type) {
    String ptr = path + File.separator + filename;
    File file;
    switch (type) {
      case 0:
        file = new File(ptr + TXT);
        break;
      default:
        file = new File(ptr);
        break;
    }
    if (file.exists()) {
      return false;
    } else {
      try {
        file.createNewFile();
        return true;
      } catch (IOException e) {
        return false;
      }
    }
  }
 
 
  
  public static boolean reName(String name, File file) {
    String pathStr = file.getParent() + File.separator + name;
    return file.renameTo(new File(pathStr));
  }
 
  
  public static boolean copeyFile(File oldFile, String toNewPath) {
    String newfilepath = toNewPath + File.separator + oldFile.getName();
 
    File temp = new File(newfilepath);
    //Determines whether the copied file path has a relative file, and if so, stops the operation
    if (temp.exists()) {
      return false;
    }
    //Determines whether the copied file type is a folder
    if (oldFile.isDirectory()) {
      temp.mkdir();
      for (File i : oldFile.listFiles()) {
        copeyFile(i, temp.getPath());
      }
    } else {
      //If it is a file, pipe copy is performed
      try {
        //Create the pipe from the file stream
        FileInputStream fis = new FileInputStream(oldFile);
        FileChannel creatChannel = fis.getChannel();
        //Creates a pipe on the file output target
        FileOutputStream fos = new FileOutputStream(newfilepath);
        FileChannel getChannel = fos.getChannel();
        //File copying (pipeline docking)
        getChannel.transferFrom(creatChannel, 0, creatChannel.size());
 
        getChannel.close();
        creatChannel.close();
        fos.flush();
        fos.close();
        fis.close();
      } catch (Exception e) {
        Log.i(TAG, "copey defeated,mebey file was existed");
        e.printStackTrace();
        return false;
      }
    }
    return true;
  }
 
  
  public static boolean cutFile(File oldFile, String newFilePath) {
    if (copeyFile(oldFile, newFilePath)) {
      oldFile.delete();
      return true;
    } else {
      return false;
    }
  }
 
 
  
  public static List<File> getTheTypeFile(File dir, String type) {
    List<File> files = new ArrayList<File>();
    for (File i : dir.listFiles()) {
      String filesTyepe = getFileType(i);
      if (type.equals(filesTyepe)) {
        files.add(i);
      }
    }
    return files;
  }
 
  
  public static String getFileType(File file) {
    String fileName = file.getName();
    if (fileName.contains(".")) {
 
      String fileType = fileName.substring(fileName.lastIndexOf("."),
          fileName.length());
      return fileType;
    } else {
      return null;
    }
  }
 
 
  /**
   *  Gets the last operation time class of the file 
   *
   * @param file  The file classes that need to be queried 
   * @return  " yy/MM/dd HH:mm:ss "Data string 
   *  Such as: 
   * 14/07/01 01:02:03
   */
  public static String getCreatTime(File file) {
    long time = file.lastModified();
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
    String date = dateFormat.format(calendar.getTime());
    return date;
  }
 
}

That's all for this article, and I hope it will help you learn Java.


Related articles: