0

I would like to know how can I read a text file from assets character by character.

For example, if I have this file "text.txt" and inside of it "12345", I would like to read all numbers one by one.

I've already looked for this but I can't find any solution.

Thanks.

Th3lmuu90
  • 1,717
  • 2
  • 15
  • 18
  • Are all the characters in the file single-byte? Then just use [this question](http://stackoverflow.com/questions/10039672/android-how-to-read-file-in-bytes) to get an array of bytes, and each of those will represent a character which can be turned to a `String`. – Cat Dec 15 '12 at 20:07
  • I'm already using the example given in this page: http://www.java2s.com/Code/Java/File-Input-Output/Readfilecharacterbycharacter.htm But it does not find my file inside the assets folder. Code: " File file = new File("name.txt"); " – Th3lmuu90 Dec 15 '12 at 21:53

3 Answers3

0

Use getAssets().open("name.txt") to retrieve an InputStream on assets/name.txt, then read it in as you wish.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Thanks CommonsWare for your answer :) Along with my link where I did replied to Eric, I did add your piece of code and here is the result (fully working):

AssetManager manager = getContext().getAssets();
    InputStream input = null;
    try {
        input = manager.open("test.txt");
    } catch (IOException e1) {
        Log.d("ERROR DETECTED", "ERROR WHILE TRYING TO OPEN FILE");
    }
    try {
      char current;
      while (input.available() > 0) {
        current = (char) input.read();
        Log.d("caracter", ""+current);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }

Thanks for your help guys :)

EDIT: The next code will read all file lines while the above not:

AssetManager manager = getContext().getAssets();
    InputStream input = null;
    InputStreamReader in = null;
    try {
        input = manager.open("teste.txt");
        in = new InputStreamReader(input);
    } catch (IOException e1) {
        Log.d("ERROR DETECTED", "ERROR WHILE TRYING TO OPEN FILE");
    }
    try {
      char current;
      while (in.ready()) {
        current = (char) in.read();
        Log.d("caracter", ""+current);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
Th3lmuu90
  • 1,717
  • 2
  • 15
  • 18
0

To read files in character units, often used to read text, Numbers, and other types of files

public static char[] readFileByChars(File file) {
        CharArrayWriter charArrayWriter = new CharArrayWriter();
        char[] tempBuf = new char[100];
        int charRead;

        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            while ((charRead = bufferedReader.read(tempBuf)) != -1) {
                charArrayWriter.write(tempBuf, 0, charRead);
            }
            bufferedReader.close();
            return charArrayWriter.toCharArray();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
Richard Kamere
  • 749
  • 12
  • 10