I want to read text file by using java swing.But the text on JLabel is not the same as the text in the file.Please help with this problem.
This is my text file and expected result:
Zero:100
Van:70
BC:60
But this is my result:
Zero:100 Van:70 BC:50
Here is my code.It can print on the console right but not in JLabel:
JLabel l = new JLabel();
l.setBounds(10, 20, 500, 500);
f.add(l);
File file = new File("src/a.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
StringBuilder cont = new StringBuilder();
String text;
while ((text = reader.readLine()) != null) {
cont.append(text).append(lineSeparator);
}
System.out.println(cont.toString());
l.setText(cont.toString());
} catch (IOException e) {
e.printStackTrace();
}
}