Androidファイルおよびフォルダの作成、削除、名前変更、コピーのコピーについて
6871 ワード
package com.example.administrator.myapplication.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2019/5/17 0017.
*/
public class FileUtils {
private static final String TAG = "FileUtils";
/**
*
*
* @param filePath
* @param fileName
* @return
*/
public static boolean createFile(String filePath, String fileName) {
String strFilePath = filePath + fileName;
File file = new File(filePath);
if (!file.exists()) {
/** mkdirs() */
file.mkdirs();
}
File subfile = new File(strFilePath);
if (!subfile.exists()) {
try {
boolean b = subfile.createNewFile();
return b;
} catch (IOException e) {
e.printStackTrace();
}
} else {
return true;
}
return false;
}
/**
*
*
* @param file
*/
public static List getFile(File file) {
List list = new ArrayList<>();
File[] fileArray = file.listFiles();
if (fileArray == null) {
return null;
} else {
for (File f : fileArray) {
if (f.isFile()) {
list.add(0, f);
} else {
getFile(f);
}
}
}
return list;
}
/**
*
*
* @param filePath
* @return
*/
public static boolean deleteFiles(String filePath) {
List files = getFile(new File(filePath));
if (files.size() != 0) {
for (int i = 0; i < files.size(); i++) {
File file = files.get(i);
/** */
if (file.isFile()) {
file.delete();
}
}
}
return true;
}
/**
*
*
* @param strcontent
* @param filePath
* @param fileName
*/
public static void writeToFile(String strcontent, String filePath, String fileName) {
// , ,
String strFilePath = filePath + fileName;
// ,
File subfile = new File(strFilePath);
RandomAccessFile raf = null;
try {
/** */
raf = new RandomAccessFile(subfile, "rw");
/** */
raf.seek(subfile.length());
/** */
raf.write(strcontent.getBytes());
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* ( )
*
* @param path
* @param content
* @param append , (true= )(false= )
*/
public static void modifyFile(String path, String content, boolean append) {
try {
FileWriter fileWriter = new FileWriter(path, append);
BufferedWriter writer = new BufferedWriter(fileWriter);
writer.append(content);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
*
* @param filePath
* @param filename
* @return
*/
public static String getString(String filePath, String filename) {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(new File(filePath + filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer("");
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("
");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
/**
*
*
* @param oldPath
* @param newPath
*/
public static void renameFile(String oldPath, String newPath) {
File oleFile = new File(oldPath);
File newFile = new File(newPath);
//
oleFile.renameTo(newFile);
}
/**
*
*
* @param fromFile
* @param toFile
* @return
*/
public static boolean copy(String fromFile, String toFile) {
//
File[] currentFiles;
File root = new File(fromFile);
// SD
// return
if (!root.exists()) {
return false;
}
//
currentFiles = root.listFiles();
//
File targetDir = new File(toFile);
//
if (!targetDir.exists()) {
targetDir.mkdirs();
}
//
for (int i = 0; i < currentFiles.length; i++) {
if (currentFiles[i].isDirectory())//
{
copy(currentFiles[i].getPath() + "/", toFile + currentFiles[i].getName() + "/");
} else//
{
CopySdcardFile(currentFiles[i].getPath(), toFile + currentFiles[i].getName());
}
}
return true;
}
//
// ( )
public static boolean CopySdcardFile(String fromFile, String toFile) {
try {
InputStream fosfrom = new FileInputStream(fromFile);
OutputStream fosto = new FileOutputStream(toFile);
byte bt[] = new byte[1024];
int c;
while ((c = fosfrom.read(bt)) > 0) {
fosto.write(bt, 0, c);
}
fosfrom.close();
fosto.close();
return true;
} catch (Exception ex) {
return false;
}
}
}