【发布时间】:2014-10-12 06:43:01
【问题描述】:
我是安卓编程新手。我正在创建一个应用程序,我应该在其中从自定义库中获取文件的绝对路径并将该文件移动到私人文件夹。我搜索了很多,但我无法得到任何完美的答案。请帮我写代码。谢谢。
【问题讨论】:
标签: android file move absolute-path
我是安卓编程新手。我正在创建一个应用程序,我应该在其中从自定义库中获取文件的绝对路径并将该文件移动到私人文件夹。我搜索了很多,但我无法得到任何完美的答案。请帮我写代码。谢谢。
【问题讨论】:
标签: android file move absolute-path
获取 SD 卡的绝对路径:
String SDCARD_PATH = Environment.getExternalStorageDirectory().getPath() + "/";
File dirFolder = new File(path);
File[] folders = dirFolder.listFiles(); // list of all files and folder into this path
for (File file : folders) {
if (file.isDirectory()) {
file.getName() // this will return folder name
} else {
file.getName(); // this will return file name with extension
}
}
在整个过程中执行递归方法。
复制文件:
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = new FileInputStream(filePath + fileNameWithExtension);
// Path to the just created empty db
String outFileName = OUTPUT_PATH + "/" + FILE_NAME;
// Open the empty db as the output stream
new File(outFileName).createNewFile();
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
之后,如果需要,只需删除文件:
String myFile = filePath+"/"+fileNameWithExtension;
if (new File(myFile).delete())
// do something
else
// do something
【讨论】: