读取TXT文件中文本并存入数组中
当我们看到这样的txt文本,必然好奇如何读取文本的数据。如何读取数据很简单,只要设计一个有关函数,分行读取即可!那么如何将数据存入数组里呢?只要将获取的数据存入StringBuilder里,将带有数据的StringBuilder变量变成String类型数组,再用String类型数组读取即可。
读取txt文件
在txtToString(File file)函数中,我们建立一个StringBuilder类型的result变量,这个result变量是需要存储所需的txt数据集,不能没有。然后构造一个BufferedReader类来读取文件,使用使用readLine方法,一次读一行。注意关闭IO流!
public static StringBuilder txtToString(File file){
StringBuilder result = new StringBuilder();
try{
BufferedReader br = new BufferedReader(new FileReader(file));/
String s = null;
while((s = br.readLine())!=null){
result.append(System.lineSeparator()+s);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
return result;
}
数据存入数据集
按行读取数据会出现的问题
正常将StringBuilder可以用toString()变成String类型,再用toCharArray()的话,输出的字符都是单个字符。所以建议用下列代码
String[] str = txtlist.toString().split("\n");
代码实现
public static void main(String[] args){
File file = new File("能不能给点赞/给点打赏.txt");
StringBuilder txtlist=TxtReader.txt2String(file);
String[] str = txtlist.toString().split("\n");
// System.out.println("人数"+str[1]);
// System.out.println("姓名"+str[2].substring(0,7));
String arrayname1=str[2].substring(0,7);
String[] array1=new String[4];
int j=3;
for (int i = 0; i <array1.length ; i++) {
while (j<7){
array1[i]=str[j++];
// System.out.println(array1[i++]);
}
}
}
优化思路
创建一个学生类,再用ArrayList存储
学生类
import java.util.ArrayList;
public class Student {
private static int id;
private int selfId;
public String name;
public ArrayList<String> subjects;
public Student(String name) {
this.name = name;
this.selfId = id;
subjects = new ArrayList<String>();
id++;
}
public int getId() {
return selfId;
}
@Override
public String toString() {
return "Student{" +
"selfId=" + selfId +
", name='" + name + '\'' +
", subjects=" + subjects +
'}';
}
}
读取
private static ArrayList<Student> students;
public void InputReaderTest(String fileName) throws IOException {
students = new ArrayList<Student>();
File inputFile = new File(fileName);
FileReader inputReader = new FileReader(inputFile);
BufferedReader bufferReader = new BufferedReader(inputReader);
String line = bufferReader.readLine();
int lineCount = 0;
while (line != null) {
line = line.trim();
if (line.length() < 1) {
System.out.println("Skipping empty line at line no. " + lineCount);
lineCount++;
line = bufferReader.readLine();
continue;
}
if (lineCount == 0) {
line = bufferReader.readLine();
lineCount++;
continue;
}
if (lineCount == 0) {
line = bufferReader.readLine();
lineCount++;
continue;
}
String[] stInfo = line.split(",");
if (stInfo.length < 2) {
throw new IOException("Error reading student info at line " + Integer.toString(lineCount));
}
Student st = new Student(stInfo[0]);
int v = Integer.parseInt(stInfo[1].trim());
for (int i = 0; i < v; i++) {
line = bufferReader.readLine();
lineCount++;
st.subjects.add(line);
}
students.add(st);
lineCount++;
line = bufferReader.readLine();
}
}
测试
public static void main(String[] args) throws IOException {
new InputReaderTest().InputReaderTest("输入你文件相对地址");
for ( Student stu : students) {
System.out.println(stu.toString());
}
}