java往文本文件中写入信息并修改

时间:2019-04-03
本文章向大家介绍java往文本文件中写入信息并修改,主要包括java往文本文件中写入信息并修改使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目要求:

1.可以往一个文本文档中写入员工信息:name,id和详情

2.可以更改name

package FanCQ.Xue.practice;

import java.io.*;
import java.util.Scanner;

/*
* @author XueWeiWei
* @date 2019/3/31 20:13
*/
public class Xue_RW {
public static void main(String[] args) throws IOException {
//创建一个文本文件,如果已经存在,则告知用户:The file was created
File file = new File("./demo.txt");
if (file.exists()){
System.out.println("The file was created.");
}else {
file.createNewFile();
}
RandomAccessFile randomAccessFile = new RandomAccessFile("./demo.txt","rw"); //使用RandomAccessFile类对文件进行随意修改
//让用户进行操作选择(1.往文本文件中插入数据;2.修改文本文件的数据;0.退出该程序)
System.out.println("插入,输入1");
System.out.println("修改,输入2");
System.out.println("退出,输入0");
System.out.println("输入你的选择:");
Scanner scanner = new Scanner(System.in);
int choose = Integer.parseInt(scanner.next());
XWW xww = new XWW(); //初始化要写入文件的类
int i=0; //使用变量i来记录文本文件的行数
//使用BufferedReader来进行文件的按行读取
FileReader fileReader = new FileReader("./demo.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder sh = new StringBuilder(); //使用StringBuilder来将读取的数据填充到一个字符串内,容易进行查找与修改
String[] str = new String[20]; //使用数组str[]将文本文件的每一行数据进行存储,放到StringBuilder中
while ( ( str[i] = bufferedReader.readLine() ) != null){
sh.append(str[i]);
i++;
}
// System.out.println("i的值:" + i);
do {
if (choose == 1){
//如果是初次写入数据,将指标放到文件开头,放到文件末尾
if (i>0){
randomAccessFile.seek(randomAccessFile.length());
}else {
randomAccessFile.seek(0);
}
//输入类的三个信息,并进行写入
System.out.println("name:");
String name = scanner.next();
System.out.println("id:");
String id = String.valueOf(scanner.next());
System.out.println("text:");
String text = scanner.next();
xww.setName(name);
xww.setId(id);
xww.setText(text);
xww.write(randomAccessFile);
//写入一行,增加一行数据
i++;
//同时stringBuilder中也要增加数据
sh.append(name).append(id).append(text);
}
if (choose == 2){
System.out.println("当前一共" +(i) + "行数据");
System.out.println("输入你想修改的数据name:");
String s = scanner.next();
System.out.println("输入你想修改后的数据name(请输入和想修改name的信息等长!!!!):");
String s1 = scanner.next();
System.out.println("字符串的位置" + sh.indexOf(s));
System.out.println(sh);
//使用start来查找到想要修改数据的位置
int start = sh.indexOf(s);
if (start == -1){
System.out.println("您所想要修改的数据不存在");
}else {
// if (s.length()<=s1.length()){
randomAccessFile.seek(start);
xww = new XWW(s1,"","");
xww.write2(randomAccessFile);
// }else {}
}
}
System.out.println("输入你的选择:");
choose = Integer.parseInt(scanner.next());
}while (choose != 0);

bufferedReader.close();
fileReader.close();
// XWW xww0 = new XWW("薛卫卫","110","我是16智71班薛卫卫");
// randomAccessFile.seek(0);
// xww0.write(randomAccessFile);
//
// String name = scanner.next();
// xww0.setName(name);
// xww0.write(randomAccessFile);
// randomAccessFile.seek(0);
//
// XWW xww1 = new XWW();
// xww1.read(randomAccessFile);
// System.out.println(xww1.name + " " + xww1.id + " " + xww1.text);
}
}
class XWW{
public String name;
public String id;
public String text;

public XWW() {
}

public XWW(String name, String id, String text) {
this.name = name;
this.id = id;
this.text = text;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

//将三个数据进行写入,同时换行
public void write(RandomAccessFile randomAccessFile) throws IOException {
randomAccessFile.writeUTF(name);
randomAccessFile.writeUTF(id);
randomAccessFile.writeUTF(text);
randomAccessFile.writeUTF("\n");
}

//将三个数据读取文件
public void read(RandomAccessFile randomAccessFile) throws IOException {
this.name=randomAccessFile.readUTF();
this.id=randomAccessFile.readUTF();
this.text=randomAccessFile.readUTF();
}
//修改名字
public void write2(RandomAccessFile randomAccessFile) throws IOException {
randomAccessFile.writeUTF(name);
}
}