1

im trying to read a txt file downloaded from an arduino server. I can download the file and save it on internal storage, but when i want to read it, i see weird symbols and i can't see all saved text, only 6 o 7 characters. I don't know what happen.

public void downloadFile (String fileDownloadPath, String saveFilePath){

    try{
    File SaveFile = new File(saveFilePath);


    URL u= new URL ("http://169.254.0.1:44300/169.254.0.1");
    URLConnection con=u.openConnection();
    int lengthofContent=con.getContentLength();

    DataInputStream DIStream = new DataInputStream(u.openStream());

    byte [] buffer = new byte[2000];

    DIStream.read(buffer);
    DIStream.close();

    DataOutputStream DOStream = new DataOutputStream(new FileOutputStream(SaveFile));
    DOStream.write(buffer);
    DOStream.flush();
    DOStream.close();
    System.out.println ("o");

    hideProgressIndicator();

}   catch (FileNotFoundException e){
    hideProgressIndicator();

}
    catch (IOException e){
    hideProgressIndicator();

}
 catch (Exception e){

}

}

when i want to read it i use this:

private String readFile() {
    String result = "", line;
    try
    {


    BufferedReader br = new BufferedReader((new FileReader("/data/data/com.example.sensolog/files/LOL.txt")));

    while((line = br.readLine()) != null)
    {
       result += line + "\n";

    }


    System.out.println (result);
    br.close();
    }
    catch(Exception e)
   {
   e.printStackTrace();
   }

   return result ;

}

And the results are the followings :

08-26 15:38:11.498: I/System.out(30593): SERVIDORn�����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������...

  • 2
    Are you reading with the correct encoding? – Estel Aug 26 '14 at 13:57
  • Do you know what encoding the text file is in originally? – Revive Aug 26 '14 at 13:57
  • You are opening the file wrong, look at this: http://stackoverflow.com/questions/5585826/android-files-in-intenal-storage-and-local-directory and http://stackoverflow.com/questions/6030744/android-reading-from-file-openfileinput – EpicPandaForce Aug 26 '14 at 14:11
  • ( I had similar problems here: http://stackoverflow.com/questions/24106734/simplexml-throws-xmlpullparserexception-unterminated-entity-ref-for-no-reason ) – EpicPandaForce Aug 26 '14 at 15:04

2 Answers2

1

Define the encoding of the file while reading, here is an example:

BufferedReader br = new BufferedReader(( new FileReader("/data/data/com.example.sensolog/files/LOL.txt"),"ISO-8859-1"));
Bruno Franco
  • 2,028
  • 11
  • 20
0

You're downloading and storing the file as a DataOutputStream - Which can only be read again by a DataInputStream - Is that your intention? I would save the file as a simple text file, unless you absolutely must save as a binary type?

Further information on DataOutputStream:

Wraps an existing OutputStream and writes big-endian typed data to it. Typically, this stream can be read in by DataInputStream. Types that can be written include byte, 16-bit short, 32-bit int, 32-bit float, 64-bit long, 64-bit double, byte strings, and MUTF-8 encoded strings. from: http://developer.android.com/reference/java/io/DataOutputStream.html

  • i just want download the file and read it , i don't mind the way but i can't .. now, im trying to use DataInputStrem for read it. I will tell you the result when i finish. Thank you – Jose Barrueco Aug 26 '14 at 14:13
  • That's all well and good, but you're still going to get binary data out. Check out this answer: http://stackoverflow.com/questions/17066265/download-a-text-file-on-android - This will explain how to download the file and write it to a text file. Thus being human readable when you read it in. – madhatter Aug 26 '14 at 14:15
  • im having the same problem, weird symbols =S – Jose Barrueco Aug 26 '14 at 14:27
  • DataInputStream file1 = new DataInputStream(new FileInputStream("/data/data/com.example.sensolog/files/LOL.txt")); System.out.println (file1.readLine()); file1.close(); – Jose Barrueco Aug 26 '14 at 14:28
  • As I said, even with DataInputStream, you'll still get binary data returned. Check the question I linked above and that will show you how to download a file and write as text file for easier reading in. – madhatter Aug 26 '14 at 15:05
  • Thank you so much, i got it using the link you provide above. – Jose Barrueco Sep 02 '14 at 15:57