简体   繁体   中英

Reading from a .txt file into a JFrame

Just trying to delete this question, sorry. It was a stupid question.

You could use a BufferedReader to read each line of the file. You could then use String#split to split the String on the ~ delimiter and parse the individual elements, for example...

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();
}

which prints...

Name                 | Price
Honda Civic          | 10000
Porsche 911          | 50000
Chevrolet Blazer     | 20000
Toyota Camry         | 15000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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