Java,使用Scanner逐行获取数据

时间:2015-04-25 18:32:04

标签: java

我基本上运行这段代码:

AddressBook.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package addressbook;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author hassan
 */
public class AddressBook extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
        new AddressBookMapper().parseDataFile();
    }
}

AddressBookMapper.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package addressbook;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
/**
 *
 * @author hassan
 */
public class AddressBookMapper {
    private File dataFile;
    private FileWriter fileWriter;
    private PrintWriter printWriter;
    public ArrayList lines;

    private void openDataFile() {
        this.dataFile = new File("src/addressbook/datafile.txt");
    }
    public void writeData() {

        System.out.println(this.lines);
        Iterator iterator = this.lines.iterator();
        System.out.println(this.lines.get(0));
        while(iterator.hasNext()) {
            System.out.println();
            System.out.println("-----");
        }
    }
    public void createAddress(String fullName, String email, String telephoneNumber, String mobileNumber) {
        this.openDataFile();
        try {
            this.fileWriter = new FileWriter(this.dataFile, true);
            this.printWriter = new PrintWriter(this.fileWriter);
            this.printWriter.append(fullName + "," + email + "," + telephoneNumber + "," + mobileNumber + "\n");
            this.printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public int getLastId() {
        return 0;
    }
public void parseDataFile() {
    File dataFile = new File("src/addressbook/datafile.txt");

    try {
        Scanner scanner = new Scanner(dataFile, "UTF-8").useDelimiter("\\n");

        while(scanner.hasNext()) {
            System.out.println(scanner.next());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    this.writeData();
}
    private int findLastAddressId() {
        return 0;
    }
}

我的数据文件:

ff,ff,fff,ff
krfr,frffr,frfs,ff
a,a,b,c
d,a,f,e
fa,e,f,a

但是,出于某种原因,我得到了这个输出:

ff,ff,fff,ff
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
krfr,frffr,frfs,ff
a,a,b,c
d,a,f,e
fa,e,f,a
null
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NullPointerException
    at addressbook.AddressBookMapper.writeData(AddressBookMapper.java:36)
    at addressbook.AddressBookMapper.parseDataFile(AddressBookMapper.java:69)
    at addressbook.AddressBook.main(AddressBook.java:36)
    ... 11 more
Exception running application addressbook.AddressBook
Java Result: 1

任何解决方案?我基本上试图逐行从我的数据文件中获取文本并将其堆叠到我的ArrayList中。此外,在编写我的数据文件时,在文档的末尾添加一个空行。

1 个答案:

答案 0 :(得分:1)

请你试试 -

while(scanner.hasNext()) {
    lines.add(scanner.next());
}