Java中通过txt文件存储和取出数据
正在编写一个学生信息系统,需要在txt中存储并且在需要的时候读取信息。只要一个类就可以了,请大家帮帮我,真心地感谢你。...
正在编写一个学生信息系统,需要在txt中存储并且在需要的时候读取信息。
只要一个类就可以了,请大家帮帮我,真心地感谢你。 展开
只要一个类就可以了,请大家帮帮我,真心地感谢你。 展开
4个回答
展开全部
Java中读取txt文件可以使用file类先创建一个对象,然后使用I/O操作,进行读取或者写入操作,示例如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class demo2 {
private static String path = "f:/demo1.txt";
private static File file;
static{
file = new File(path);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
Student stu = new Student(1,"张三",90);
writeDataToFile(file,stu);
readDataFromFile(file);
}
private static void readDataFromFile(File file) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String str = "";
while((str = reader.readLine())!=null){
String[] stuInfo = str.split(",");
System.out.println("学号:"+stuInfo[0]+" 姓名:"+stuInfo[1]+" score:"+stuInfo[2]);
}
}
private static void writeDataToFile(File file,Student stu) throws FileNotFoundException {
PrintWriter out = new PrintWriter(new FileOutputStream(file, true));
out.println(stu.toString());
out.close();
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class demo2 {
private static String path = "f:/demo1.txt";
private static File file;
static{
file = new File(path);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
Student stu = new Student(1,"张三",90);
writeDataToFile(file,stu);
readDataFromFile(file);
}
private static void readDataFromFile(File file) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String str = "";
while((str = reader.readLine())!=null){
String[] stuInfo = str.split(",");
System.out.println("学号:"+stuInfo[0]+" 姓名:"+stuInfo[1]+" score:"+stuInfo[2]);
}
}
private static void writeDataToFile(File file,Student stu) throws FileNotFoundException {
PrintWriter out = new PrintWriter(new FileOutputStream(file, true));
out.println(stu.toString());
out.close();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class demo2 {
private static String PATH = "f:/demo1.txt";
private static File file;
static{
file = new File(PATH);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
Student stu = new Student(1,"张三",90);
writeDataToFile(file,stu);
readDataFromFile(file);
}
private static void readDataFromFile(File file) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String str = "";
while((str = reader.readLine())!=null){
String[] stuInfo = str.split(",");
System.out.println("学号:"+stuInfo[0]+" 姓名:"+stuInfo[1]+" score:"+stuInfo[2]);
}
}
private static void writeDataToFile(File file,Student stu) throws FileNotFoundException {
PrintWriter out = new PrintWriter(new FileOutputStream(file, true));
out.println(stu.toString());
out.close();
}
}
class Student{
int stuNo;
String name;
int score;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(int stuNo, String name, int score) {
super();
this.stuNo = stuNo;
this.name = name;
this.score = score;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return this.stuNo+","+this.name+","+this.score;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class demo2 {
private static String PATH = "f:/demo1.txt";
private static File file;
static{
file = new File(PATH);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
Student stu = new Student(1,"张三",90);
writeDataToFile(file,stu);
readDataFromFile(file);
}
private static void readDataFromFile(File file) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String str = "";
while((str = reader.readLine())!=null){
String[] stuInfo = str.split(",");
System.out.println("学号:"+stuInfo[0]+" 姓名:"+stuInfo[1]+" score:"+stuInfo[2]);
}
}
private static void writeDataToFile(File file,Student stu) throws FileNotFoundException {
PrintWriter out = new PrintWriter(new FileOutputStream(file, true));
out.println(stu.toString());
out.close();
}
}
class Student{
int stuNo;
String name;
int score;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(int stuNo, String name, int score) {
super();
this.stuNo = stuNo;
this.name = name;
this.score = score;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return this.stuNo+","+this.name+","+this.score;
}
}
追问
我马上试试,如果好用,马上给分!
可以大概说一下思路和过程吗,麻烦您了
追答
就是定一个规则啊,比如我定的规则是 学号,姓名,成绩 以行字符串的形式写入文件
读的话就按行读,按规则去解析字符串.
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
数据库中你给的type是text吧?
在持久层你就用String装它不就得了。
在持久层你就用String装它不就得了。
追问
你好,谢谢你的回答。
是这样的,我没有用到数据库,现在只是在以前命令行的基础之上,要加上利用文件存储和取出数据的能力。要保存的是一些信息条目,格式和类型都一样。
存储的时候,要把信息条目放入txt文件。
当读取的时候,我要把每一个条目的每一项信息都拿出来,进行比对。
请说得具体一些,最好帮我写一个小例子,分数可以追加。
追答
配置文件啊,你用xml不行啊,读txt很让人纠结。
读XML可能还能帮你写出来,但是读txt你自己百度怎么读就好了。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询