读书人

读取txt资料

发布时间: 2012-12-21 12:03:49 作者: rapoo

读取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和bprivate 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

读书人网 >编程

热点推荐