Commit 5ac6b6ce authored by fallenstardust's avatar fallenstardust

统一使用IOUtils处理file

parent af0fbf7b
...@@ -114,9 +114,7 @@ public class GameUriManager { ...@@ -114,9 +114,7 @@ public class GameUriManager {
} }
return new File(dir, "tmp_" + System.currentTimeMillis() + ".ydk"); return new File(dir, "tmp_" + System.currentTimeMillis() + ".ydk");
} else { } else {
if (!dir.exists()) { IOUtils.createFolder(dir);
dir.mkdirs();
}
} }
return file; return file;
} }
...@@ -180,10 +178,7 @@ public class GameUriManager { ...@@ -180,10 +178,7 @@ public class GameUriManager {
ParcelFileDescriptor pfd = null; ParcelFileDescriptor pfd = null;
FileInputStream input = null; FileInputStream input = null;
try { try {
File dir = local.getParentFile(); IOUtils.createFolder(local.getParentFile());
if (!dir.exists()) {
dir.mkdirs();
}
if (remoteFile != null) { if (remoteFile != null) {
FileUtils.copyFile(remoteFile, local); FileUtils.copyFile(remoteFile, local);
} else { } else {
......
...@@ -21,6 +21,8 @@ import java.io.File; ...@@ -21,6 +21,8 @@ import java.io.File;
import cn.garymb.ygomobile.lite.R; import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.ui.activities.BaseActivity; 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 public class FileActivity extends BaseActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener
...@@ -82,7 +84,7 @@ public class FileActivity extends BaseActivity implements AdapterView.OnItemClic ...@@ -82,7 +84,7 @@ public class FileActivity extends BaseActivity implements AdapterView.OnItemClic
return; return;
} }
File dir = new File(mFileAdapter.getCurPath(), name); File dir = new File(mFileAdapter.getCurPath(), name);
dir.mkdirs(); IOUtils.createFolder(dir);
if (dir.isDirectory()) { if (dir.isDirectory()) {
mFileAdapter.setPath(dir.getAbsolutePath()); mFileAdapter.setPath(dir.getAbsolutePath());
} }
......
...@@ -24,6 +24,7 @@ import cn.garymb.ygomobile.lite.R; ...@@ -24,6 +24,7 @@ import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.ui.activities.BaseActivity; import cn.garymb.ygomobile.ui.activities.BaseActivity;
import cn.garymb.ygomobile.ui.plus.DialogPlus; import cn.garymb.ygomobile.ui.plus.DialogPlus;
import cn.garymb.ygomobile.ui.plus.VUiKit; import cn.garymb.ygomobile.ui.plus.VUiKit;
import cn.garymb.ygomobile.utils.FileUtils;
import cn.garymb.ygomobile.utils.IOUtils; import cn.garymb.ygomobile.utils.IOUtils;
import ocgcore.DataManager; import ocgcore.DataManager;
import ocgcore.data.Card; import ocgcore.data.Card;
...@@ -261,10 +262,7 @@ public class ImageUpdater implements DialogInterface.OnCancelListener { ...@@ -261,10 +262,7 @@ public class ImageUpdater implements DialogInterface.OnCancelListener {
if (file.exists()) { if (file.exists()) {
file.delete(); file.delete();
} else { } else {
File dir = file.getParentFile(); IOUtils.createFolder(file.getParentFile());
if (!dir.exists()) {
dir.mkdirs();
}
} }
file.createNewFile(); file.createNewFile();
mConnection = (HttpURLConnection) new URL(url).openConnection(); mConnection = (HttpURLConnection) new URL(url).openConnection();
......
...@@ -324,9 +324,7 @@ public class ResCheckTask extends AsyncTask<Void, Integer, Integer> { ...@@ -324,9 +324,7 @@ public class ResCheckTask extends AsyncTask<Void, Integer, Integer> {
File dirFile = null; File dirFile = null;
for (String dir : dirs) { for (String dir : dirs) {
dirFile = new File(mSettings.getResourcePath(), dir); dirFile = new File(mSettings.getResourcePath(), dir);
if (!dirFile.exists()) { IOUtils.createFolder(dirFile);
dirFile.mkdirs();
}
} }
} }
......
...@@ -5,6 +5,7 @@ import android.net.Uri; ...@@ -5,6 +5,7 @@ import android.net.Uri;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import java.io.BufferedReader; import java.io.BufferedReader;
...@@ -101,10 +102,7 @@ public class FileUtils { ...@@ -101,10 +102,7 @@ public class FileUtils {
public static void copyFile(InputStream in, File out) throws IOException { public static void copyFile(InputStream in, File out) throws IOException {
FileOutputStream outputStream = null; FileOutputStream outputStream = null;
File dir = out.getParentFile(); IOUtils.createFolder(out.getParentFile());
if (!dir.exists()) {
dir.mkdirs();
}
try { try {
outputStream = new FileOutputStream(out); outputStream = new FileOutputStream(out);
copy(in, outputStream); copy(in, outputStream);
...@@ -117,10 +115,7 @@ public class FileUtils { ...@@ -117,10 +115,7 @@ public class FileUtils {
FileOutputStream outputStream = null; FileOutputStream outputStream = null;
FileInputStream inputStream = null; FileInputStream inputStream = null;
try { try {
File dir = out.getParentFile(); IOUtils.createFolder(out.getParentFile());
if (!dir.exists()) {
dir.mkdirs();
}
inputStream = new FileInputStream(in); inputStream = new FileInputStream(in);
outputStream = new FileOutputStream(out); outputStream = new FileOutputStream(out);
copy(inputStream, outputStream); copy(inputStream, outputStream);
...@@ -134,7 +129,11 @@ public class FileUtils { ...@@ -134,7 +129,11 @@ public class FileUtils {
return true; 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) { if (!isName) {
...@@ -155,16 +154,7 @@ public class FileUtils { ...@@ -155,16 +154,7 @@ public class FileUtils {
} }
public static void moveFile(String oldPath, String newPath) throws IOException {
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];
}
FileInputStream fis = new FileInputStream(oldPath); FileInputStream fis = new FileInputStream(oldPath);
FileOutputStream fos = new FileOutputStream(newPath); FileOutputStream fos = new FileOutputStream(newPath);
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
...@@ -199,20 +189,18 @@ public class FileUtils { ...@@ -199,20 +189,18 @@ public class FileUtils {
if (filePath == null) if (filePath == null)
return; return;
if (!(new File(newPath)).exists()) { IOUtils.createFolder(new File(newPath));
(new File(newPath)).mkdirs();
}
for (int i = 0; i < filePath.length; i++) { for (String path : filePath) {
if ((new File(oldPath + file.separator + filePath[i])).isDirectory()) { File src = new File(oldPath, path);
copyDir(oldPath + file.separator + filePath[i], newPath + file.separator + filePath[i], false); 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());
} }
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);
} }
} }
} }
......
...@@ -7,6 +7,8 @@ import android.os.Build; ...@@ -7,6 +7,8 @@ import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import androidx.annotation.Nullable;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
...@@ -160,17 +162,17 @@ public class IOUtils { ...@@ -160,17 +162,17 @@ public class IOUtils {
} }
public static void createFolderByFile(File file) { public static void createFolderByFile(File file) {
File dir = file.getParentFile(); createFolder(file.getParentFile());
if (dir != null && !dir.exists()) {
dir.mkdirs();
}
} }
public static boolean createFolder(File file) { public static boolean createFolder(@Nullable File file) {
if(file == null){
return false;
}
if (!file.exists()) { if (!file.exists()) {
return file.mkdirs(); return file.mkdirs();
} }
return false; return true;
} }
public static void copy(InputStream in, OutputStream out) throws IOException { public static void copy(InputStream in, OutputStream out) throws IOException {
......
...@@ -228,10 +228,11 @@ public class YGODialogUtil { ...@@ -228,10 +228,11 @@ public class YGODialogUtil {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
du.dis(); du.dis();
DeckType toType = otherType.get(position); DeckType toType = otherType.get(position);
IOUtils.createFolder(new File(toType.getPath()));
List<DeckFile> deckFileList = deckAdp.getSelectList(); List<DeckFile> deckFileList = deckAdp.getSelectList();
for (DeckFile deckFile : deckFileList) { for (DeckFile deckFile : deckFileList) {
try { try {
FileUtils.moveFile(deckFile.getPath(), toType.getPath(), false); FileUtils.moveFile(deckFile.getPath(), new File(toType.getPath(), deckFile.getName()).getPath());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -257,10 +258,11 @@ public class YGODialogUtil { ...@@ -257,10 +258,11 @@ public class YGODialogUtil {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
du.dis(); du.dis();
DeckType toType = otherType.get(position); DeckType toType = otherType.get(position);
IOUtils.createFolder(new File(toType.getPath()));
List<DeckFile> deckFileList = deckAdp.getSelectList(); List<DeckFile> deckFileList = deckAdp.getSelectList();
for (DeckFile deckFile : deckFileList) { for (DeckFile deckFile : deckFileList) {
try { try {
FileUtils.copyFile(deckFile.getPath(), toType.getPath(), false); FileUtils.copyFile(deckFile.getPath(), new File(toType.getPath(), deckFile.getName()).getPath());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); 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