Java文件-I / O无法读取

时间:2018-04-12 19:38:02

标签: java io

我遇到maing java为我读文件的问题。我有一个包含所有丹麦岛屿的.txt文件,但无论如何我都不会在控制台中显示。

这是具有main方法的类。从这里它读取文件并拆分行,以便将数据放入ArrayList。

public class DanishIslandFileReader {

private File inFile;
private List<DanishIsland> islandList;

public DanishIslandFileReader(String fName) {
    inFile = new File(fName);
}

private void readFile() {
    islandList = new ArrayList<DanishIsland>();
    Scanner scan = null;
    try {
        scan = new Scanner(inFile);
    } catch (FileNotFoundException fnfe) {
        System.out.println(fnfe);
    }
    while (scan.hasNext()) {
        String line = scan.nextLine();
        String[] tokens = line.split(" ");

        String name = tokens[0];
        double circ = Double.parseDouble(tokens[1]);
        double area = Double.parseDouble(tokens[2]);
        int addr = Integer.parseInt(tokens[3]);
        int adkm = Integer.parseInt(tokens[4]);

        DanishIsland island = new DanishIsland(name, circ, area, addr, adkm);
        System.out.println(island.toString());
        islandList.add(island);
    }

    scan.close();
}

public List<?> getList() {
    return islandList;
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws FileNotFoundException {
    System.out.println(new File(".").getAbsolutePath());
//        DanishIslandFileReader fr = new DanishIslandFileReader("Islands punktum.txt");
    DanishIslandFileReader fr = new DanishIslandFileReader("Islands komma.txt");
    fr.readFile();

    System.out.println("Result:\n" + fr.getList());

}

}

当分割线时,它会通过另一个类将数据转换为String并将其放入另一个将在consol中打印的Arraylist。

public class DanishIsland {

private String name;
private double circumference;
private double area;
private int addresses;
private int addrPerKm2;

public DanishIsland(String name, double circumference, double area,
        int addresses, int addrPerKm2) {
    super();
    this.name = name;
    this.circumference = circumference;
    this.area = area;
    this.addresses = addresses;
    this.addrPerKm2 = addrPerKm2;
}

public String getName() {
    return name;
}

public double getCircumference() {
    return circumference;
}

public double getArea() {
    return area;
}

public int getAddresses() {
    return addresses;
}

public int getAddrPerKm2() {
    return addrPerKm2;
}

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append(name);
    builder.append("\t");
    builder.append(circumference);
    builder.append("\t");
    builder.append(area);
    builder.append("\t");
    builder.append(addresses);
    builder.append("\t");
    builder.append(addrPerKm2);
    builder.append("\n");
    return builder.toString();
}

}

我没有得到任何错误或异常,程序一直运行,直到它认为列表已被打印。

问题列表为空,我似乎无法从.txt文件中做出任何“sout”。我做错了吗?

Lolland 298,388 1234,96 38919 32
Bornholm 108,047 588,025 27125 46
Falster 145,926 516,268 26654 52
Mors 139,254 361,745 12374 34

1 个答案:

答案 0 :(得分:0)

你试过调试吗? 我认为它没有进入

while (scan.hasNext())

因为scan.hasNext返回false。 这可能是文件权限问题。

这也可能与此有关: Why is hasNext() False, but hasNextLine() is True?