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);
}
}
StringBuffer
? \$\endgroup\$ – Uri Agassi Apr 2 '14 at 18:02StringBuilder
instead ofStringBuffer
.StringBuffer
is use when there are multithreading. \$\endgroup\$ – Marc-Andre Apr 2 '14 at 18:02str = str + "more";
but I wouldn't recommend that, a StringBuilder is really the best way to go. \$\endgroup\$ – Simon Forsberg Apr 2 '14 at 18:13char[]
of course. \$\endgroup\$ – Simon Forsberg Apr 2 '14 at 18:14