public static void main(String[] args) {
try {
System.out.println(System.in);
FileReader fileReader = new FileReader("D:\\data.txt");
BufferedReader buf = new BufferedReader(fileReader);
int i = 0;
String readLine = "";
String[] myArray = new String[500]; //100:这个值你自己定义,但不宜过大,要根据你文件的大小了,或者文件的行数
while((readLine = buf.readLine()) != null){
myArray[i] = readLine;
if("33333".equalsIgnoreCase(readLine)){
myArray[i]="aaaaa";
}
i++;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
这个是我一个算法中的读取代码,功能是读取单词,并存放到数组里:
public static String[][] readNodes(int lnum) {
File file = new File(
"C:\\Users\\afads\\Desktop\\1.txt");
BufferedReader reader = null;
String[][] nodes = new String[lnum][];
String[] lines;
try {
//System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String line = null;
int x=0;
// 一次读入一行,直到读入null为文件结束
while ((line = reader.readLine()) != null && x
// 显示行号
//System.out.println("line " + lnum + ": " + line);
lines = line.split(",");
// for (int i = 0; i < lines.length; i++) {
// System.out.println("一维 :" + lines[i] + " ");
// }
nodes[x] = new String[lines.length];
for (int y = 0; y < lines.length; y++) {
nodes[x][y] = lines[y];
}
x++;
//lines = null;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return nodes;
}
你可以把里面的注释放开,看看数组里存放的单词。