0
我是android編程的新手。我正在創建一個應用程序,我應該從自定義庫中獲取文件的絕對路徑並將該文件移動到私有文件夾。我搜查了很多,但我無法得到任何完美的答案。請幫我編寫代碼。謝謝。獲取絕對路徑並移動文件
我是android編程的新手。我正在創建一個應用程序,我應該從自定義庫中獲取文件的絕對路徑並將該文件移動到私有文件夾。我搜查了很多,但我無法得到任何完美的答案。請幫我編寫代碼。謝謝。獲取絕對路徑並移動文件
讓你的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