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;
    }
    }
    }
    }
    }
  • 相关阅读:
    图像裁剪功能,鼠标抬起移除事件,不只是想去掉鼠标抬起时的裁剪事件,重要的是jquery绑定的都是dom2级事件
    在 JavaScript 里 + 会把一些字符转化成数字
    ++[[]][+[]]+[+[]] == 10 //true
    node 接口
    windows下,node.js默认执行的根目录
    鸟速度不匀速的方法Math.sqrt(this.i++); 开根号
    rotate 里面的是弧度不是度,如果需要度则要转成度 Math.PI/180
    定义一个数,它可能为正 也可能为负 var num = Math.pow(-1,parseInt(Math.random() * 2) + 1);
    如何单独使用modelsim进行仿真
    Xilinx开发板信息
  • 原文地址:https://www.cnblogs.com/dumanqingren/p/2025297.html
Copyright © 2011-2022 走看看