高人们帮我看下这个java 代码被

来源:百度知道 编辑:UC知道 时间:2024/06/22 12:58:34
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class DateT {

public static void main(String[] args) {
int type =0;
int n=0;
int e=0;
TD t=new TD();
String[][] arr=new String[80][100];
String fileName="c:/2DTarget_LeftBackward_L3.dat";
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
String[] temp=null;
int line=3;
while ((tempString = reader.readLine()) != null) {
temp=tempString.split(" ");
int i = 0;
if(temp[i]!=null){
arr[line][i]=temp[i]+"";
i++;
line+=1;}

}
}
catch (IOException i) {
i.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e1) {
}

首先,楼主不是万分感谢,也没有万分啊,只有30分吗

其次,String[][] arr=new String[80][100],这个容器有点不妥,它会在初始化是分配空间并赋值为null

再次,在调用Integer.parseInt方法时 最好先判断参数是否为空,想要进一步增强其健壮性,可以判断参数是否是只有数字构成

代码如下:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DateT {

public static void main(String[] args) {
String[][] arr;
List<String[]> list = new ArrayList<String[]>();
String fileName = "c:/2DTarget_LeftBackward_L3.dat";
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
String[] temp = null;
while ((tempString = reader.readLine()) != null) {
//这里可以用正则表达式,兼容当分隔符为多个空格的情况
temp = tempString.split("[\\s