天天看點

java編寫檔案管理器_java編寫的檔案管理器代碼分享

比較适合新手。邏輯上仍然有點問題。可以用于學習java檔案操作

下載下傳位址:http://yun.baidu.com/share/link?shareid=4184742416&uk=1312160419

下面是主要的JAVA檔案操作代碼

FileHelp.javapackage 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";

//上下文

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);

//判斷複制到的檔案路徑是否存在相對檔案,如果存在,停止該操作

if (temp.exists()) {

return false;

}

//判斷複制的檔案類型是否是檔案夾

if (oldFile.isDirectory()) {

temp.mkdir();

for (File i : oldFile.listFiles()) {

copeyFile(i, temp.getPath());

}

} else {

//如果是檔案,則進行管道複制

try {

//從檔案流中建立管道

FileInputStream fis = new FileInputStream(oldFile);

FileChannel creatChannel = fis.getChannel();

//在檔案輸出目标建立管道

FileOutputStream fos = new FileOutputStream(newfilepath);

FileChannel getChannel = fos.getChannel();

//進行檔案複制(管道對接)

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 getTheTypeFile(File dir, String type) {

List files = new ArrayList();

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;

}

}

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;

}

}

以上所述就是本文的全部内容了,希望能夠對大家學習java有所幫助。

更多java編寫的檔案管理器代碼分享相關文章請關注PHP中文網!

本文原創釋出php中文網,轉載請注明出處,感謝您的尊重!