0

I want to read a text file. For this I am giving a path of the file but its not getting read.

Giving error like : ClassLoader referenced unknown path: /data/app/com.kiranaapp-1/lib/arm

I have saved the text file in helper folder of an app.

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);
    }
}

As I debug to see if file is getting read and strings are stored in an array, but nothing happens.

Help please , Thank you..

Edit :

My attempt to get strings in list, not getting any value in 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;

    }
}
Sid
  • 2,792
  • 9
  • 55
  • 111
  • replace every slash with a double backslash might help – XtremeBaumer Dec 12 '16 at 09:31
  • tried with that too. Did not work. @XtremeBaumer – Sid Dec 12 '16 at 09:31
  • 1
    i think , That file should be placed in Raw resource folder. – Noorul Dec 12 '16 at 09:32
  • the folder where we have layouts and drawabels? @Ahamed – Sid Dec 12 '16 at 09:34
  • http://stackoverflow.com/questions/35926287/classloader-referenced-unknown-path-data-app ? – Guy Dec 12 '16 at 09:34
  • instead of passing the path as string try`new FileReader(new File("E:\\siddhiwork\\KiranaCustomerApp\\app\\src\\main\\java\\com\\kiranacustomerapp\\helper\\itemNames.txt")));` – XtremeBaumer Dec 12 '16 at 09:37
  • You should put the file into raw folder. The file path seems to be a computer path. – Paresh P. Dec 12 '16 at 09:37
  • could you please explain me the path? @Wizard – Sid Dec 12 '16 at 09:38
  • Yes. that's what I'm telling. How would your emulator recognize that file? Put it in the raw folder and try `Scanner` to read a file. – Paresh P. Dec 12 '16 at 09:43
  • i just realized that the filepath you give is does not occur in the error. it seems that you are using another file somewhere with a different path and you call `ClassLoader.getSystemResource` on that file (or something different but of course class loader) – XtremeBaumer Dec 12 '16 at 09:43
  • Store the same file in either assets or raw folder then fetch from there, check ready android answer – Ready Android Dec 12 '16 at 09:59

3 Answers3

1

First of all, Put the file in raw directory under res directory. Now try below code to read file,

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();
}
Paresh P.
  • 6,677
  • 1
  • 14
  • 26
1

You should not give a file path to the computer path. Store file either in assets folder or in raw folder then fetch from there in 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;
    }

To split text with "," format:

String[] sTextArray = sText.replace("\"", "").split(",");
List<String> stringList = new ArrayList<String>(Arrays.asList(sTextArray));
Ready Android
  • 3,529
  • 2
  • 26
  • 40
  • My file contains data like this: "Potato", "rice", "Eggs", "Maggi", "Dryfruits", "Maza", "cold drink", "sauce", "catchup", "coconut oil" @Ready Android – Sid Dec 12 '16 at 10:19
  • Check last lines answer for String array list – Ready Android Dec 12 '16 at 10:33
  • this works but I am trying to add this list to another in an activity. why it isnt working.Please check the edited question. @Ready Android – Sid Dec 12 '16 at 10:41
  • Show me the code how are you adding this stringList to another array list in your activity ? I think you aren't doing exactly what I posted in my answer. Once check again my answer. – Ready Android Dec 12 '16 at 11:05
0

If the file is generated dynamically in cache, you can

File file = getCacheDir() + "FOLDER_PATH_WITH_FILENAME";

Otherwise, save the file in assets folder inside main directory.

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

then, get file using:

InputStream inputStream = getAssets().open("FILE_NAME");
rupinderjeet
  • 2,984
  • 30
  • 54
  • wont fix it because `/data/app/com.kiranaapp-1/lib/arm` does **not** occur in `E:/siddhiwork/KiranaCustomerApp/app/src/main/java/com/kiranacustomerapp/helper/itemNames.txt` – XtremeBaumer Dec 12 '16 at 09:47
  • `E://` is a windows directory address. He should use assets or raw. – rupinderjeet Dec 12 '16 at 09:52