从.txt文件读取到JFrame

时间:2015-12-09 04:25:59

标签: java swing jframe text-files

只是想删除这个问题,抱歉。这是一个愚蠢的问题。

1 个答案:

答案 0 :(得分:1)

您可以使用BufferedReader来读取文件的每一行。然后,您可以使用String#split拆分String分隔符上的~并解析各个元素,例如......

try (BufferedReader br = new BufferedReader(new FileReader(new File("CarLoan.txt")))) {
    String text = null;
    System.out.printf("%-20s | %s%n", "Name", "Price");
    while ((text = br.readLine()) != null) {
        String parts[] = text.split("~");
        String name = parts[0];
        int price = Integer.parseInt(parts[1]);
        System.out.printf("%-20s | %d%n", name, price);
    }
} catch (IOException ex) {
    ex.printStackTrace();
}

打印......

Name                 | Price
Honda Civic          | 10000
Porsche 911          | 50000
Chevrolet Blazer     | 20000
Toyota Camry         | 15000
相关问题