各位这段代码怎么理解,还有里面的空指向异常是为什么
import java.io.BufferedReader;
import
java.io.FileNotFoundException;
import java.io.FileReader;
import
java.io.IOException;
import java.io.*;
import java.util.Vector;
public class VectorDemo{
public static void main(String a[]) throws Exception {
//File f = File("E:" + File.separator + "data.txt");
String fileName = "data.txt";
Vector<String> vector = getContentVector(fileName);
for(int i = 0; i < vector.size(); i++){
System.out.print(vector.get(i) + " ");
if(i % 7 == 0 && i != 0){
System.out.println();
}
}
}
private static Vector<String> getContentVector(String fileName) throws Exception {
Vector<String> vector = new Vector<String>();
BufferedReader bf = new BufferedReader(new FileReader(fileName));
String content = "";
StringBuilder sb = new StringBuilder();
while(content != null){
content = bf.readLine();
if(content == null){
break;
}
if(!content.trim().equals("")){
sb.append(content.trim().toUpperCase());
}
}
for(String str: sb.toString().split("\\s+")){
vector.add(str);
}
return vector;
}
}
------解决方案--------------------
这个就是把data.txt里的内容转换成大写打印在控制台上,如果你没有data.txt当然会有错。
在E盘建个data.txt,里面写一些字母。改一下String fileName = "E://data.txt";