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);
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM