加载文本文件会出现路径错误

时间:2016-12-12 09:30:32

标签: java android file path

我想读一个文本文件。为此,我给出了文件的路径,但它没有被阅读。

给出错误:ClassLoader引用未知路径:/data/app/com.kiranaapp-1/lib/arm

我已将文本文件保存在应用的帮助文件夹中。

public void ReadFile() {

    try {
        BufferedReader in = new BufferedReader(new FileReader("E:/siddhiwork/KiranaCustomerApp/app/src/main/java/com/kiranacustomerapp/helper/itemNames.txt"));
        String str;

        List<String> list = new ArrayList<String>();
        while ((str = in.readLine()) != null) {
            list.add(str);
        }

        String[] stringArr = list.toArray(new String[0]);
    }
    catch (FileNotFoundException e)
    {
        System.out.print(e);
    }
    catch (IOException e)
    {
        System.out.print(e);
    }
}

当我调试以查看文件是否被读取并且字符串存储在数组中时, 但没有任何反应。

请帮助,谢谢..

编辑:

我尝试在列表中获取字符串,而不是在itemList中获取任何值

public class StartupActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



    List<String> itemList = new ArrayList<>();

    itemList = readRawTextFile(StartupActivity.this);

    }

    public static List<String> readRawTextFile(Context context) {
        String sText = null;
        List<String> stringList;
     try{

        InputStream is = context.getResources().openRawResource(R.raw.item_names);
        //Use one of the above as per your file existing folder

        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        sText = new String(buffer, "UTF-8");

        stringList = new ArrayList<String>(Arrays.asList(sText.split(" ")));
         System.out.print(stringList);
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return stringList;

    }
}

3 个答案:

答案 0 :(得分:1)

首先,将文件放在raw目录下的res目录下。 现在尝试下面的代码来读取文件,

public static String readRawTextFile(Context ctx, int resId) {
    InputStream inputStream = ctx.getResources().openRawResource(resId);

    InputStreamReader inputreader = new InputStreamReader(inputStream);
    BufferedReader buffreader = new BufferedReader(inputreader);
    String line;
    StringBuilder text = new StringBuilder();

    ArrayList<String> lineList = new ArrayList<>();
    try {
        while (( line = buffreader.readLine()) != null) {
            text.append(line);
            lineList.add(line);
            text.append('\n');
        }
    } catch (IOException e) {
        return null;
    }

    // Use your arraylist here, since its filled up.
    return text.toString();
}

答案 1 :(得分:1)

您不应该提供计算机路径的文件路径。将文件存储在assets文件夹或raw文件夹中,然后从android中获取。

public String loadTextFromFile() {
        String sText = null;
        try {
            //If your file is in assets folder
            InputStream is = getAssets().open("file_name.txt");
            //If your file is in raw folder
            InputStream is = getResources().openRawResource(R.raw.file_name);
            //Use one of the above as per your file existing folder

            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            sText = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return sText;
    }

用“,”格式分割文字:

String[] sTextArray = sText.replace("\"", "").split(",");
List<String> stringList = new ArrayList<String>(Arrays.asList(sTextArray));

答案 2 :(得分:0)

如果文件是在缓存中动态生成的,则可以

DECLARE @results VARCHAR(1000) = '' 
SELECT @results = @results + 
           ISNULL(CASE WHEN LEN(@results) = 0 THEN '' ELSE ',' END + [StudentId], '')
FROM Student WHERE condition = xyz

select @results

否则,将文件保存在主目录内的assets文件夹中。

File file = getCacheDir() + "FOLDER_PATH_WITH_FILENAME";

然后,使用以下方式获取文件:

main
-----> java
-----> res
-----> assets
-----> AndroidManifest.xml