Android File Manipulation Tool Class (ext.) 2014/4/3 18:13:35 Lonely Traveller Blog Park
This tool class contains a few basic files for Android app development, and it's the first time I've sent a blog to share what I've written.
If there are any additions or modifications, you are welcome to put forward valuable suggestions.
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.util.ArrayList;
Import java.util.List;
Import com.example.test.applicationcontext;//has no source files and can be used to delete the associated code.
Import com.example.test.utils.assist.myfilefilter;//has no source files and can be used to delete the associated code.
/*
* This class is used to perform basic operations on files, including copying, moving, creating, deleting, and so on.
*/
public class FileUtils {
Public final static int default_file_operate_mode = 0; File objects are absolute paths when files are created, overwriting if files are present
Public final static int ignore_not_recreate_mode = 1; The file object is the path to the relative memory when the files are created, and if there is a file, it means that it exists and does not operate
Public final static int ignore_and_recreate_mode = 2; The file object is the path to the relative memory when the files are created, and if there is a file, it means that it exists and does not operate
Public final static int not_ignore_recreate_mode = 3; File objects are absolute paths when files are created, overwriting if files are present
Private Final Static Boolean Default_ignore_style = false;
Private Final Static Boolean default_auto_create_directory = true; If the directory does not exist, it is automatically created
/**
* Create files with the file object as parameters
* @param file
* @param ignore
* If mode equals Ignore_not_recreate_mode or Ignore_and_recreate_mode, the input file is a relative path, and if the other two modes indicate the absolute path of the input.
* Added SDcard dirctory.
* @throws IOException
*/
public static void CreateFile (file file, int mode) throws IOException
{
if (file = = NULL | | Stringutils.isempty (File.getabsolutepath ())) {
Return
}
if (mode = = Ignore_not_recreate_mode
|| mode = = Ignore_and_recreate_mode) {
File = new file (Storageutils.getstoragefile (),
File.getabsolutepath ());
}
if (mode = = Default_file_operate_mode
|| mode = = Ignore_and_recreate_mode) {
DeleteFile (file);
}
File.createnewfile ();
}
/**
* Create a file with a string object as a parameter
* @param FilePath
* @param mode
* @throws IOException
*/
public static void CreateFile (String filePath, int mode) throws IOException
{
CreateFile (New File (FilePath), mode);
}
/**
* Create files in default way
* @param FilePath
* @throws IOException
*/
public static void CreateFile (File filePath) throws IOException
{
CreateFile (FilePath, Default_file_operate_mode);
}
/**
* @param FilePath
* @throws IOException
*/
public static void CreateFile (String filePath) throws IOException
{
CreateFile (New File (FilePath));
}
/**
* Create Folder
*
* @param folder
* @param mode
*/
public static void CreateFolder (File folder, int mode)
{
if (folder = = NULL | | Stringutils.isempty (Folder.getabsolutepath ())) {
Return
}
if (!folder.isdirectory ()) {
Return
}
if (mode = = Ignore_not_recreate_mode
|| mode = = Ignore_and_recreate_mode) {
folder = new File (Storageutils.getstoragefile (),
Folder.getabsolutepath ());
}
if (mode = = Default_file_operate_mode
|| mode = = Ignore_and_recreate_mode) {
DeleteFolder (folder);
}
Folder.mkdirs ();
}
/**
* @param folder
* @param mode
*/
public static void CreateFolder (String folder, int mode)
{
CreateFolder (New File (folder), mode);
}
/**
* @param folder
*/
public static void CreateFolder (File folder)
{
CreateFolder (folder, Default_file_operate_mode);
}
/**
* @param folder
*/
public static void CreateFolder (String folder)
{
CreateFolder (New File (folder));
}
/**
* Delete Files
*
* @param file
*/
public static void DeleteFile (file file)
{
if (file = = NULL | | Stringutils.isempty (File.getabsolutepath ())) {
Return
}
if (file.exists ()) {
if (!file.isdirectory ()) {
File.delete ();
}
}
}
/**
* @param FilePath
*/
public static void DeleteFile (String filePath)
{
if (! Stringutils.isempty (FilePath)) {
DeleteFile (New File (FilePath));
}
}
/**
* Delete a directory
*
* @param folder
*/
public static void DeleteFolder (File folder)
{
if (folder = = NULL | | Stringutils.isempty (Folder.getabsolutepath ())) {
Return
}
if (folder.exists ()) {
if (Folder.isdirectory ()) {
file[] files = folder.listfiles ();
if (files!=null)
{
for (File file:files) {
DeleteFolder (file);
}
}
}
else {
DeleteFile (folder);
}
}
}
/**
* @param folderpath
*/
public static void DeleteFolder (String folderpath)
{
if (! Stringutils.isempty (FolderPath)) {
DeleteFile (New File (FolderPath));
}
}
/**
* Find files ending with extensions in the specified directory
*
* @param end
* @return
*/
public static list<file> getallwithend (file file, Boolean ignore,
String ... extensions)
{
if (Stringutils.isempty (File.getabsolutepath ())) {
return null;
}
for (String extension:extensions) {
if (stringutils.isempty (extension)) {
return null;
}
}
if (ignore) {
File = new file (Storageutils.getstoragefile (),
File.getabsolutepath ());
}
if ((!file.exists ()) && file.isdirectory ()) {
return null;
}
list<file> files = new arraylist<file> ();
FileFilter (file, files, extensions);
return files;
}
/**
* @param path
* @param extensions
* @param ignore
* @return
*/
public static list<file> Getallwithend (String path, Boolean ignore,
String ... extensions)
{
Return Getallwithend (new File (path), ignore, extensions);
}
/**
* @param file
* @param extensions
* @return
*/
public static list<file> getallwithend (file file, String ... extensions)
{
return getallwithend (file, Default_ignore_style, extensions);
}
/**
* @param file
* @param extensions
* @return
*/
public static list<file> Getallwithend (String File, string ... extensions)
{
Return Getallwithend (new file, Default_ignore_style, extensions);
}
/**
* Filter Files
*
* @param file
* @param extensions
* @param files
*/
public static void FileFilter (file file, list<file> files,
String ... extensions)
{
if (!file.isdirectory ()) {
Return
}
file[] Allfiles = File.listfiles ();
file[] Allextensionfiles = file.listfiles (new Myfilefilter (extensions));
if (allextensionfiles! = null) {
for (File single:allextensionfiles) {
Files.add (single);
}
}
if (allfiles! = null) {
for (File single:allfiles) {
if (Single.isdirectory ()) {
FileFilter (single, files, extensions);
}
}
}
}
/**
* Copy files from the assets directory to the specified location
*
* @param Strassetsfilepath
* @param Strdesfilepath
* @return
*/
public boolean assetscopydata (String Strassetsfilepath,
String Strdesfilepath)
{
Boolean bissuc = true;
InputStream inputstream = null;
OutputStream outputstream = null;
File File = new file (Strdesfilepath);
if (!file.exists ()) {
try {
File.createnewfile ();
Runtime.getruntime (). EXEC ("chmod 766" + file);
}
catch (IOException e) {
Bissuc = false;
}
}
else {//exists
return true;
}
try {
InputStream = ApplicationContext.mContext.getAssets (). Open (
Strassetsfilepath);
OutputStream = new FileOutputStream (file);
int nlen = 0;
byte[] Buff = new byte[1024 * 1];
while ((Nlen = Inputstream.read (buff)) > 0) {
Outputstream.write (Buff, 0, Nlen);
}
}
catch (IOException e) {
Bissuc = false;
}
finally {
try {
if (outputstream! = null) {
Outputstream.close ();
}
if (InputStream! = null) {
Inputstream.close ();
}
}
catch (IOException e) {
Bissuc = false;
}
}
return BISSUC;
}
/**
* Copy Files
*
* @param src
* @param DST
* @return
* @throws IOException
*/
public static Boolean copyFile (file src, file dst) throws IOException
{
if (!src.exists ()) | | src.isdirectory () | | dst.isdirectory ()) {
return false;
}
if (!dst.exists ()) {
Dst.createnewfile ();
return false;
}
FileInputStream inputstream = null;
FileOutputStream outputstream = null;
InputStream = new FileInputStream (SRC);
OutputStream = new FileOutputStream (DST);
int readlen = 0;
byte[] buf = new byte[1024];
while ((Readlen = Inputstream.read (BUF))! =-1) {
Outputstream.write (buf, 0, Readlen);
}
Outputstream.flush ();
Inputstream.close ();
Outputstream.close ();
return true;
}
/**
* @param src
* @param DST
* @return
* @throws IOException
*/
public static Boolean CopyFile (String src, string dst) throws IOException
{
Return CopyFile (new file (src), new file (DST));
}
/**
* Copy Entire Directory
* @param srcdir
* @param destDir
* @param auto
* @return
* @throws IOException
*/
public static Boolean CopyFolder (file Srcdir, File DestDir, Boolean Auto)
Throws IOException
{
if ((!srcdir.exists ())) {
return false;
}
if (Srcdir.isfile () | | destdir.isfile ())
return false;
if (!destdir.exists ()) {
if (auto) {
Destdir.mkdirs ();
}
else {
return false;
}
}
file[] Srcfiles = Srcdir.listfiles ();
int len = srcfiles.length;
for (int i = 0; i < len; i++) {
if (Srcfiles[i].isfile ()) {
File DestFile = new file (Destdir.getpath () + "//"
+ Srcfiles[i].getname ());
CopyFile (Srcfiles[i], destfile);
}
else if (srcfiles[i].isdirectory ()) {
File Thedestdir = new file (Destdir.getpath () + "//"
+ Srcfiles[i].getname ());
CopyFolder (Srcfiles[i], thedestdir,auto);
}
}
return true;
}
/**
* @param srcdir
* @param desdir
* @param auto
* @return
* @throws IOException
*/
public static Boolean CopyFolder (String Srcdir, String Desdir, Boolean Auto)
Throws IOException
{
Return CopyFolder (new file (Srcdir), new file (Desdir), auto);
}
/**
* @param srcdir
* @param desdir
* @return
* @throws IOException
*/
public static Boolean CopyFolder (file Srcdir, file Desdir)
Throws IOException
{
Return CopyFolder (Srcdir, Desdir, default_auto_create_directory);
}
/**
* @param srcdir
* @param desdir
* @return
* @throws IOException
*/
public static Boolean CopyFolder (String srcdir, String desdir)
Throws IOException
{
Return CopyFolder (Srcdir, Desdir, default_auto_create_directory);
}
/**
* Move individual files
* @param src
* @param DST
* @return
*/
public static Boolean moveFile (file src, file DST)
{
Boolean iscopy = false;
try {
iscopy = CopyFile (src, DST);
}
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
if (!iscopy)
{
return false;
}
DeleteFile (SRC);
return true;
}
/**
* @param src
* @param DST
* @return
*/
public static Boolean MoveFile (String src, string dst)
{
Return MoveFile (new file (src), new file (DST));
}
/**
* Move the entire directory
* @param srcdir
* @param destDir
* @param auto
* @return
*/
public static Boolean MoveFolder (File srcdir,file Destdir,boolean Auto)
{
if (!srcdir.isdirectory () | |!destdir.isdirectory ()) {
return false;
}
if (!srcdir.exists ())
{
return false;
}
if (!destdir.exists ())
{
if (auto)
{
Destdir.mkdirs ();
}
else{
return false;
}
}
file[] Srcdirfiles = Srcdir.listfiles ();
int len = srcdirfiles.length;
if (len<=0)
{
Srcdir.delete ();
}
for (int i = 0; i < len; i++) {
if (Srcdirfiles[i].isfile ()) {
File Onedestfile = new file (Destdir.getpath () + "//"
+ Srcdirfiles[i].getname ());
MoveFile (Srcdirfiles[i], onedestfile);
} else if (Srcdirfiles[i].isdirectory ()) {
File Onedestfile = new file (Destdir.getpath () + "//"
+ Srcdirfiles[i].getname ());
MoveFolder (Srcdirfiles[i], onedestfile,auto);
DeleteFolder (Srcdirfiles[i]);
}
}
return true;
}
/**
* @param src
* @param DST
* @param auto
* @return
*/
public static Boolean MoveFolder (String src,string Dst,boolean Auto)
{
Return MoveFolder (new file (src), new file (DST));
}
/**
* @param src
* @param DST
* @return
*/
public static Boolean MoveFolder (File src,file DST)
{
Return MoveFolder (SRC, DST, default_auto_create_directory);
}
/**
* @param src
* @param DST
* @return
*/
public static Boolean MoveFolder (String src,string DST)
{
Return MoveFolder (new file (src), new file (DST), default_auto_create_directory);
}
/**
* Get private file directory (/data/data/yourpackage/file)
*
* @return
*/
public static File Getprivatedir ()
{
return ApplicationContext.mContext.getFilesDir ();
}
}
Android File Manipulation Tool Class (GO)