android reading from a text file

后端 未结 3 1081
攒了一身酷
攒了一身酷 2020-12-20 05:44

I have a java class where it reads some data from a text file using a buffered reader and returns that data as a hash map:

import java.io.BufferedReader;
imp         


        
3条回答
  •  萌比男神i
    2020-12-20 06:24

    I think the error might well be there :

    BufferedReader bf = new BufferedReader(new FileReader("unigramFrequencies.txt"));
    

    You should provide an absolute path here and first make sure that the file exists before accessing it or handle the exception.

    If this file is some final asset, you should place it in your project assets folder and get a filereader from there.

    Example (from here):

    AssetFileDescriptor descriptor = getAssets().openFd("unigramFrequencies.txt");
    FileReader reader = new FileReader(descriptor.getFileDescriptor());
    

    Note that your unigramFrequencies.txt file should be present in your /assets/ directory

提交回复
热议问题