-1

i have a question about Broken text when android app is reading large size text file.

I am trying to build the app to read large size text file(about 10mb)

when I am reading a file and using System.println to check the contents of text file

However, when I display message but print statement

it displays broken text such as..

��T��h��e�� ��P��r��o��j��e��c��t�� ��G��u

when I was reading small size of rtf was find, but i used text file then i made problems

I used code like ..

          String UTF8 = "utf8"; 
            int BUFFER_SIZE = 8192;
            File gone = new File(path); 
             FileInputStream inputStream = new FileInputStream(gone);  
           // FileInputStream inputStream  = openFileInput(gone); 
            if ( inputStream != null ) {

                InputStreamReader inputStreamReader = new InputStreamReader(inputStream,UTF8);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader, BUFFER_SIZE);
                String receiveString = "";
                StringBuilder stringBuilder = new StringBuilder();

                while ( (receiveString = bufferedReader.readLine()) != null ) {
                    stringBuilder.append(receiveString);
                }

                inputStream.close();

                ret = stringBuilder.toString();
                System.out.println(ret);
            }

I was thinking about that it can be problem of encoding. there fore i added utf8 option.

However, it still doesn't work ..

Does anyone know solution of broken text ?

UPDATE: I think, I solved problem. I create new text file from window text editor and then i copy and paste content. Now , it is reading file correctly

Dc Redwing
  • 1,681
  • 11
  • 32
  • 42

2 Answers2

0

It may be wrong encoding for the given file, may be the file does not contain text, may be console does not support the characters.

Besides the code is too long, here's a one line solution

String s  = new String(Files.readAllBytes(Paths.get(file)), "UTF-8");
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

The file may contain images or unsupported format, in that case it'll display like that.

Akash
  • 1
  • 2