从文件中读取数据,保存到一个java一个类中,怎么实现

2025-05-01 05:38:11
推荐回答(2个)
回答1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

/**
* 读出写出
* @param oldFileName 源文件
* @param newFileName 新文件
* @throws IOException
*/
public static void testRead(String oldFileName,String newFileName) throws IOException{
FileOutputStream fos=new FileOutputStream(new File(newFileName));
RandomAccessFile raf=new RandomAccessFile(new File(oldFileName), "rw");
fos.write(raf.read(new byte[8]));
fos.flush();
fos.close();
raf.close();
}

public static void fileWrite() throws FileNotFoundException, IOException {
testRead("G:/森云/测试文件1。txt","G:/newFile.txt");
}

回答2:

  1. 文本中存的数据要有一个具体格式

  2. 把文本读取为一个字符串

  3. 采用字符串切割,获取到不同的字符串,并把字符串赋值给一个类的对象

  4. 最后形成一个类的列表

  5. 具体代码,我就随便写一个,我不知道你的文本中到底要存的是什么,也不知道你的类有哪些属性,写一个例子,体现下思路。

    效果:

  6. 文档结构图:

  7. 源代码如下:

  8. package com.song.entity;
    /**
     * 学生类
     * @author song
     *
     */
    public class Student {
    private String sno;//学号
    private String sname;//姓名
    private String sage;//年龄
    //无参构造
    public Student() {}
    //带参构造
    public Student(String sno, String sname, String sage) {
    this.sno = sno;
    this.sname = sname;
    this.sage = sage;
    }
    //getter和setter方法
    public String getSno() {
    return sno;
    }
    public void setSno(String sno) {
    this.sno = sno;
    }
    public String getSname() {
    return sname;
    }
    public void setSname(String sname) {
    this.sname = sname;
    }
    public String getSage() {
    return sage;
    }
    public void setSage(String sage) {
    this.sage = sage;
    }
    //重写toString方法
    @Override
    public String toString() {
    return "学号:"+sno+",姓名:"+sname+",年龄:"+sage;
    }

    }
  9. package com.song.service;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    /**
     * 读取文件
     */
    import com.song.entity.Student;

    public class ReadAFileToList {
    public static List readFileToList(String filePath) {
    List studentList = new ArrayList();
    String s = "";
    String result = "";
    try {
    FileReader fileReader = new FileReader(new File(filePath));
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    try {
    while ((s=bufferedReader.readLine())!=null) {
    result += s;
    }

    String[] students = result.split("[&]");
    for(int i=0;i String[] info = students[i].split("[@]");
    Student student = new Student(info[0], info[1], info[2]);
    studentList.add(student);
    }

    } catch (IOException e) {
    System.err.println("读取异常");
    e.printStackTrace();
    }
    try {
    bufferedReader.close();
    fileReader.close();
    } catch (IOException e) {
    System.err.println("关闭异常");
    e.printStackTrace();
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    return studentList;
    }
    }

package com.song.test;
/**
 * 主函数:测试读取文本效果
 */
import java.util.List;
import com.song.entity.Student;
import com.song.service.ReadAFileToList;

public class Test {

public static void main(String[] args) {
String path = "txt.txt";
List studentList = ReadAFileToList.readFileToList(path);
for(Student student:studentList) {
System.out.println("学号:"+student.getSno());
System.out.println("姓名:"+student.getSname());
System.out.println("年龄:"+student.getSage());
System.out.println("***************************");
}
}

}
txt.txt结构:
1@张三@19&2@李四@18&3@王五@20