zoukankan      html  css  js  c++  java
  • java向文件写数据的3种方式

    下边列举出了三种向文件中写入数据的方式,当然还有其他方式,帮助自己理解文件写入类的继承关系。类的关系:

    file->fileoutputstream->outputstreamWriter(FileWriter继承outputstreamWriter对象)

    测试代码:

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    
    /**
     * 测试向文件中写文件
     * 
     * @author lenovo
     * 
     */
    public class TestWirteFile {
    
        /**
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
            String sContent = "2015年的双十一真的是非常火爆!";
            String sDestFile = "F:/myWrite.txt";
            File destFile = new File(sDestFile);
            if (!destFile.exists()) {
                destFile.createNewFile();
            }
    
            // 1.向文件写入内容
            // writeByFileWrite(sDestFile, sContent);
    
            // 2.FileOutputStream向文件写入内容
            // writeByFileWrite(sDestFile, sContent);
    
            // 2.OutputStreamWriter向文件写入内容
            writeByOutputStreamWrite(sDestFile, sContent);
        }
    
        /**
         * 用FileWrite向文件写入内容
         * 
         * @param _destFile
         * @throws IOException
         */
        public static void writeByFileWrite(String _sDestFile, String _sContent)
                throws IOException {
            FileWriter fw = null;
            try {
                fw = new FileWriter(_sDestFile);
                fw.write(_sContent);
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (fw != null) {
                    fw.close();
                    fw = null;
                }
            }
        }
    
        /**
         * 用FileOutputStream向文件写入内容
         * 
         * @param _destFile
         * @throws IOException
         */
        public static void writeByFileOutputStream(String _sDestFile,
                String _sContent) throws IOException {
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(_sDestFile);
                fos.write(_sContent.getBytes());
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (fos != null) {
                    fos.close();
                    fos = null;
                }
            }
        }
    
        /**
         * 用OutputStreamWrite向文件写入内容
         * 
         * @param _destFile
         * @throws IOException
         */
        public static void writeByOutputStreamWrite(String _sDestFile,
                String _sContent) throws IOException {
            OutputStreamWriter os = null;
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(_sDestFile);
                os = new OutputStreamWriter(fos, "UTF-8");
                os.write(_sContent);
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (os != null) {
                    os.close();
                    os = null;
                }
                if (fos != null) {
                    fos.close();
                    fos = null;
                }
    
            }
        }
    
    }
  • 相关阅读:
    SpringBoot中并发定时任务的实现、动态定时任务的实现(看这一篇就够了)
    压力测试
    AlertManger的详细配置
    http://www.linuxe.cn/post-518.html
    https://helpcdn.aliyun.com/knowledge_detail/194196.html
    AIOPS智能监控团队
    普罗新修斯监控mysql数据库1
    干货 | Elasticsearch集群黄色原因的终极探秘
    elasticsearch集群在生产上面必看的优化文章
    干货丨DolphinDB与Elasticserach在金融数据集上的性能对比测试
  • 原文地址:https://www.cnblogs.com/longshiyVip/p/4959534.html
Copyright © 2011-2022 走看看