我正在構建一個FileManager類來處理我所有的文件管理需求。理想情況下,這個FileManager應完全依賴Android SDK提供的方法。就正確管理子目錄而言,我正遇到一個障礙。當我嘗試使用新的FileInputStream對象加載文件時,遇到FileNotFoundException。如果我嘗試使用帶有自定義文件路徑的context.openFileInput來實現,我遇到了與我的路徑分隔符相關的IllegalArgumentException。Android:FileNotFoundException使用FileInputStream
這裏是我的writefile和readfile方法。所有傳遞給這些方法的目錄字符串都是內部存儲的子目錄(如data/user/0/com.app.package/files/recordings,其中記錄是我創建的子目錄)。 :
/**
* Writes the provided data to a file based on the designated filename and directory
* @param data - Array of Strings to be written to the file
* @param fileName - Relative path of the file being written
* @param dir - Path of the directory where the file will be written
* @throws IOException
*/
private void writeFile(String[] data, String fileName, String dir) throws IOException {
Log.v(LOG_TAG, "writeFile");
if(fileName == null) {
Log.w(LOG_TAG, "No File Path Provided. File Not Written.");
return;
}
/* Creates and Appends the directory to the provided filename */
if(dir != null && dir.length() > 0) {
createDirectory(dir);
fileName = String.format("%s/%s", dir, fileName);
}
/* Opens the Output Stream */
FileOutputStream fileStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
/* Joins String Array into one string to be written to the file */
String text = TextUtils.join(" ", data);
/* Writes the String in bytes to the file stream */
fileStream.write(text.getBytes());
/* Closes the output stream */
fileStream.close();
//This Commented block was part of my previous method of writing files.
/*
OutputStreamWriter outputStream = new OutputStreamWriter(fileStream);
for(int i = 0; i < data.length; i++){
outputStream.write(data[i]);
if(i < data.length - 1)
outputStream.write("\n");
}
outputStream.close();
*/
}
/**
*
* @param filePath - Relative Filepath of the file being accessed
* @param dir - Path of the Directory the file can be found in
* @return - Array of Strings for containing each line of the accessed file.
* @throws IOException
*/
private String[] readFile(String filePath, String dir) throws IOException {
Log.v(LOG_TAG, "readFile");
if(filePath == null)
return null;
/* Updates the Filepath with the directory */
if(dir != null && dir.length() > 0)
filePath = String.format("%s/%s", dir, filePath);
String temp;
ArrayList<String> list = new ArrayList<>();
/* Initializing Input Stream & Reader */
FileInputStream fileStream = new FileInputStream(new File(filePath));
InputStreamReader inputStream = new InputStreamReader(fileStream);
BufferedReader input = new BufferedReader(inputStream);
/* Reads Each line into an Array List */
while((temp = input.readLine()) != null){
list.add(temp);
}
/* Close the Input Streams */
input.close();
inputStream.close();
if(list.size() < 1)
return null;
String[] arr = new String[list.size()];
arr = list.toArray(arr);
return arr;
}
我需要能夠讀取,寫入和刪除文本文件及其與一致性內存包含的目錄,我以很困惑什麼我做錯了,因爲我知道這應該相對簡單。一些關於爲什麼這些方法不像預期那樣行事的信息將不勝感激。
你給你的應用程序訪問文件/文件夾的權限? –
@Robotia - 應用程序始終擁有內部存儲文件夾的權限,除非對方已授予全球訪問權限,否則無法獲得其他人的許可。這更多的是對文件名稱和文件方法濫用的誤解。 –
正如克里斯提到的,我不應該真的需要它,但是我的清單中具有WRITE_EXTERNAL_STORAGE權限,無論如何,我也打算提供此功能(當然,一次只能一步一步)。 –