读取txt文件


/**
* 读取txt文件
*
* @param srcPathName
* @return
*/
public static Vector readText(String srcPathName) {
Vector vc = new Vector();
ArrayList al = null;
double l = 0;
double m = 0;
double x = 0;
double y = 0;
double r = 0;
File inputFile = new File(srcPathName);
try {
BufferedReader br = new BufferedReader(new FileReader(inputFile));
String firstLine = br.readLine();
String singleLine = null;
// 判断取得矩形框的坐标
if (firstLine != null) {
double first[] = dealString(firstLine);
if (first != null && first.length > 0) {
l = first[0];
m = first[1];
} else {
return null;
}
} else {
return null;
}
// 处理余下的基站坐标
while ((singleLine = br.readLine()) != null) {
if (singleLine.length() > 0 && singleLine.contains(";")) {
String[] tempStr = singleLine.split(";");
double xy[] = dealString(tempStr[0]);
if (xy != null && xy.length > 0) {
x = xy[0];
y = xy[1];
} else {
break;
}
try {
r = Double.parseDouble(tempStr[1].trim());
} catch (NumberFormatException e) {
break;
}
}
al = new ArrayList();
al.add(l);
al.add(m);
al.add(x);
al.add(y);
al.add(r);
if (!vc.contains(al)) {
vc.add(al);
}
}
} catch (FileNotFoundException e) {
System.out.println("系统没有找到指定文件" + srcPathName);
System.exit(-1);
} catch (IOException e) {
System.out.println("读取文件失败,可能输入文件有问题,请查看检查。");
System.exit(-1);
}
return vc;
}

处理坐标点:

// 处理坐标点(a,b),得到a和b
private static double[] dealString(String src) {
double[] result = new double[2];
double a = 0;
double b = 0;
if (src != null && src.length() > 0 && src.trim().startsWith("(")
&& src.trim().endsWith(")")) {
String[] temp = src.trim().substring(1, src.length() - 1)
.split(",");
try {
for (int i = 0; i < temp.length; i++) {
result[i] = Double.parseDouble(temp[i].trim());
}
} catch (NumberFormatException e) {
System.out.println("输入文件中的坐标值有误,请查看修改");
return null;
}
} else {
return null;
}
return result;

}


txt文件:

(50,60)
(1,2); 4
(150,100); 5
(210,50); 8
(96,50); 10
(93,50); 10
(200,300); 19
(200,300); 20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值