अपने ऐप में मैं एक निश्चित फ़ाइल की एक कॉपी को एक अलग नाम से सहेजना चाहता हूं (जो मुझे उपयोगकर्ता से मिलती है)
क्या मुझे वास्तव में फ़ाइल की सामग्री को खोलने और इसे किसी अन्य फ़ाइल में लिखने की आवश्यकता है?
ऐसा करने का सबसे अच्छा तरीका क्या है?
अपने ऐप में मैं एक निश्चित फ़ाइल की एक कॉपी को एक अलग नाम से सहेजना चाहता हूं (जो मुझे उपयोगकर्ता से मिलती है)
क्या मुझे वास्तव में फ़ाइल की सामग्री को खोलने और इसे किसी अन्य फ़ाइल में लिखने की आवश्यकता है?
ऐसा करने का सबसे अच्छा तरीका क्या है?
जवाबों:
किसी फ़ाइल की प्रतिलिपि बनाने और उसे अपने गंतव्य पथ पर सहेजने के लिए आप नीचे दी गई विधि का उपयोग कर सकते हैं।
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।
वैकल्पिक रूप से, आप फ़ाइल को कॉपी करने के लिए FileChannel का उपयोग कर सकते हैं । यह हो सकता है तेजी से बाइट प्रति विधि की तुलना में जब एक बड़ी फ़ाइल को कॉपी हो। यदि आपकी फ़ाइल 2GB से बड़ी है, तो आप इसका उपयोग नहीं कर सकते।
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();
}
}
}
यह एक उत्तर के लिए बहुत देर हो सकती है लेकिन सबसे सुविधाजनक तरीका उपयोग कर रहा है
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
यहां एक समाधान है जो वास्तव में इनपुट / आउटपुट धाराओं को बंद कर देता है अगर नकल करते समय कोई त्रुटि होती है। यह समाधान अपाचे कॉमन्स IO IOUtils तरीकों का उपयोग प्रतिलिपि बनाने और धाराओं को बंद करने दोनों से निपटने के लिए करता है।
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);
}
}