More suitable for novices. There's still some logic issues. Can be used to learn java file operations
Download address: http://yun.baidu.com/share/link?shareid=4184742416&uk=1312160419
The following is the main JAVA file operation 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; /** * File related help class*/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 JPG = ".jpg"; public static final String PNG = ".png"; final String MP3 = ".mp3"; public static final String MP4 = ".mp4"; public static final String APK = ".apk"; //Context private static Context context; /** * txt text*/ public static int ISTXT = 0; private static String TXT = ".txt"; /** * File deletion*/ 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; } /** * Create a new folder* Return true File creation is successful* Return false File creation failed -> file exists* Return true File creation is successful, return false File creation failed (file exists, permissions are insufficient) */ 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; } } /** * Create a custom file type file * Any folder * 0 txt text * * @return boolean * Return true The file is created successfully and false is returned. The file creation fails (the file exists and the permissions are insufficient) * * */ 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; } } } /** * Duplicate file name* * @param name The name of the newly created file* @param file The place where the file is created* / public static boolean reName(String name, File file) { String pathStr = file.getParent() + File.separator + name; return file.renameTo(new File(pathStr)); } /** * File copy* * @param oldFile The file to be copied* @param toNewPath The place to copy to* @return boolean trun if the copy is successful, false if the copy fails* * */ public static boolean copeyFile(File oldFile, String toNewPath) { String newfilepath = toNewPath + File.separator + oldFile.getName(); File temp = new File(newfilepath); //Determine whether there is a relative file in the copied file path. If it exists, stop the operation if (temp.exists()) { return false; } //Determine 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, perform pipe copy try { //Create a pipe from the file stream FileInputStream fis = new FileInputStream(oldFile); FileChannel creatChannel = fis.getChannel(); //Create a pipe at the file output target FileOutputStream fos = new FileOutputStream(newfilepath); FileChannel getChannel = fos.getChannel( ); //Copy files (pipeline connection) 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; } /** * File cut* * @param oldFile The file to be cut* @param newFilePath The place to be cut* @return boolean trun if the cut is successful, false if the cut fails*/ public static boolean cutFile(File oldFile, String newFilePath) { if (copeyFile(oldFile, newFilePath) ) { oldFile.delete(); return true; } else { return false; } } /** * Get the file set corresponding to the file type* * @param dir folder* @param type file type, format ".xxx" * @return List<file> file set*/ 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; } /** * Get the file type* * @param file The file that needs to be verified * @return String file type* Such as: * Pass in File named "test.txt" * Returns .txt * * */ 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; } } /** * Get File last operation time class * * @param file File class to be queried * @return "yy/MM/dd HH:mm:ss" data string * For example: * 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; } }
The above is the entire content of this article. I hope it will be helpful to everyone learning Java.