আমার অ্যাপে আমি একটি নির্দিষ্ট নামের একটি নির্দিষ্ট ফাইলের একটি অনুলিপি সংরক্ষণ করতে চাই (যা আমি ব্যবহারকারীর কাছ থেকে পেয়েছি)
আমার কি সত্যিই ফাইলটির বিষয়বস্তুগুলি খুলতে এবং এটি অন্য কোনও ফাইলে লেখার দরকার আছে?
এটি করার সর্বোত্তম উপায় কী?
আমার অ্যাপে আমি একটি নির্দিষ্ট নামের একটি নির্দিষ্ট ফাইলের একটি অনুলিপি সংরক্ষণ করতে চাই (যা আমি ব্যবহারকারীর কাছ থেকে পেয়েছি)
আমার কি সত্যিই ফাইলটির বিষয়বস্তুগুলি খুলতে এবং এটি অন্য কোনও ফাইলে লেখার দরকার আছে?
এটি করার সর্বোত্তম উপায় কী?
উত্তর:
একটি ফাইল অনুলিপি করতে এবং এটি আপনার গন্তব্য পথে সংরক্ষণ করতে আপনি নীচের পদ্ধতিটি ব্যবহার করতে পারেন।
public static void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
try {
OutputStream out = new FileOutputStream(dst);
try {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
এপিআই 19+ এ আপনি জাভা অটোমেটিক রিসোর্স ম্যানেজমেন্ট ব্যবহার করতে পারেন:
public static void copy(File src, File dst) throws IOException {
try (InputStream in = new FileInputStream(src)) {
try (OutputStream out = new FileOutputStream(dst)) {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
}
}
finally।
বিকল্পভাবে, আপনি ফাইল অনুলিপি করতে ফাইলচেনেল ব্যবহার করতে পারেন । এটা তোলে পারে দ্রুত বাইট কপি পন্থার চেয়ে যখন একটি বড় ফাইল কপি হবে। আপনার ফাইলটি যদিও 2 জিবি থেকে বড় হয় আপনি এটি ব্যবহার করতে পারবেন না।
public void copy(File src, File dst) throws IOException {
FileInputStream inStream = new FileInputStream(src);
FileOutputStream outStream = new FileOutputStream(dst);
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
outStream.close();
}
java.io.FileNotFoundException: /sdcard/AppProj/IMG_20150626_214946.jpg: open failed: ENOENT (No such file or directory)এবং FileOutputStream outStream = new FileOutputStream(dst);পদক্ষেপে ব্যতিক্রম ছাড়া এটি আমার পক্ষে ব্যর্থ । আমি যে পাঠ্যটি অনুধাবন করেছি সে অনুসারে, ফাইলটি বিদ্যমান নেই, তাই আমি এটি যাচাই করি এবং dst.mkdir();প্রয়োজনে কল করি, তবে এটি এখনও সহায়তা করে না। আমিও চেক করার চেষ্টা করেছিলাম এবং dst.canWrite();ফিরে এসেছিল false। এটি কি সমস্যার উত্স হতে পারে? এবং হ্যাঁ, আমি আছে <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>।
try ( FileInputStream inStream = new FileInputStream(src); FileOutputStream outStream = new FileOutputStream(dst) ) {
onProgressUpdate, তাই আমি এটি একটি প্রগতি বারে প্রদর্শন করতে পারি? গৃহীত সমাধানে আমি লুপের অগ্রগতি গণনা করতে পারি, তবে এখানে এটি কীভাবে করব তা আমি দেখতে পাচ্ছি না।
কোটলিন এর জন্য এক্সটেনশন
fun File.copyTo(file: File) {
inputStream().use { input ->
file.outputStream().use { output ->
input.copyTo(output)
}
}
}
contentResolver.openInputStream(uri)।
এগুলি আমার পক্ষে ভাল কাজ করেছে
public static void copyFileOrDirectory(String srcDir, String dstDir) {
try {
File src = new File(srcDir);
File dst = new File(dstDir, src.getName());
if (src.isDirectory()) {
String files[] = src.list();
int filesLength = files.length;
for (int i = 0; i < filesLength; i++) {
String src1 = (new File(src, files[i]).getPath());
String dst1 = dst.getPath();
copyFileOrDirectory(src1, dst1);
}
} else {
copyFile(src, dst);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copyFile(File sourceFile, File destFile) throws IOException {
if (!destFile.getParentFile().exists())
destFile.getParentFile().mkdirs();
if (!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
}
এটি Android ও (এপিআই 26) এ সহজ, যেমন আপনি দেখুন:
@RequiresApi(api = Build.VERSION_CODES.O)
public static void copy(File origin, File dest) throws IOException {
Files.copy(origin.toPath(), dest.toPath());
}
উত্তরের জন্য খুব দেরি হতে পারে তবে সবচেয়ে সুবিধাজনক উপায়টি ব্যবহার করা
FileUtilsএর
static void copyFile(File srcFile, File destFile)
উদাহরণস্বরূপ এটি আমি করেছি
`
private String copy(String original, int copyNumber){
String copy_path = path + "_copy" + copyNumber;
try {
FileUtils.copyFile(new File(path), new File(copy_path));
return copy_path;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
`
কোটলিনের সাথে এখন অনেক সহজ:
File("originalFileDir", "originalFile.name")
.copyTo(File("newFileDir", "newFile.name"), true)
trueবা falseগন্তব্য ফাইল ওভাররাইটিং জন্য
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-file/copy-to.html
এখানে এমন একটি সমাধান রয়েছে যা অনুলিপি করার সময় কোনও ত্রুটি ঘটলে প্রকৃতপক্ষে ইনপুট / আউটপুট স্ট্রিমগুলি বন্ধ করে দেয়। এই সমাধানটি স্ট্রিমের অনুলিপি অনুলিপি এবং পরিচালনা করার জন্য অ্যাপাচি কমন্স আইও আইইউটিস পদ্ধতিগুলি ব্যবহার করে।
public void copyFile(File src, File dst) {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(src);
out = new FileOutputStream(dst);
IOUtils.copy(in, out);
} catch (IOException ioe) {
Log.e(LOGTAG, "IOException occurred.", ioe);
} finally {
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(in);
}
}