gpt4 book ai didi

java - Android:使用 FileInputStream 的 FileNotFoundException

转载 作者:行者123 更新时间:2023-11-30 01:08:03 24 4
gpt4 key购买 nike

我正在构建一个 FileManager 类来处理我所有的文件管理需求。理想情况下,这个 FileManager 应该完全依赖于 Android SDK 提供的方法。就正确管理子目录而言,我遇到了障碍。当我尝试使用新的 FileInputStream 对象加载文件时,我遇到了 FileNotFoundException。如果我尝试使用带有自定义文件路径的 context.openFileInput 这样做,我会遇到与我的路径分隔符相关的 IllegalArgumentException。

这是我的 writefile 和 readfile 方法。传递给这些方法的所有目录字符串都是内部存储中的子目录(例如data/user/0/com.app.package/files/recordings,其中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和这个路径目录,在这种情况下可能如下所示: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

假设您按如下方式传入目录: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

所以我看到两点

  1. 你必须确保 filePath 参数只包含文件名
  2. 您必须确保 dir 参数的末尾已经没有路径分隔符

关于java - Android:使用 FileInputStream 的 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38707923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com