【发布时间】:2015-04-01 04:06:10
【问题描述】:
我正在尝试接受来自用户的文件(通过 GUI 中的文件浏览器)并逐行读取文件。基本上我的兴趣是知道在字符位置 60 和 80 之间到底写了什么,因为在此基础上我得出一些结论需要做出一些决定(特别是阅读 rinex.o 文件)。
但我收到错误Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: -45
排队:String typeField=line.substring(60,line.length());,无法前进。
提前感谢您的帮助。
void parseRinexOFile(File inputfile) throws FileNotFoundException, IOException{
File obsFile= inputfile;
// if (obsFile.exists()) //Code to test if the file exists.
// System.out.println("Hello, I exist"+ obsFile.getPath());
// Create File Stream, Stream Reader and Bufferreader
streamObs = new FileInputStream(obsFile);
inStreamObs = new InputStreamReader(streamObs);
buffStreamObs = new BufferedReader(inStreamObs);
BufferedReader in = new BufferedReader(new FileReader(obsFile));
String line="";
while((line = in.readLine()) != null)
{
String typeField=line.substring(60,line.length());
typeField=typeField.trim();
if (typeField.equals("RINEX VERSION / TYPE")) {//Check if the observation file identifier is missing else find the version of the file
if (!line.substring(20, 21).equals("O")) {
// Error if observation file identifier was not found
System.err.println("Observation file identifier is missing in file "
+ obsFile.toString() + " header");
ver = 0;
} else if (line.substring(5, 7).equals("3.")){
ver = 3;
} else if (line.substring(5, 9).equals("2.12")){
ver = 212;
} else {
ver = 2;
System.out.println(" Current version:"+ver);
}
}
//System.out.println(line);
}
in.close();
}
【问题讨论】: