我正在构建一个FileManager类来处理我的所有文件管理需求。理想情况下,这个FileManager应该完全依赖于Android SDK提供的方法。就正确管理子目录而言,我遇到了一个障碍。当我尝试使用新的FileInputStream对象加载文件时,会遇到FileNotFoundException。如果我尝试使用带有自定义文件路径的context.openFileInput来执行此操作,我会遇到与路径分隔符相关的IllegalArgumentException。
以下是我的writefile和readfile方法。传递给这些方法的所有目录字符串都是内部存储中的子目录(如data/user/0.com.app.package/files/recorations,其中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;
}
我需要能够一致地读取、写入和删除内部内存中的文本文件及其包含的目录,我很困惑自己做错了什么,因为我知道这应该相对简单。关于为什么这些方法没有如预期那样表现的一些信息将不胜感激。
在readFile方法中,您可以获得文件路径,我认为它可能看起来像这样:data/user/0/com.app.package/files/recordings/examplefile1.xml
和这个路径目录,在这种情况下可能看起来像这样:CCD_ 2。
然后,您将这两个字符串用路径分隔符连接起来。在我的示例中,这将导致以下结果:data/user/0/com.app.package/files/recordings/data/user/0/com.app.package/files/recordings/examplefile1.xml
假设您按如下方式传入目录:data/user/0/com.app.package/files/recordings/
该路径将产生以下结果:data/user/0/com.app.package/files/recordings//data/user/0/com.app.package/files/recordings/examplefile1.xml
所以我看到两点
- 您必须确保
filePath
参数仅包含文件名 - 您必须确保在
dir
参数的末尾已经没有路径分隔符