Java-written File Manager code sharing _java

Source: Internet
Author: User
Tags dateformat file copy mkdir

More suitable for beginners. There is still a problem in logic. Can be used to learn Java file operations

Download Address: http://yun.baidu.com/share/link?shareid=4184742416&uk=1312160419

The following are the main Java file manipulation codes

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 MP3 = ". mp3";
  public static final String MP4 = ". mp4";
 
  public static final String APK = ". APK";
 
  Contextual private static 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;
  /** * New Folder * Return True file creation Success * return false file creation failed-> file exists * Return True file creation succeeded, return false file creation failed (file exists, insufficient permissions) * * public static Boolean Creatfile (string filename, string path) {File File = new file (path + file.separator + Filenam
    e);
    if (file.exists ()) {return false;
      else {file.mkdir ();
    return true; /** * Create custom file type Files * Free for folder * 0 txt text * * @return Boolean * Returns true file creation succeeded, returns false file creation failed (file exists, Insufficient permissions) * */public static Boolean creatfile (string filename, string path, int type) {string ptr = path + Fil
    E.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; /** * File Duplicate name * * @param name the newly created filename * @param the place where file was created * * public static Boolean Renam
    E (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 copied to * @return Boolean trun replication succeeded, false copy Failed * */public static Boolean Copeyfile (File oldfile, String tonewpath) {string newfilepath = Tonewpath + F
 
    Ile.separator + oldfile.getname ();
    File temp = new file (Newfilepath);
    Determines whether a relative file exists for the file path being copied to, and if so, stops 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, make a pipe copy try {//Create a pipeline from the file stream FileInputStream fis = new FileInputStream (old
        File);
        FileChannel Creatchannel = Fis.getchannel ();
        Create pipeline in file output target FileOutputStream fos = new FileOutputStream (Newfilepath);
        FileChannel Getchannel = Fos.getchannel ();
 
        Make file copy (pipe butt) 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 clipped * @param where newfilepath is clipped * @return Boolean trun cut succeeded, false Cut failure/public static Boolean Cutfile (File oldfile, String newfilepath) {if (Copeyfile (Oldfile, Newfilepath)) {
      Oldfile.delete ();
    return true;
    else {return false; }/** * Get the file type of the Ask set * * @param dir folder * @param type file types, format ". xxx" * @return list<file> file Set */public static list<file> Getthetypefile (File dir, String type) {list<file> files = new Arrayli
    St<file> ();
      For (File i:dir.listfiles ()) {String Filestyepe = Getfiletype (i);
      if (Type.equals (Filestyepe)) {files.add (i);
  } return to files; 
   /** * Get File type * * @param file required to validate * @return String file type * such as: * Incoming file with filename "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 class to query @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; }
 
}

The above is the entire contents of this article, I hope to be able to learn Java help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.