回答:
ファイルをコピーして宛先パスに保存するには、以下の方法を使用できます。
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();
}
}
API 19以降では、Java自動リソース管理を使用できます。
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);
}
}
}
}
または、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
に公開させる方法はありますか?それで、ProgressBarでそれを示すことができますか?受け入れられたソリューションでは、whileループで進行状況を計算できますが、ここではその方法を確認できません。
Kotlin拡張機能
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;
}
`
Kotlinを使用すると、はるかに簡単になります。
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
これは、コピー中にエラーが発生した場合に実際に入出力ストリームを閉じるソリューションです。このソリューションは、ストリームのクローズのコピーと処理の両方にApache Commons 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);
}
}