我正在尝试复制android中的文件。我有文件的文件路径。我想将它复制到另一个文件名不同的文件夹。我使用下面的代码,但它没有工作。我的文件是视频文件。 我收到错误 -
File source=new File(filepath);
File destination=new File(Environment.getExternalStorageDirectory()+ "/testcopy.mp4");
copyFile(source.getAbsolutePath(),destination.getAbsolutePath());
private void copyFile(String inputPath, String outputPath) {
InputStream in = null;
OutputStream out = null;
try {
//create output directory if it doesn't exist
File dir = new File (outputPath);
if (!dir.exists())
{
dir.mkdirs();
}
in = new FileInputStream(inputPath );
out = new FileOutputStream(outputPath);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file (You have now copied the file)
out.flush();
out.close();
out = null;
} catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
}
catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
以下是我的代码
@Index
答案 0 :(得分:0)
您创建一个名称为" /storage/emulated/0/testcopy.mp4"这里
//create output directory if it doesn't exist
File dir = new File (outputPath);
if (!dir.exists())
{
dir.mkdirs();
}
试试这段代码
//create output directory if it doesn't exist
File dir = (new File (outputPath)).getParentFile();
if (!dir.exists())
{
dir.mkdirs();
}
答案 1 :(得分:0)
它们的主要问题是您正在尝试编写作为目录的文件。要避免此异常,请先创建目录,然后编写文件:
#include <iostream>
using namespace std;
int main() {
int x;
int array[x];
cout << "Enter your numbers: ";
for (int x = 0; x = <char>; ++x) {
cin >> x;
}
cout << x;
return 0;
}