簡體   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