Java function call and return function in Android

I have a Java class with some code that will work based on the flag value, below my code that works as the flag value is 1

if(flag==1)
{
    Log.d("Flag value", "flag= "+flag);
    System.out.println("Read have "+read());
    String tt=read();
    s1=tt;
}

From this function above, the value in the variable "s1" is some string value returned by the read () function.

the output of this code returns twice the read () function, for example

s1 with "StringString"

Here is my read function code

public String read(){

          try{
             FileInputStream fin = openFileInput(file);
             int c;

             while( (c = fin.read()) != -1)
             {
                temp = temp + Character.toString((char)c);
             }
          }
          catch(Exception e)
          {

          }
          Log.d("INSIDE READ FUNC", "temp have "+temp);
        return temp;
       }

So far I have skipped this "System.out.println (" Read have "+ read ()); below the code

if(flag==1)
    {
        Log.d("Flag value", "flag= "+flag);
        //System.out.println("Read have "+read());
        String tt=read();
        s1=tt;
    }

And I got a great result, for example

s1 having "String"

Why does the code work like this? I called the read () function only once to save the "tt" variable.

And saving the tt variable to the s1 variable.

System.out.println( "Read have" + read()); , "tt" String read() "tt" String.

, String "tt" read() . ?

+4
3
if(flag==1)
    {
        Log.d("Flag value", "flag= "+flag);
        //System.out.println("Read have "+read());
        String tt=read();
        s1=tt;
    }

read() - . read() "temp"

temp = temp + Character.toString((char)c);

concat temp.

, temp

public String read(){

          try{
             FileInputStream fin = openFileInput(file);
             int c;
             String temp="";

             while( (c = fin.read()) != -1)
             {
                temp = temp + Character.toString((char)c);
             }
          }
          catch(Exception e)
          {

          }
          Log.d("INSIDE READ FUNC", "temp have "+temp);
        return temp;
       }
+2
temp = temp + Character.toString((char)c);

temp read(), , , . , , read(), . , temp read():

String temp;

.

+3
InputStream is = Context.openFileInput(someFileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
while ((int bytesRead = is.read(b)) != -1) {
   bos.write(b, 0, bytesRead);
}
byte[] bytes = bos.toByteArray();
0
source

Source: https://habr.com/ru/post/1528235/


All Articles