- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个 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
所以我看到两点
filePath
参数只包含文件名dir
参数的末尾已经没有路径分隔符关于java - Android:使用 FileInputStream 的 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38707923/
这可能有点傻,但我想知道后台操作的区别。 InputStream is = new FileInputStream(filepath); FileInputStream is = new FileIn
优点: 所以,我有这个二进制数据文件(大小 - 恰好 640631 字节),我正试图让 Java 读取它。 我有两个可互换的类实现为读取该数据的层。其中之一使用 RandomAccessFile,效果
问题是当我在 Android Studio 中运行我的程序时,FileInputStream 找不到 c:\poi-test.xls 文件。 我在 Android Studio 中运行的简单测试 ja
Scanner scanner= new Scanner(new File("target.txt")); 和 FileInputStream d = new FileInputStream("tar
我有 @Component public class CodesConverterService { private final FileInputStreamFactory fileInput
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我正在创建一个程序,用于根据 iTunes 播放列表文件从用户的计算机播放音乐。当我尝试根据播放列表文本文件中提供的位置打开音频文件时,它说存在错误。 filename = "Macintosh HD
我需要读取的文件如下所示: 30 15 6 3 12 20 3 4 (没有要点) inputStream 没有读取 30 和 15,但它正在读取所有其他的。 如何让 inputStream 读取第一行
我收到“找不到文件!”无论我做什么。 FileInputStream fin; try { fin = new FileInputStream("foo.txt");
public static void main(String[] args) { try { File f = new File("file.txt");
我正在加载另一个类通过 FileOutputstream 方法保存的文件。无论如何,我想在另一个类中加载该文件,但它要么给我语法错误,要么使我的应用程序崩溃。 我能找到的唯一教程是在同一个类中保存和加
我在 res/raw 中有一个名为“pack.dat”的原始文件。 我可以使用以下代码打开一个InputStream: InputStream fis = null; try {
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我已经对我的代码进行了 klocwork 代码分析。我收到以下错误我终于关闭了输入流即使那样我也收到错误“fileInputStream”在退出时未关闭。 下面是一段代码 LOGGER.log
我想逐个标记地读取 file.txt 文件中的单词,并向每个标记添加词性标记,然后将其写入 file2.text 文件。 file.txt 内容已标记化。这是我的代码。 public class Po
“Dbconnection.java”“db.properties”文件找不到该文件。我向您展示如何获取以下文件。 我的下一个项目目录。 源代码 数据库 DbConnection.java db.pr
如果我将文本文件放在同一项目文件夹中,程序可以毫无问题地读取它。但是我怎样才能让它从我的计算机中的某个地方读取文件(例如:在桌面中) FileInputStream fstream = new Fil
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
这是我将位图转换为字节数组的代码。 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); image.compress
我是 Java 编码的新手,遇到了很多困难。我假设使用从文件中读取的 bufferedreader 编写一个程序,我已经创建了名为“scores.txt”的文件。所以我有一个名为 processFil
我是一名优秀的程序员,十分优秀!