正文之前
一天打鱼九天晒网的毕业设计。感觉快要迎来中期检查了。所以有点慌这两天,哆哆嗦嗦的把一部分代码写完了(大部分都是抄,小部分自己改)。所以为了我的简书存活,到底要把今天写的一个东西放了上来。
正文
其实没什么好说的,就是一个文本读取类。当然,为了体现效果,我还这几在里面留了一个测试的数据。如果本身有数据的话把测试的数据删掉就可以了。这个基本上是通用吧,很多地方都可以用的哦。
import java.io.*;
public class ReadData {
public Object[][] ReadData() throws IOException {
Object[][] rawData = new Object [][]{
{ "<30 ", "High ", "No ", "Fair ", "0" },
{ "<30 ", "High ", "No ", "Excellent", "0" },
{ "30-40", "High ", "No ", "Fair ", "1" },
{ ">40 ", "Medium", "No ", "Fair ", "1" },
{ ">40 ", "Low ", "Yes", "Fair ", "1" },
{ ">40 ", "Low ", "Yes", "Excellent", "0" },
{ "30-40", "Low ", "Yes", "Excellent", "1" },
{ "<30 ", "Medium", "No ", "Fair ", "0" },
{ "<30 ", "Low ", "Yes", "Fair ", "1" },
{ ">40 ", "Medium", "Yes", "Fair ", "1" },
{ "<30 ", "Medium", "Yes", "Excellent", "1" },
{ "30-40", "Medium", "No ", "Excellent", "1" },
{ "30-40", "High ", "Yes", "Fair ", "1" },
{ ">40 ", "Medium", "No ", "Excellent", "0" }
};
File file = new File("/Users/zhangzhaobo/IdeaProjects/Graduation_Design/src/data.txt"); //存放数组数据的文件
FileWriter DataToTXT = new FileWriter(file); //文件写入流
int row=0;
//将数组中的数据写入到文件中。每行各数据之间TAB间隔
for(int i=0;i<rawData.length;i++){
for(int j=0;j<rawData[0].length;j++){
DataToTXT.write(rawData[i][j]+"\t");
}
row++;
DataToTXT.write("\r\n");
}
DataToTXT.close();
BufferedReader in = new BufferedReader(new FileReader(file));
String line; //一行数据
//逐行读取,并将每个数组放入到数组中
Object[][] DataToOut = new Object[row][5];
int readrow=0;
while((line = in.readLine()) != null){
String[] temp = line.split("\t");
for(int j=0;j<temp.length;j++){
DataToOut[readrow][j] = temp[j];
}
readrow++;
}
in.close();
//显示读取出的数组
for(int i=0;i<DataToOut.length;i++){
for(int j=0;j<DataToOut[0].length;j++){
System.out.print(DataToOut[i][j]+"\t");
}
System.out.println();
}
return DataToOut;
}
}
前面的一段是将那个二维数组的数据写入到指定的文本中。而后面的则是从文本中读取出数据,并且解析为原先的二维数组。如果我们实际存在了一部分数据,那么就可以直接将修改一下数组的大小并且改一下文件地址就好了。
正文之后
最近几天忙活着筹谋一下毕业活动的策划。然后就是爆肝毕业设计了。感觉再不做完可能就要gg了,别到时候毕业失败,那什么都是空的了!我的大好前程可就不在了!
网友评论