整理2:文件读写的几种方式--记录于2017.7

1.先读取所有再添加

//先判断要添加的内容在文件中是否存在,不存在在向其添加
    public static void main(String[] args)throws IOException {
        try {
           //1、创建流对象
            Reader reader=new FileReader("e:/bbb.txt");
           //构建高效流对象
            BufferedReader buffReader=new BufferedReader(reader);
           //2、读取一行字符串
            String line=buffReader.readLine();
            StringBuffer buffer=new StringBuffer();
            while(line!=null){
                buffer.append(line+"\r\n");
                line=buffReader.readLine();

            }
            String a = buffer.toString();
            if(a.contains("80")){
                System.out.println("重复");
            }else{
                System.out.println("buchongfu");
                Writer w=new FileWriter("e:/bbb.txt");
                BufferedWriter buffWriter=new BufferedWriter(w);
                buffWriter.write(buffer.toString()+"\r\n");
                buffWriter.close();
                w.close();
                System.out.println("写入成功!");
            }
           //3、关闭流
            buffReader.close();
            reader.close();
        } catch (FileNotFoundException e) {
            System.out.println("要读取的文件不存在:"+e.getMessage());
        } catch (IOException e) {
            System.out.println("文件读取错误:"+e.getMessage());
        }
    }

.=================================================================

2.文件末尾追加信息 

       /**
         * @param args
         * 文件末尾追加信息
         */
        try {
        //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
            FileWriter writer = new FileWriter("E:\\bbb.txt", true);
            writer.write("\r\n:"+"哈哈哈哈1234567");
            System.out.println("--------追加成功-------------");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

======================================================================

3.任意位置删除写入文件 

try {
            File srcFile = new File("E:/upteam.conf");//首先存在文件,文件内容:1 2 3
            if (srcFile.exists()) {
                File temp = new File("E:\\2.txt");
                RandomAccessFile read = new RandomAccessFile(srcFile, "rw");
                RandomAccessFile insert = new RandomAccessFile(temp, "rw");
                String str = "";
                int ind = 0;
                while (null != (str = read.readLine())) {
                    if(str.contains("Port")){//去除空格后,以关键字开头并包含关键字
                        insert.write(("Port 67\n").getBytes());
                    }else{
                        insert.write((str+"\n").getBytes());
                    }
                    ind++;
                }
                read.close();
                insert.close();
                srcFile.delete();//删除源文件
                temp.renameTo(srcFile);//临时文件更名为源文件
                System.out.println("--------------End 写入成功----------");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

======================================================================

4. 文件中总共有多少行 

try {
            File srcFile = new File("E:/kfw.conf");//首先存在文件,文件内容:1 2 3
            if (srcFile.exists()) {
                RandomAccessFile read = new RandomAccessFile(srcFile, "rw");
                String str = "";
                int ind = 0;
                while (null != (str = read.readLine())) {
                    if(str.contains("name") || str.contains("/")){
                        System.out.println("str内容:"+str);
                        ind++;
                    }
                }
                System.out.println("总共多少行===:"+ind);
                read.close();
                System.out.println("--------------End 查找成功----------");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

发布了19 篇原创文章 · 获赞 11 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/sweet__queen/article/details/105605321