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 "";
    }
}
原文地址:https://www.cnblogs.com/tingbogiu/p/5919004.html