3

I have a function based on FileInputStream that should return a string she reads from the file. But why is the return value - empty. The function's return value should be displayed in MainActivity. This function is part of the service in my app.

public static String UserLoginFile;
public static String UserPassFile;

    public String LoadUserInfopassFromFilePostData() {  
    String FILENAME = "TeleportSAASPass.txt";
    FileInputStream inputStream = null;
    if(UserLoginFile != null){
    try {
        inputStream = openFileInput(FILENAME);
        byte[] reader = new byte[inputStream.available()];
        while (inputStream.read(reader) != -1) {}
        UserPassFile = new String(reader);
 //   Toast.makeText(getApplicationContext(), "GPSTracker " + UserPassFile, Toast.LENGTH_LONG).show();
    } catch(IOException e) {
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
    }
    }
   return UserPassFile;
 }  

Please tell me how to fix my function so that it can return a string which she read from the file.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Alex
  • 368
  • 2
  • 13
  • Have you made sure that you're not catching any exceptions? You could also try a DataInputStream and do a readLine() instead – britzl Jun 04 '13 at 17:43

2 Answers2

2

I would suggest using an input stream reader. Check out http://developer.android.com/reference/java/io/InputStreamReader.html Your code will look something like:

inputStream = openFileInput(FILENAME);
InputStreamReader( inputstream, "UTF_8"), 8);
StringBuilder response = new StringBuilder();
String line;
//Read the response from input stream line-wise

while((line = reader.readLine()) != null){ 
response.append(line).append('\n');
}

//Create response string
result = response.toString();
//Close input stream
reader.close();
edughofaili
  • 133
  • 1
  • 1
  • 7
1

Consider the difference between your code:

        while (inputStream.read(reader) != -1) {}
        UserPassFile = new String(reader);

And this alternative:

        while (inputStream.read(reader) != -1) {
             UserPassFile = new String(reader);
        }

In your case, UserPassFile is assigned from reader after every read() call, including the final one which fails. In the second case, UserPassFile is only assigned after a read which succeeds, and so should still contain a non-empty string to return when the end of the method is reached.

However, you seem to be doing one other odd thing. You assume that you will be able to read the entire .available() amount of data in a single read, which is probably true (at least if you were able to allocate a byte[] that large). However, after reading this data, you try again and see if you fail, which seems unnecessary. It would seem that if you are going to make the assumption that you can read the data in a single attempt, then you should simply close the stream and return once you have done so - there doesn't seem to be a compelling reason to try yet another read which would be expected to fail. Otherwise, if there's a possibility that you can't get all the data in a single read(), you should keep track of how much you have gotten and keep appending until you get a failed read or otherwise decide you are done.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117