Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中读取和标记文件_Java_File_File Io_Bufferedreader_Stringtokenizer - Fatal编程技术网

在Java中读取和标记文件

在Java中读取和标记文件,java,file,file-io,bufferedreader,stringtokenizer,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

我想知道如何从文件中读取一些关于旅行推销员问题的数据。我已经包括了文件的前几行(其余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
int dimension = read dimension of 13509
Coordinate[] xy = create coordinates array, with coordinates of each city
public class Coordinate {
    double x;
    double y;
    public Coordinate(double x, double y) {
        this.x = x;
        this.y = y;
    }
}
我对两件事感兴趣。标注值和城市坐标。显示编号为
1、…、6
的城市(但有13509个),它们的
x
y
坐标相邻。例如,城市4有
x=249238.889
y=806280.556
。 基本上,我希望读取我的文件并按如下方式存储数据:

    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
int dimension = read dimension of 13509
Coordinate[] xy = create coordinates array, with coordinates of each city
public class Coordinate {
    double x;
    double y;
    public Coordinate(double x, double y) {
        this.x = x;
        this.y = y;
    }
}
其中,
坐标
对象定义如下:

    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
int dimension = read dimension of 13509
Coordinate[] xy = create coordinates array, with coordinates of each city
public class Coordinate {
    double x;
    double y;
    public Coordinate(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

我想我需要使用缓冲读取器、一些IO异常和字符串标记器。我是新手,所以我真的不知道如何实现它。我不知道如何具体读取尺寸值以及x和y坐标。有人提出过一些建议吗?

这里是一个基本示例。将在发生更改时更新

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

因此,只有一个巨大的文件在开始时正好包含6行无用的行,其余的是需要解析的数据?@Doc有多个大小类似的文件,但每次执行程序时,我只会读取一个文件。所以本质上,是的,每次程序运行时,只有一个巨大的文件。从这个文件中,我只需要维度整数值和每个城市的坐标。所有其他数据都是无用的。所以有5条无用的线。很多文件。2.一次一个。3.开始时,每一行正好有6行无用的代码。4.有用的数据,直到最后。是吗。对2.对3.在每个文件的开头有5行无用的行。名为
DIMENSION
的行很有用。4.直到最后两行的有用数据(最后两行只是空行)。确定。启动记事本:)这看起来可能有用!我要测试一下!我不确定,但我认为需要在某个地方进行
尝试
捕获
。这到底是怎么回事?