This is my code which receives a file to read and converts binary values to ASCII text.The code works fine, so I am looking for a review on what I have written, and suggestions on how I could do it better, perhaps without the use of a StringBuffer.import java.io.File;import java.util.Scanner;/** * * @author Tumpi */public class DN6 {public static void main(String[] args)        throws Exception {    File file = new File(sporocilo.txt);    Scanner sc = new Scanner(file);    String lastString = ;    while (sc.hasNextLine()) {        String line = sc.nextLine();        lastString = lastString + line;    }        StringBuffer result = new StringBuffer();        for (int i = 0; i < lastString.length(); i += 8) {            result.append((char) Integer.parseInt(lastString.substring(i, i + 8), 2));        }        System.out.println(result);    } }
Read and convert binary files to ASCII text
java;strings
Aside from turning StringBuffer into StringBuilder, there are a few more things I'd like to note about your code.Compound assignment operatorThis line: lastString = lastString + line;Can be rewritten as this one: lastString += line;It's short and it complies with what many people would expect: a concatenation. By explicitly mentioning the original variable again, many people might think it is a different variable before looking at it more closely.Additionally, you might also want to add a StringBuilder to the reading of the file. Even if the inputfile isn't that big, it would still be consistent.Magic valuesConsider this code:for (int i = 0; i < lastString.length(); i += 8) {     result.append((char) Integer.parseInt(lastString.substring(i, i + 8), 2));}Can someone other than the authro tell with certainty what the value 8 is? An educated guess would lead to the bit representation of 2 bytes, but we shouldn't have to guess that. Get rid of this so called magic value by adding a variable which clears it up:int amountOfBits = 8;for (int i = 0; i < lastString.length(); i += amountOfBits ) {     result.append((char) Integer.parseInt(lastString.substring(i, i + amountOfBits ), 2));}Exception handlingYou're completely bypassing exception handling. I'm assuming this is just so this contrived sample wouldn't get cluttered with code, but just in case it isn't:throws Exceptionis always a bad idea in an actual program. You should catch the exact exception and handle it appropriately. 