比较适合新手。逻辑上仍然有点问题。可以用于学习java文件操作
下载地址:http://yun.baidu.com/share/link?shareid=4184742416&uk=1312160419
下面是主要的java文件操作代码
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";
//上下文
private static context context;
/**
* txt文本
*/
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;
}
/**
* 新建文件夹
* 返回true 文件创建成功
* 返回false 文件创建失败 ->文件存在
* 返回true 文件创建成功,返回false 文件创建失败 (文件存在、权限不够)
*/
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;
}
}
/**
* 创建自定义文件类型文件
* 随意为文件夹
* 0 txt文本
*
* @return boolean
* 返回true 文件创建成功,返回false 文件创建失败 (文件存在、权限不够)
* *
*/
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;
}
}
}
/**
* 文件重名
*
* @param name 新创建的文件名
* @param file 创建文件的地方
*/
public static boolean rename(string name, file file) {
string pathstr = file.getparent() + file.separator + name;
return file.renameto(new file(pathstr));
}
/**
* 文件复制
*
* @param oldfile 要被复制的文件
* @param tonewpath 复制到的地方
* @return boolean trun 复制成功,false 复制失败
* *
*/
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;
}
/**
* 文件剪切
*
* @param oldfile 要被剪切的文件
* @param newfilepath 剪切到的地方
* @return boolean trun 剪切成功,false 剪切失败
*/
public static boolean cutfile(file oldfile, string newfilepath) {
if (copeyfile(oldfile, newfilepath)) {
oldfile.delete();
return true;
} else {
return false;
}
}
/**
* 获取对应文件类型的问件集
*
* @param dir 文件夹
* @param type 文件类型,格式".xxx"
* @return list<file> 文件集合
*/
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;
}
/**
* 获取文件类型
*
* @param file 需要验证的文件
* @return string 文件类型
* 如:
* 传入文件名为“test.txt”的文件
* 返回 .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;
}
}
/**
* 获取文件最后操作时间类
*
* @param file 需要查询的文件类
* @return “yy/mm/dd hh:mm:ss”的数据字符串
* 如:
* 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;
}
}
以上所述就是本文的全部内容了,希望能够对大家学习java有所帮助。
更多java编写的文件管理器代码分享。