要将文件从一个位置移动到另一个位置,可以使用以下方法:private void moveFile(String inputPath, String outputPath) {
System.out.println(inputPath);
System.out.println(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
out.flush();
out.close();
out = null;
//delete the original file
new File(inputPath).delete();
}catch (FileNotFoundException fnfe1) {
Log.e("File not found exception", fnfe1.getMessage());
} catch (Exception e) {
Log.e("Other Exception", e.getMessage());
}
}
要调用这里方法,可以执行以下操作:moveFile(Environment.getExternalStorageDirectory() +"/download/abc.txt", Environment.getExternalStorageDirectory() +"/abc.txt");
你还需要在你的AndroidManifest.xml 中给出写权限