简体   繁体   English

如何在android中获取正确的路径文件

[英]How to get right path files in android

My code is following:: 我的代码如下:

Scanner sc = null;
try {
    sc = new Scanner(new File("assets/mainmenu/readSourceFile.txt"));
} catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    System.out.println(e1.toString());
    e1.printStackTrace();
}

Then, logcat show exception java.io.FileNotFoundException 然后,logcat显示异常java.io.FileNotFoundException

How can I find my file? 我怎样才能找到我的档案? I tried 我试过了

sc = new Scanner(new File("mainmenu/readSourceFile.txt"));

However, it's still throw FilenotFoundException 但是,它仍然抛出FilenotFoundException

My file is in folder assets/mainmenu/readSourceFile.txt 我的文件位于文件夹assets / mainmenu / readSourceFile.txt中

and I've try this:: 我试试这个::

private InputStream is;
try {
        is = this.getResources().getAssets().open("mainmenu/readSourceFile.txt");
    } catch (IOException e) {
        e.toString();
    }

I use getAssets to access assets file in android. 我使用getAssets来访问android中的资产文件。 However, how can I suppose to read text file if I use InputStream 但是,如果我使用InputStream ,我怎么能想读取文本文件

You try this...Hope it will work 你试试这个......希望它能奏效

sc = new Scanner(new File("file:///android_asset/mainmenu/readSourceFile.txt");

Edit try this 编辑尝试这个

AssetFileDescriptor descriptor = getAssets().openFd("mainmenu/readSourceFile.txt");
FileReader reader = new FileReader(descriptor.getFileDescriptor());

Copied from here Asset folder path 从此处复制资产文件夹路径

You can't access the assets folder of your Android application as if they were regular files on the file system (hint: they're not). 您无法访问Android应用程序的assets文件夹,就好像它们是文件系统上的常规文件一样(提示:它们不是)。 Instead, you need to use AssetManager .Call getAssets() on your activity/application context to get the AssetManager . 相反,您需要在活动/应用程序上下文中使用AssetManager .Call getAssets()来获取AssetManager

Once you have this, you can use open("mainmenu/readSourceFile.txt") to obtain an InputStream . 完成后,您可以使用open("mainmenu/readSourceFile.txt")来获取InputStream

You can now read this InputStream like you would any other. 您现在可以像阅读其他任何内容一样阅读此InputStream Apache Commons IO is a great library for working with input and output streams. Apache Commons IO是一个很棒的库,用于处理输入和输出流。 Depending on how you want to get the data, you could try: 根据您想要获取数据的方式,您可以尝试:

  • Read the entire file into a String: 将整个文件读入String:

     try { String contents = IOUtils.toString(in); } finally { IOUtils.closeQuietly(in); } 
  • Read the entire file into a list of Strings, one per line: 将整个文件读入一个字符串列表,每行一个:

     try { List<String> lines = IOUtils.readLines(in); } finally { IOUtils.closeQuietly(in); } 
  • Iterate through the file one line at a time: 一次遍历文件一行:

     try { LineIterator it = IOUtils.lineIterator(in, "UTF-8"); while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { IOUtils.closeQuietly(in); } 

I had the same issue. 我遇到过同样的问题。 I noticed that the file should not include the hierarchy. 我注意到该文件不应包含层次结构。 This worked for me if someone had the same issue in future. 如果将来有人遇到同样的问题,这对我有用。 ` `

    InputStream is=null;

    try {
        is=activity.getAssets().open("fileInAssets.dat");
    } catch (IOException e1) {
        Log.e(TAG, "Erro while openning hosts file.");
        e1.printStackTrace();
    }

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM