Java实现文本文件动态存储和读写
Java实现文本文件动态存储和读写
娘炮中的女汉子 发表于11个月前
Java实现文本文件动态存储和读写
  • 发表于 11个月前
  • 阅读 18
  • 收藏 0
  • 点赞 1
  • 评论 0

【腾讯云】买域名送云解析+SSL证书+建站!>>>   

package text;

 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

 

public class text {

 

 public static void main(String[] args) {
  int n = -1;
  File file = new File("text.txt");
  byte[] a = new byte[(int) file.length()];
  int as = 0;
  int ex = 0;
  try {
   FileInputStream in1 = new FileInputStream(file);
   while ((n = in1.read(a, 0, (int) file.length())) != -1) {
    String s = new String(a, 0, n);
    for (int shu2 = 0; shu2 < s.length(); shu2++) {
     if (s.charAt(shu2) == '\n')
      as++;
    }
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  String[][] stu = new String[4][as];
  String[] con = { "姓名", "性别", "年龄", "专业" };
  Scanner read = new Scanner(System.in);
  String[] k = new String[as];
  int shu = 0, d = 0;
  int a1 = 1;
  try {
   FileInputStream in = new FileInputStream(file);
   while ((n = in.read(a, 0, (int) file.length())) != -1) {
    String s = new String(a, 0, n);
    char[] aa = s.toCharArray();
    System.out.println(aa);
    for (int j = 0; j < as; j++) {
     for (int i = 0; i < stu.length; i++) {
      for (shu = d; shu < s.length(); shu++) {
       if ((s.charAt(shu) == ' ' || s.charAt(shu) == '\n') && a1 == 0) {
        d = shu;
        a1 = 1;
        break;
       }
       if (s.charAt(shu) != ' ' && s.charAt(shu) != '\n') {
        if (stu[i][j] == null) {
         stu[i][j] = s.charAt(shu) + "";
         a1 = 0;
        } else {
         stu[i][j] = stu[i][j] + s.charAt(shu) + "";
         a1 = 0;
        }
       }
       if ((s.charAt(shu) == ' ' || s.charAt(shu) == '\n') && a1 == 0) {
        d = shu;
        break;
       }
      }
     }
    }
   }
   System.out.println("该数据是否需要更改?");
   Scanner chan = new Scanner(System.in);
   String cha = chan.next();
   if (cha.equals("是")) {
    while (ex == 0) {
     System.out.println("请更改:");
     Scanner gai = new Scanner(System.in);
     String sh = gai.next();
     for (int j = 0; j < as; j++) {
      if (sh.equals(stu[0][j])) {
       for (int ss = 0; ss < k.length; ss++) {
        if (j == ss) {
         Scanner set = new Scanner(System.in);
         for (int shu2 = 0; shu2 < con.length; shu2++)
          System.out.println("你想更改" + stu[0][j] + "的" + con[shu2] + "请输入" + (shu2 + 1));
         int and = set.nextInt();
         for (int s = 0; s < stu.length; s++) {
          if (and == s + 1) {
           System.out.println("请更改" + stu[0][j] + "的" + con[s] + ":");
           String t=stu[s][j];
           stu[s][j] = read.next();
           if(t.equals(stu[s][j]))
            System.out.println("你根本没有更改");
           else
            System.out.println("更改成功");
          }
         }
         break;
        }
       }
       break;
      }else if(j==as-1){
       System.out.println("不存在"+sh+"的信息");
       break;
      }
     }
     System.out.println("你是否想继续更改?");
     Scanner ex1 = new Scanner(System.in);
     String ga = ex1.next();
     if (ga.equals("是"))
      ex = 0;
     if (ga.equals("否"))
      ex = 1;
    }
   }
   in.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  byte[] num[] = new byte[as][];
  for (int kk = 0; kk < num.length; kk++) {
   num[kk] = (stu[0][kk] + " " + stu[1][kk] + " " + stu[2][kk] + " " + stu[3][kk] + "\n").getBytes();
   try {
    FileOutputStream out = new FileOutputStream(file);
    for (int jj = 0; jj < num.length; jj++) {
     if (num[jj] != null)
      out.write(num[jj]);
    }
    out.close();
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

 

}

我想实现动态存储事件

大雄 男 8 下町小学

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 1
博文 1
码字总数 498
×
娘炮中的女汉子
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: