zoukankan      html  css  js  c++  java
  • Java写文件

    import java.io.File;   
    
    import java.io.FileOutputStream;   
    
    import java.io.*;   
    
    public class FileTest {   
    
        public FileTest() {   
    
        }   
    
        public static void main(String[] args) {   
    
            FileOutputStream out = null;   
    
            FileOutputStream outSTr = null;   
    
            BufferedOutputStream Buff=null;   
    
    
    
            FileWriter fw = null;  
        
    
    
            int count=1000;//写文件行数   
    
    
            try {   
    
        //方式一
                out = new FileOutputStream(new File(“C:/add.txt”));   
    
                long begin = System.currentTimeMillis();   
    
                for (int i = 0; i < count; i++) {   
    
                    out.write(“测试java 文件操作
    ”.getBytes());   
    
                }   
    
                out.close();   
    
                long end = System.currentTimeMillis();   
    
                System.out.println(“FileOutputStream执行耗时:” + (end - begin) + ” 豪秒”);  
    
            
        //方式二
    
                outSTr = new FileOutputStream(new File(“C:/add0.txt”));   
    
                 Buff=new BufferedOutputStream(outSTr);   
    
                long begin0 = System.currentTimeMillis();   
    
                for (int i = 0; i < count; i++) {   
    
                    Buff.write(“测试java 文件操作
    ”.getBytes());   
    
                }   
    
                Buff.flush();   
    
                Buff.close();   
    
                long end0 = System.currentTimeMillis();   
    
                System.out.println(“BufferedOutputStream执行耗时:” + (end0 - begin0) + ” 豪秒”);   
    
            //方式三(推荐)
    
                fw = new FileWriter(“C:/add2.txt”);   
    
                long begin3 = System.currentTimeMillis();   
    
                for (int i = 0; i < count; i++) {   
    
                    fw.write(“测试java 文件操作
    ”);   
    
                }   
    
                            fw.close();   
    
                long end3 = System.currentTimeMillis();   
    
                System.out.println(“FileWriter执行耗时:” + (end3 - begin3) + ” 豪秒”);   
    
            } catch (Exception e) {   
    
                e.printStackTrace();   
    
            }   
    
            finally {   
    
                try {   
    
                    fw.close();   
    
                    Buff.close();   
    
                    outSTr.close();   
    
                    out.close();   
    
                } catch (Exception e) {   
    
                    e.printStackTrace();   
    
                }   
    
            }   
    
        }   
    
    }
  • 相关阅读:
    并发编程3
    并发编程2
    4/23
    4/22
    并发编程1
    粘包问题
    Navicat12激活
    IDEA创建maven项目报错解决:Failed to create a Maven project: 'C:/Users/../IdeaProjects/../pom.xml' already e
    IDEA
    windows下查看端口运行情况--解决端口冲突问题
  • 原文地址:https://www.cnblogs.com/suhfj-825/p/9044760.html
Copyright © 2011-2022 走看看