简体   繁体   中英

Java/android Not Able to print output of string builder

I have the following code

public static String GetLocalMasterFileStream(String Operation) throws Exception {

String FullPath = "/sdcard/CM3/advices/advice_master.txt";
String line;
String return_value = "";
System.out.println("path is " + FullPath);
    File file = new File(FullPath);
      if (file.canRead() == true) {System.out.println("Determined that file is readable...");}

    //Read text from file
    StringBuilder text = new StringBuilder();
    BufferedReader br = new BufferedReader(new FileReader(file));


if (Operation.equals("PreCheck")) {
    line = br.readLine();
    text.append(line.toString() + "\r\n");

    return_value = String.valueOf(text);
} else {
          //add some other stuff to do later here
       }

    br.close();
    System.out.println("value being returned is> " + return_value);
    return return_value;

}

The line

System.out.println("value being returned is> " + return_value);

SHOULD print to the log the contents of the first line, which is some letters and a number basically in the txt file. It does not. Infact that entire line starting with "value being returned.." is not printed.

If i change it and make return_value = line or just print the variable "line" directly it prints fine and the contents is correct.

Somehow when i put the variable line (or its string value) into the string builder things stop working.

Unless there is a specific reason for you to use the method System.out.println(); , I'd use the android built in function for debugging purposes. In short, I do not know the answer to your question, but I have a suggestion to acquire the same result. Typing the shortcut 'logd' followed by an 'enter' keypress will give you what you need to log whatever you want. 'logt' followed by the 'enter' is required, this combination will give you good and fast information on what you need to know about your running code.

in case anyone is wondering in the future... the \\r breaks string builder.

i changed text.append(line.toString() + "\\r\\n");

to text.append(line.toString()); text.append("\\n");

and it worked fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM