zoukankan      html  css  js  c++  java
  • Java增、删、改、查txt文件

    1.txt

    zhang,f3d8071f8f789df766588469ecc6b1f5;

    package readtext;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;

    publicclass ReadText {


    /**
    *
    @param args
    *
    @throws IOException
    */
    publicstaticvoid main(String[] args) throws IOException {
    String FileName
    ="C:/1.txt";
    File f
    =new File(FileName);
    char act='u';
    String UserName
    ="zhang";
    String SID
    ="f3d8071f8f789df766588469ecc6b1f5";
    switch(act)
    {
    case'c':
    ReadText.CreateTxt(f);
    break;
    case'r':
    ReadText.ReadTxt(f);
    break;
    case'w':
    ReadText.WriteTxt(f, SID,UserName);
    break;
    case'd':
    ReadText.DeleteTxt(f);
    break;
    case'u':
    ReadText.UpdateTxt(f, SID);
    break;

    }
    }
    //新建文件
    publicstaticvoid CreateTxt(File f){
    if(f.exists())
    {
    System.out.println(
    "文件已存在");
    }
    else{
    try{
    // 新建text文件
    f.createNewFile();
    }
    catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    //删除文件
    publicstaticvoid DeleteTxt(File f){
    if(f.exists()){
    f.delete();
    System.out.println(
    "文件delete");
    }
    else{
    System.out.println(
    "文件不存在");
    }
    }
    //读文件
    publicstaticvoid ReadTxt(File f) throws IOException{
    String read;
    String readStr
    ="";
    if(f.exists()){
    BufferedReader br
    =new BufferedReader(new FileReader(f));
    while ((read = br.readLine()) !=null) {
    readStr
    = readStr + read;
    }
    br.close();
    System.out.println(
    "文件内容是:"+"\r\n"+ readStr);
    }
    else{
    System.out.println(
    "文件不存在!");
    }
    }
    publicstaticint CheckUser(File f,String SID) throws IOException{
    int isOk=0;
    String read;
    String readStr
    ="";
    if(!f.exists()){
    f.createNewFile();
    }
    BufferedReader br
    =new BufferedReader(new FileReader(f));
    while ((read = br.readLine()) !=null) {
    readStr
    = readStr + read;
    }
    br.close();
    if(readStr==""||readStr==null){
    isOk
    =1;
    System.out.println(
    "null or ''");
    }
    else{
    String[] str1
    =readStr.split(";");
    for(int i=0;i<str1.length;i++){
    String[] str_sid
    =str1[i].split(",");
    //System.out.println(str1[i]);
    if(str_sid[1].equals(SID)){
    System.out.println(
    "SID--------"+SID);
    System.out.println(
    "str_sid[1]------"+str_sid[1]);
    isOk
    =0;
    }
    else{
    isOk
    =1;
    }

    }
    }
    return isOk;
    }
    //写文件
    publicstaticvoid WriteTxt(File f,String SID,String UserName) throws IOException{
    String read;
    String readStr
    ="";
    if(!f.exists()){
    f.createNewFile();
    }
    BufferedReader br
    =new BufferedReader(new FileReader(f));
    while ((read = br.readLine()) !=null) {
    readStr
    = readStr + read;
    }
    br.close();
    int l=CheckUser(f, SID);
    if(l==1){
    PrintWriter bf
    =new PrintWriter(new FileWriter(f));
    String newstr
    =UserName+","+SID+";";
    String s
    =readStr+newstr;
    bf.print(s);
    bf.close();
    System.out.println(
    "输出到文件成功!"+s);
    }
    else{
    System.out.println(
    "用户已存在!"+SID);
    }
    }
    //修改文件
    publicstaticvoid UpdateTxt(File f,String SID) throws IOException{
    String read;
    String readStr
    ="";
    if(!f.exists()){
    System.out.println(
    "文件不存在!");
    }
    else{
    BufferedReader br
    =new BufferedReader(new FileReader(f));
    while ((read = br.readLine()) !=null) {
    readStr
    = readStr + read;
    }
    br.close();
    String[] str1
    =readStr.split(";");
    for(int i=0;i<str1.length;i++){
    String[] str_sid
    =str1[i].split(",");
    //System.out.println(str1[i]);
    if(str_sid[1].equals(SID)){
    PrintWriter bf
    =new PrintWriter(new FileWriter(f));
    String s
    =readStr.replace(str1[i]+";", "");
    bf.print(s);
    bf.close();
    System.out.println(
    "修改文件成功!");
    break;
    }
    }
    }
    }
    }
  • 相关阅读:
    CentOS中怎样卸载旧版本Git并安装高版本Git
    下载抖音无水印视频工具 python webdriver 2020.09.02
    axel-2.17.9 aria2c-1.35.0 wget-1.20.3 curl-7.72.0 最新CLI下载工具在win10环境测试下载速度
    wget-1.20.3 static for win32
    undefined reference to `inet_pton' under MSYS
    undefined reference to `gnutls_protocol_set_priority'
    wget2 for windows 2020.08.28
    Unicode转义(uXXXX)的编码和解码 go-nascii 类似于 native2ascii
    正则表达式零宽断言 grep sift ripgrep(rg)
    编译 jq git版本
  • 原文地址:https://www.cnblogs.com/dumanqingren/p/2025297.html
Copyright © 2011-2022 走看看