JAVA的文件读取写入常用工具类


public class FileUtil {
    /**
     * 写文件
     *
     * @param file
     * @param data
     */
    public static void rewrite(File file, String data) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(file));
            bw.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 读文件
     *
     * @param file
     * @return
     */
    public static List readList(File file) {
        BufferedReader br = null;
        List data = new ArrayList();
        try {
            br = new BufferedReader(new FileReader(file));
            for (String str = null; (str = br.readLine()) != null; ) {
                data.add(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return data;
    }
}

你可能感兴趣的:(学习资料,日常问题)