I'm building a FileManager class to handle all of my file management needs. Ideally, this FileManager should rely entirely on methods provided by the Android SDK. I'm running into a road block as far as properly managing subdirectories. When I attempt to load a file using a new FileInputStream object, I'm met with a FileNotFoundException. If I attempt to do so using the context.openFileInput with a custom filepath, I'm met with an IllegalArgumentException related to my path separators.
Here are my writefile and readfile methods. All Directory strings passed to these methods are subdirectories in internal storage (such as data/user/0/com.app.package/files/recordings, where recordings is my created subdirectory). :
/**
* 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;
}
I need to be able to read, write, and delete text files and their containing directories in internal memory with consistency, and I'm quite confused as to what I'm doing wrong, as I know that should be relatively simple. Some information as to why these methods are not behaving as expected would be greatly appreciated.