• java开发_读写txt文件操作


    package com.mi.util;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    
    public class MyFIle {
    
        private static String path = "txt/";
        private static String filenameTemp;
    
        /**
         * 创建文件
         * 
         * @throws IOException
         */
        public static boolean createTextFile(String name) throws IOException {
            boolean flag = false;
            filenameTemp = path + name + ".txt";
            File filename = new File(filenameTemp);
            if (!filename.exists()) {
                filename.createNewFile();
                flag = true;
            }
            return flag;
        }
    
        /**
         * 写入数据
         * 
         * @param newStr
         * @return
         * @throws IOException
         */
        public static boolean writeTextFile(String newStr) throws IOException {
            // 先读取原有文件内容,然后进行写入操作
            boolean flag = false;
            String filein = newStr + "
    ";
            String temp = "";
    
            FileInputStream fis = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
    
            FileOutputStream fos = null;
            PrintWriter pw = null;
    
            try {
                File filename = new File(filenameTemp);
                fis = new FileInputStream(filename);
                isr = new InputStreamReader(fis);
                br = new BufferedReader(isr);
    
                StringBuffer sbu = new StringBuffer();
                // 保存该文件原有的内容
                for (int j = 1; (temp = br.readLine()) != null; j++) {
                    sbu = sbu.append(temp);
                    // 行与行之间的分隔符 相当于“
    ”
                    sbu = sbu.append(System.getProperty("line.speparator"));
                }
                // 将新的内容追加到读取文件中的内容字符之后
                sbu.append(filein);
    
                fos = new FileOutputStream(filename);
                pw = new PrintWriter(fos);
                pw.write(sbu.toString().toCharArray());
                pw.flush();
                flag = true;
            } catch (Exception e) {
                // TODO: handle exception
            } finally {
                if (pw != null) {
                    pw.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (br != null) {
                    br.close();
                }
                if (isr != null) {
                    isr.close();
                }
                if (fis != null) {
                    fis.close();
                }
            }
            return flag;
        }
    
        /**
         * 读取数据
         */
        public void readData1() {
            try {
                FileReader read = new FileReader(filenameTemp);
                BufferedReader br = new BufferedReader(read);
                StringBuilder sb = new StringBuilder();
                String row;
                while ((row = br.readLine()) != null) {
                    sb.append(row);
                }
                System.out.println(sb.toString());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public String readData2() {
            String returnStr = "";
            try {
                FileReader read = new FileReader(new File(filenameTemp));
                StringBuffer sb = new StringBuffer();
                char ch[] = new char[1024];
                int d = read.read(ch);
                while (d != -1) {
                    String str = new String(ch, 0, d);
                    sb.append(str);
                    d = read.read(ch);
                }
                System.out.println(sb.toString());
                String a = sb.toString().replaceAll("@@@@@", ",");
                returnStr = a.substring(0, a.length() - 1);
    
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return "";
        }
    }
  • 相关阅读:
    Matlab 图像处理中出现纯黑或纯白是怎么回事?
    网页中的公式在翻译时如何被保留下来?
    请勿 fAKe 评论
    C++ STL 使用注意事项整理
    APIO 2020 题解
    谈谈对题解本质的理解
    点双 & 边双连通图计数的细节比较
    联合省选 2021 A 卷 题解
    指数生成函数(EGF)计数小记
    UOJ-37 清华集训2014 主旋律
  • 原文地址:https://www.cnblogs.com/tingbogiu/p/5919004.html
Copyright © 2020-2023  润新知