读取并标记Java中的文件

时间:2019-05-02 18:50:53

标签: java file file-io bufferedreader stringtokenizer

我试图弄清楚如何从文件中读取有关旅行商问题的一些数据。我已经包含了文件的前几行(其余13503行的格式相同,因此我将其删除了)。该文件如下所示:

    NAME : usa12
    COMMENT : Cities with population at least 500 in 
    TYPE : TSP
    DIMENSION : 13509
    EDGE_WEIGHT_TYPE : EUC_2D
    NODE_COORD_SECTION
    1 245552.778 817827.778
    2 247133.333 810905.556
    3 247205.556 810188.889
    4 249238.889 806280.556
    5 250111.111 805152.778
    6 254475.000 804794.444

我对两件事感兴趣。尺寸值和城市坐标。显示了编号为1,..,6的城市(但其中有13509个),它们的xy坐标中的每一个都相邻。例如。城市4具有x=249238.889y=806280.556。 基本上,我想读取我的文件并像这样存储数据:

int dimension = read dimension of 13509
Coordinate[] xy = create coordinates array, with coordinates of each city

coordinate对象的定义如下:

public class Coordinate {
    double x;
    double y;
    public Coordinate(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

我想我需要使用缓冲读取器,一些IO异常和字符串令牌生成器。我是新手,所以我不确定如何实现它。我不知道如何具体读取尺寸值以及x和y坐标。有人建议一些实现吗?

2 个答案:

答案 0 :(得分:1)

这是一个基本示例。如有更改,将进行更新。

import java.util.*;
import java.io.*;
class SO{
    public static void main(String...a)throws Exception{
        System.out.println("Start");


//Read thing
File f = new File("so_data.txt");

Scanner s = new Scanner(f);

int counts = 0;

s.nextLine();//skip 1
s.nextLine();//skip 2
s.nextLine();//skip 3
counts = Integer.parseInt(s.nextLine().split(" ")[2]);//use 4th
s.nextLine();//skip 5
s.nextLine();//skip 6
System.out.println(counts+" : counts");



counts = 6;//DUMMY DATA FOR TEST FILE - REMOVE FOR ACTUAL FILE

Coordinate[] xy = new Coordinate[counts];


int i = 0;

while(i<counts){ // picking exactly the required number of items.

            String line = s.nextLine();
            String[] vals = line.split(" ");

            double x = Double.parseDouble(vals[1]);
            double y = Double.parseDouble(vals[2]);

            Coordinate c =  new Coordinate(x,y);
//          System.out.println(c);
            xy[i++] = c;
        }


for( i = 0;i<xy.length;i++)
            System.out.println("for index "+i+") "+xy[i]);

    }
}
 class Coordinate {
    double x;
    double y;
    public Coordinate(double x, double y) {
        this.x = x;
        this.y = y;
    }
    public String toString(){
        return "Coord:: "+x+" , "+y;
    }
}

so_data.txt

NAME : usa12
COMMENT : Cities with population at least 500 in 
TYPE : TSP
DIMENSION : 13509
EDGE_WEIGHT_TYPE : EUC_2D
NODE_COORD_SECTION
1 245552.778 817827.778
2 247133.333 810905.556
3 247205.556 810188.889
4 249238.889 806280.556
5 250111.111 805152.778
6 254475.000 804794.444

答案 1 :(得分:-1)

这是一个代码段:

  public static void main(String[] args) {
        try {
            File file = new File("input.txt");
            Scanner input = new Scanner(file);
            int dimension = 13509;

            Coordinate[] coordinates = new Coordinate[dimension];
            while (input.hasNext()) {
                coordinates[input.nextInt()-1] = new Coordinate(input.nextDouble(), input.nextDouble());
            }
        } catch (FileNotFoundException ex) {
            System.out.println("Error! file not find");
        }
    }
相关问题