AndroidStudio 找不到 CSV 文件。尝试过绝对文件路径和相对文件路径
AndroidStudio can't find CSV file. Tried Absolute File Path & Relative File Path
我创建了一个向用户显示文本的简单 Android 应用程序。
现在我正在尝试实现 CSVReader 以从 CSV 文件中检索文本。经过几个小时尝试不同的事情。
我终于实现了一个开源 CSVReader
(至少它不再给我任何编译错误)。
现在,当我 运行 应用程序崩溃时,我得到一个 "file not found" 异常。我没有将我的 CSV 文件放在正确的位置,我没有引用正确的文件路径,或者两者都有。
我试过使用绝对文件路径(例如,从我的 C:/Users/Tim/AndroidStudioProjects/...
开始)。
我也试过使用以 Assets 文件夹开头的相对路径(例如 "Assets/SOCIAL_POSTS.csv"
),但我试过的都没有用。
我已经尝试在 Stack Overflow 上寻找类似的问题,并且我已经尝试了多种文件路径变体,但到目前为止没有任何效果。你认为我接下来应该尝试什么?
这是 GitHub 上的这个项目的 link。
指向CSV文件的代码在
下
app > src > main > java > com > example > tim > inspiredquestions
CSV 文件名为 SOCIAL_POSTS.csv
,位于
下
Assets > SOCIAL_POSTS.csv
最后说明:我已经使用 Whosebug 帮助调试一年了,但这是我问的第一个问题。
感谢您的耐心等待!我正在努力尽可能地自力更生,但我正在冒险寻求帮助。我确定这个问题有一个我忽略的简单答案。
我终于找到了答案。我加了
final CSVReader csvReader = new CSVReader(new InputStreamReader(c.getAssets().open("SOCIAL_POSTS.csv") 在查看 this question 的答案后,我在 Game.java 中加载函数。以前我已将我的 CSV 文件放在 app/src/main 下的资产文件夹中,但在查看 SO post.
之前我不知道如何正确访问该文件
我如何使用 Android Studio 处理文件是将我的文件放在原始文件夹中,然后将它们推送到 /data/data/yourapplication/files 目录。我通过 Tools/Android/Android 设备监视器执行此操作并导航到 /data/data/yourapplication/files 目录和 select 右上角的图标 "Push a file onto the device",然后导航到我的应用程序中的原始文件select 我想要放置在 /files 目录中的 csv 文件。
从那时起,我将我的流 readers 瞄准那个目录,
在我的例子中,/data/data/app/com.android/example/darrell/MenuPlanner/files 目录。此处处理的文件使用文件扩展名,不需要指定路径:
public List<EntreeData> ReadEntreesFromFilesDir(Context inContext) {
this.mContext = inContext;
List<String> lines = new ArrayList<String>();
String[] separated;
try {
FileInputStream fis = mContext.openFileInput("entrees.csv");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
while ((mLine = bufferedReader.readLine()) != null) {
if (mLine == null) break;
lines.add(mLine);
}
fis.close();
isr.close();
} catch (FileNotFoundException e) {
Log.d("FILE ERROR", e.getMessage() + " not found");
} catch (IOException ioe) {
Log.d("FILE ERROR", ioe.getMessage());
}
for (int x = 0;x<lines.size();x++){
separated = lines.get(x).split(",");
EntreeData thisEntree = new EntreeData();
thisEntree.setEntreeName(separated[0].trim());
thisEntree.setCategory(separated[1].trim());
thisEntree.setSubcategory(separated[2].trim());
thisEntree.setRecipe(separated[3].trim());
mEntreeList.add(thisEntree);
}
return mEntreeList;
}
作为后备,我从 /res/raw 目录中读取了不可写的内容,我确信文件的完整性。此处的文件不使用扩展名,因此将您的流 reader 指向一个文件而不使用扩展名。
还可以使用资产管理器访问这些文件:
public List<EntreeData> ReadEntreesFileFromRaw(Context inContext) {
this.mContext = inContext;
List<String> lines = new ArrayList<String>();
String[] separated;
AssetManager assetManager = mContext.getAssets();
try {
InputStream inputStream = mContext.getResources().openRawResource(R.raw.entrees);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
int lineCount = 0;
while ((mLine = reader.readLine()) != null) {
separated = mLine.split(",");
EntreeData entreeEntry = new EntreeData();
if (mLine == null) break;
separated = mLine.split(",");
entreeEntry.setEntreeName(separated[0].trim());
entreeEntry.setCategory(separated[1].trim());
entreeEntry.setSubcategory(separated[2].trim());
entreeEntry.setRecipe(separated[3].trim());
mEntreeList.add(lineCount, entreeEntry);
lineCount++;
}
inputStream.close();
} catch (FileNotFoundException e) {
Log.d("FILE ERROR", e.getMessage() + " not found");
} catch (IOException ioe) {
Log.d("FILE ERROR", ioe.getMessage());
}
return mEntreeList;
}
祝你好运,编程愉快!
我创建了一个向用户显示文本的简单 Android 应用程序。
现在我正在尝试实现 CSVReader 以从 CSV 文件中检索文本。经过几个小时尝试不同的事情。
我终于实现了一个开源 CSVReader
(至少它不再给我任何编译错误)。
现在,当我 运行 应用程序崩溃时,我得到一个 "file not found" 异常。我没有将我的 CSV 文件放在正确的位置,我没有引用正确的文件路径,或者两者都有。
我试过使用绝对文件路径(例如,从我的 C:/Users/Tim/AndroidStudioProjects/...
开始)。
我也试过使用以 Assets 文件夹开头的相对路径(例如 "Assets/SOCIAL_POSTS.csv"
),但我试过的都没有用。
我已经尝试在 Stack Overflow 上寻找类似的问题,并且我已经尝试了多种文件路径变体,但到目前为止没有任何效果。你认为我接下来应该尝试什么?
这是 GitHub 上的这个项目的 link。
指向CSV文件的代码在
下app > src > main > java > com > example > tim > inspiredquestions
CSV 文件名为 SOCIAL_POSTS.csv
,位于
Assets > SOCIAL_POSTS.csv
最后说明:我已经使用 Whosebug 帮助调试一年了,但这是我问的第一个问题。
感谢您的耐心等待!我正在努力尽可能地自力更生,但我正在冒险寻求帮助。我确定这个问题有一个我忽略的简单答案。
我终于找到了答案。我加了 final CSVReader csvReader = new CSVReader(new InputStreamReader(c.getAssets().open("SOCIAL_POSTS.csv") 在查看 this question 的答案后,我在 Game.java 中加载函数。以前我已将我的 CSV 文件放在 app/src/main 下的资产文件夹中,但在查看 SO post.
之前我不知道如何正确访问该文件我如何使用 Android Studio 处理文件是将我的文件放在原始文件夹中,然后将它们推送到 /data/data/yourapplication/files 目录。我通过 Tools/Android/Android 设备监视器执行此操作并导航到 /data/data/yourapplication/files 目录和 select 右上角的图标 "Push a file onto the device",然后导航到我的应用程序中的原始文件select 我想要放置在 /files 目录中的 csv 文件。 从那时起,我将我的流 readers 瞄准那个目录, 在我的例子中,/data/data/app/com.android/example/darrell/MenuPlanner/files 目录。此处处理的文件使用文件扩展名,不需要指定路径:
public List<EntreeData> ReadEntreesFromFilesDir(Context inContext) {
this.mContext = inContext;
List<String> lines = new ArrayList<String>();
String[] separated;
try {
FileInputStream fis = mContext.openFileInput("entrees.csv");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
while ((mLine = bufferedReader.readLine()) != null) {
if (mLine == null) break;
lines.add(mLine);
}
fis.close();
isr.close();
} catch (FileNotFoundException e) {
Log.d("FILE ERROR", e.getMessage() + " not found");
} catch (IOException ioe) {
Log.d("FILE ERROR", ioe.getMessage());
}
for (int x = 0;x<lines.size();x++){
separated = lines.get(x).split(",");
EntreeData thisEntree = new EntreeData();
thisEntree.setEntreeName(separated[0].trim());
thisEntree.setCategory(separated[1].trim());
thisEntree.setSubcategory(separated[2].trim());
thisEntree.setRecipe(separated[3].trim());
mEntreeList.add(thisEntree);
}
return mEntreeList;
}
作为后备,我从 /res/raw 目录中读取了不可写的内容,我确信文件的完整性。此处的文件不使用扩展名,因此将您的流 reader 指向一个文件而不使用扩展名。 还可以使用资产管理器访问这些文件:
public List<EntreeData> ReadEntreesFileFromRaw(Context inContext) {
this.mContext = inContext;
List<String> lines = new ArrayList<String>();
String[] separated;
AssetManager assetManager = mContext.getAssets();
try {
InputStream inputStream = mContext.getResources().openRawResource(R.raw.entrees);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
int lineCount = 0;
while ((mLine = reader.readLine()) != null) {
separated = mLine.split(",");
EntreeData entreeEntry = new EntreeData();
if (mLine == null) break;
separated = mLine.split(",");
entreeEntry.setEntreeName(separated[0].trim());
entreeEntry.setCategory(separated[1].trim());
entreeEntry.setSubcategory(separated[2].trim());
entreeEntry.setRecipe(separated[3].trim());
mEntreeList.add(lineCount, entreeEntry);
lineCount++;
}
inputStream.close();
} catch (FileNotFoundException e) {
Log.d("FILE ERROR", e.getMessage() + " not found");
} catch (IOException ioe) {
Log.d("FILE ERROR", ioe.getMessage());
}
return mEntreeList;
}
祝你好运,编程愉快!