繁体   English   中英

从.txt文件读取到JFrame

[英]Reading from a .txt file into a JFrame

对不起,我只是想删除这个问题。 这是一个愚蠢的问题。

您可以使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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