Commit 15d827a3 authored by qq247321453's avatar qq247321453

IOUtils

parent c919fe4e
......@@ -114,9 +114,7 @@ public class GameUriManager {
}
return new File(dir, "tmp_" + System.currentTimeMillis() + ".ydk");
} else {
if (!dir.exists()) {
dir.mkdirs();
}
IOUtils.createFolder(dir);
}
return file;
}
......@@ -180,10 +178,7 @@ public class GameUriManager {
ParcelFileDescriptor pfd = null;
FileInputStream input = null;
try {
File dir = local.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
IOUtils.createFolder(local.getParentFile());
if (remoteFile != null) {
FileUtils.copyFile(remoteFile, local);
} else {
......
......@@ -21,6 +21,8 @@ import java.io.File;
import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.ui.activities.BaseActivity;
import cn.garymb.ygomobile.utils.FileUtils;
import cn.garymb.ygomobile.utils.IOUtils;
public class FileActivity extends BaseActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener
......@@ -82,7 +84,7 @@ public class FileActivity extends BaseActivity implements AdapterView.OnItemClic
return;
}
File dir = new File(mFileAdapter.getCurPath(), name);
dir.mkdirs();
IOUtils.createFolder(dir);
if (dir.isDirectory()) {
mFileAdapter.setPath(dir.getAbsolutePath());
}
......
......@@ -24,6 +24,7 @@ import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.ui.activities.BaseActivity;
import cn.garymb.ygomobile.ui.plus.DialogPlus;
import cn.garymb.ygomobile.ui.plus.VUiKit;
import cn.garymb.ygomobile.utils.FileUtils;
import cn.garymb.ygomobile.utils.IOUtils;
import ocgcore.DataManager;
import ocgcore.data.Card;
......@@ -261,10 +262,7 @@ public class ImageUpdater implements DialogInterface.OnCancelListener {
if (file.exists()) {
file.delete();
} else {
File dir = file.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
IOUtils.createFolder(file.getParentFile());
}
file.createNewFile();
mConnection = (HttpURLConnection) new URL(url).openConnection();
......
......@@ -324,9 +324,7 @@ public class ResCheckTask extends AsyncTask<Void, Integer, Integer> {
File dirFile = null;
for (String dir : dirs) {
dirFile = new File(mSettings.getResourcePath(), dir);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
IOUtils.createFolder(dirFile);
}
}
......
......@@ -176,7 +176,7 @@ public abstract class PreferenceFragmentPlus extends BasePreferenceFragment {
if (mCurImageInfo != null) {
String cachePath = photos.get(0);
try {
FileUtils.copyFile(cachePath, mCurImageInfo.mOutFile, true);
FileUtils.copyFile(cachePath, mCurImageInfo.mOutFile);
} catch (IOException e) {
Toast.makeText(getContext(), e + "", Toast.LENGTH_LONG).show();
onChooseFileFail(curPreference);
......
......@@ -437,7 +437,7 @@ public class SettingFragment extends PreferenceFragmentPlus {
process.waitFor();
IOUtils.delete(soFile);
FileUtils.copyFile(file, soFile.getAbsolutePath(), true);
FileUtils.copyFile(file, soFile.getAbsolutePath());
me.what = COPY_SO_OK;
} catch (Exception e) {
e.printStackTrace();
......@@ -546,22 +546,22 @@ public class SettingFragment extends PreferenceFragmentPlus {
private void setPendlumScale(boolean ok) {
if (Constants.DEBUG)
Log.i("kk", "setPendlumScale " + ok);
File file = new File(mSettings.getResourcePath(), Constants.CORE_SKIN_PENDULUM_PATH);
File dir = new File(mSettings.getResourcePath(), Constants.CORE_SKIN_PENDULUM_PATH);
if (ok) {
//rename
Dialog dlg = DialogPlus.show(getActivity(), null, getString(R.string.coping_pendulum_image));
VUiKit.defer().when(() -> {
try {
IOUtils.createFolder(file);
IOUtils.createFolder(dir);
IOUtils.copyFilesFromAssets(getActivity(), getDatapath(Constants.CORE_SKIN_PENDULUM_PATH),
file.getAbsolutePath(), false);
dir.getAbsolutePath(), false);
} catch (IOException e) {
}
}).done((re) -> {
dlg.dismiss();
});
} else {
IOUtils.delete(file);
IOUtils.delete(dir);
}
}
......
......@@ -5,6 +5,7 @@ import android.net.Uri;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;
import java.io.BufferedReader;
......@@ -101,10 +102,7 @@ public class FileUtils {
public static void copyFile(InputStream in, File out) throws IOException {
FileOutputStream outputStream = null;
File dir = out.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
IOUtils.createFolder(out.getParentFile());
try {
outputStream = new FileOutputStream(out);
copy(in, outputStream);
......@@ -117,10 +115,7 @@ public class FileUtils {
FileOutputStream outputStream = null;
FileInputStream inputStream = null;
try {
File dir = out.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
IOUtils.createFolder(out.getParentFile());
inputStream = new FileInputStream(in);
outputStream = new FileOutputStream(out);
copy(inputStream, outputStream);
......@@ -134,7 +129,11 @@ public class FileUtils {
return true;
}
public static void copyFile(String oldPath, String newPath, boolean isName) throws FileNotFoundException, IOException {
public static void copyFile(String oldPath, String newPath) throws IOException {
copyFile(oldPath, newPath, true);
}
public static void copyFile(String oldPath, String newPath, boolean isName) throws IOException {
//判断复制后的路径是否含有文件名,如果没有则加上
if (!isName) {
......@@ -155,16 +154,7 @@ public class FileUtils {
}
public static void moveFile(String oldPath, String newPath, boolean isName) throws FileNotFoundException, IOException {
//判断复制后的路径是否含有文件名,如果没有则加上
if (!isName) {
//由于newPath是路径加文件名,所以获取要复制的文件名与复制后的路径组成新的newPath
String abb[] = oldPath.split("/");
newPath = newPath + "/" + abb[abb.length - 1];
}
public static void moveFile(String oldPath, String newPath) throws IOException {
FileInputStream fis = new FileInputStream(oldPath);
FileOutputStream fos = new FileOutputStream(newPath);
byte[] buf = new byte[1024];
......@@ -199,20 +189,18 @@ public class FileUtils {
if (filePath == null)
return;
if (!(new File(newPath)).exists()) {
(new File(newPath)).mkdirs();
}
IOUtils.createFolder(new File(newPath));
for (int i = 0; i < filePath.length; i++) {
if ((new File(oldPath + file.separator + filePath[i])).isDirectory()) {
copyDir(oldPath + file.separator + filePath[i], newPath + file.separator + filePath[i], false);
}
if (new File(oldPath + file.separator + filePath[i]).isFile()) {
if (!(new File(newPath + file.separator + filePath[i]).exists()) || isReplaced)
copyFile(oldPath + file.separator + filePath[i], newPath + file.separator + filePath[i], true);
for (String path : filePath) {
File src = new File(oldPath, path);
File dst = new File(newPath, path);
if (src.isDirectory()) {
copyDir(src.getPath(), dst.getPath(), false);
} else if (src.isFile()) {
if (!dst.exists() || isReplaced) {
copyFile(src.getPath(), dst.getPath());
}
}
}
}
......
......@@ -7,6 +7,8 @@ import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
......@@ -160,17 +162,17 @@ public class IOUtils {
}
public static void createFolderByFile(File file) {
File dir = file.getParentFile();
if (dir != null && !dir.exists()) {
dir.mkdirs();
}
createFolder(file.getParentFile());
}
public static boolean createFolder(File file) {
public static boolean createFolder(@Nullable File file) {
if(file == null){
return false;
}
if (!file.exists()) {
return file.mkdirs();
}
return false;
return true;
}
public static void copy(InputStream in, OutputStream out) throws IOException {
......
......@@ -228,10 +228,11 @@ public class YGODialogUtil {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
du.dis();
DeckType toType = otherType.get(position);
IOUtils.createFolder(new File(toType.getPath()));
List<DeckFile> deckFileList = deckAdp.getSelectList();
for (DeckFile deckFile : deckFileList) {
try {
FileUtils.moveFile(deckFile.getPath(), toType.getPath(), false);
FileUtils.moveFile(deckFile.getPath(), new File(toType.getPath(), deckFile.getName()).getPath());
} catch (IOException e) {
e.printStackTrace();
}
......@@ -257,10 +258,11 @@ public class YGODialogUtil {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
du.dis();
DeckType toType = otherType.get(position);
IOUtils.createFolder(new File(toType.getPath()));
List<DeckFile> deckFileList = deckAdp.getSelectList();
for (DeckFile deckFile : deckFileList) {
try {
FileUtils.copyFile(deckFile.getPath(), toType.getPath(), false);
FileUtils.copyFile(deckFile.getPath(), new File(toType.getPath(), deckFile.getName()).getPath());
} catch (IOException e) {
e.printStackTrace();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment