我想读取一个文本文件并将其存储为 java 中的字符串多维数组。
输入将是这样的
11 12 13
12 11 16
33 45 6
我想把它存储在
String[][] as={{"11","12","13"},
{"12","11","16"},
{"33","45"}};
我的代码
String file="e:\\s.txt";
try
{
int counterCol=0,counterRow=0;
String[][] d=null;
BufferedReader bw=new BufferedReader(new FileReader(file));
String str=bw.readLine();
String[] words=str.split(",");
System.out.println(words.length+"Counterrow");
counterCol=words.length; //get total words split by comma : column
while(bw.readLine()!=null)
{
counterRow++;
// to get the total words as it gives total row count
}
String[][] d=new String[counterRow][counterCol];
for(int x=0;x<counterRow;x++)
{
for(int y=0;y<counterCol;y++)
{
d[x][y]=bw.readLine();
//storing in array. But here gives me the exception
}
}
但是当我得到空指针异常时,我无法将它存储在数组中。如何克服这个问题