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;
    }
    }
    }
    }
    }
  • 相关阅读:
    Linux基础优化(二)
    权限
    分页
    序列化
    forms
    redis
    Django缓存机制
    跨域问题
    Django的ORM
    模板层
  • 原文地址:https://www.cnblogs.com/dumanqingren/p/2025297.html
Copyright © 2011-2022 走看看