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;
public class FileUtils {
private static final String TAG = "FileUtils";
public static boolean createFile(String filePath, String fileName) {
String strFilePath = filePath + fileName;
File file = new File(filePath);
if (!file.exists()) {
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;
}
public static List<File> getFile(File file) {
List<File> 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;
}
public static boolean deleteFiles(String filePath) {
List<File> 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;
}
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();
}
}
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();
}
}
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("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
public static void renameFile(String oldPath, String newPath) {
File oleFile = new File(oldPath);
File newFile = new File(newPath);
oleFile.renameTo(newFile);
}
public static boolean copy(String fromFile, String toFile) {
File[] currentFiles;
File root = new File(fromFile);
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;
}
}
}