- 将字符串写入到文本文件中 追加写入
- 读取指定目录下的TXT文件的文件内容
- 将字符串写入到文本文件中覆盖写入
- 复制整个文件夹内容
- 复制单个文件
- 遍历路径下所有图片文件
- 获取指定文件大小
- 根据文件名称获取文件后缀
public class FileUtils {
// 将字符串写入到文本文件中 追加写入
public static void writeTxtToFile(String strcontent, String filePath, String fileName) {
//生成文件夹之后,再生成文件,不然会出错
makeFilePath(filePath, fileName);
String strFilePath = filePath + fileName;
writeTxtToFile(strcontent, strFilePath);
}
}
//生成文件
public static File makeFilePath(String filePath, String fileName) {
File file = null;
makeRootDirectory(filePath);
try {
file = new File(filePath + fileName);
if (!file.exists()) {
file.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
public static void writeTxtToFile(String strcontent, String filePath) {
//生成文件夹之后,再生成文件,不然会出错
String strFilePath = filePath;
// 每次写入时,都换行写
String strContent = strcontent; //+ "\r\n"
try {
File file = new File(strFilePath);
if (!file.exists()) {
Log.d("TestFile", "Create the file:" + strFilePath);
file.getParentFile().mkdirs();
} else {
file.delete();
}
file.createNewFile();
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
raf.seek(file.length());
raf.write(strContent.getBytes());
raf.close();
} catch (Exception e) {
Log.e("TestFile", "Error on write File:" + e);
}
}
//追加写入
//调用方式 FileUtils.methoAdd(featureData, "文件路径", "cs.txt");
//读取方式 String text = FileUtils.getFileContent(new File( "文件路径" + "text3.txt"));
public static void methoAdd(String strcontent, String filePath, String fileName) {
FileWriter fw = null;
try {
String strFilePath = filePath + fileName;
//如果文件存在,则追加内容;如果文件不存在,则创建文件
File file = new File(strFilePath);
if (!file.exists()) {
Log.d("TestFile", "Create the file:" + strFilePath);
file.getParentFile().mkdirs();
}
fw = new FileWriter(file, true);
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(fw);
pw.println(strcontent);
pw.flush();
try {
fw.flush();
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//读取指定目录下的TXT文件的文件内容
public static String getFileContent(File file) {
String content = "";
if (!file.isDirectory()) { //检查此路径名的文件是否是一个目录(文件夹)
if (file.getName().endsWith("txt") || file.getName().endsWith("gson")){//文件格式为""文件
try {
InputStream instream = new FileInputStream(file);
if (instream != null) {
InputStreamReader inputreader
= new InputStreamReader(instream, "UTF-8");
BufferedReader buffreader = new BufferedReader(inputreader);
String line = "";
//分行读取
while ((line = buffreader.readLine()) != null) {
content += line + "\n";
}
instream.close();//关闭输入流
}
} catch (FileNotFoundException e) {
Log.d("TestFile", "The File doesn't not exist.");
} catch (IOException e) {
Log.d("TestFile", e.getMessage());
}
}
}
return content;
}
// 将字符串写入到文本文件中覆盖写入
public static void coverWriteTxtToFile(String strcontent, String filePath, String fileName) {
//生成文件夹之后,再生成文件,不然会出错
String strFilePath = filePath + fileName;
try {
File file = new File(strFilePath);
if (!file.exists()) {
Log.d("TestFile", "Create the file:" + strFilePath);
file.getParentFile().mkdirs();
file.createNewFile();
}
//重写文件,覆盖掉原来的数据
FileOutputStream out = new FileOutputStream(file);
out.write(strcontent.getBytes());
out.flush();
out.close();
} catch (Exception e) {
Log.e("TestFile", "Error on write File:" + e);
e.printStackTrace();
}
}
//遍历路径下所有图片文件
public static List showFilePic(String fileName){
List list=new ArrayList();
try {
File scanner5Directory = new File(fileName);
if (scanner5Directory.isDirectory()) {
for (File file : scanner5Directory.listFiles()) {
String path = file.getAbsolutePath();
if (path.endsWith(".jpg") || path.endsWith(".jpeg") || path.endsWith(".png")) {
list.add(path);
}
}
}else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return list;
}
/**
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
/**
* 复制整个文件夹内容
* @param oldPath String 原文件路径 如:c:/fqf
* @param newPath String 复制后路径 如:f:/fqf/ff
* @return boolean
*/
public static boolean copyFolder(String oldPath, String newPath) {
try {
(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
File a=new File(oldPath);
String[] file=a.list();
File temp=null;
for (int i = 0; i < file.length; i++) {
if(oldPath.endsWith(File.separator)){
temp=new File(oldPath+file[i]);
}
else{
temp=new File(oldPath+File.separator+file[i]);
}
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + "/" +
(temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
}
}
return true;
}catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
return false;
}
// 根据文件名称获取文件后缀
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
/**
* 获取指定文件大小
*/
public static long getFileSize(File file) {
long size = 0;
if (file.exists()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
size = fis.available();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Log.e("获取文件大小", "文件不存在!");
}
return size;
}
}