• FileReader和FileWriter的使用实例


    package io;
    
    import org.junit.Test;
    
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Map;
    import java.util.Properties;
    
    public class IoDemoTest {
        private FileReader fr = null;
        private FileWriter fw = null;
        private File file = null;
    
        @Test
        public void fun() {
            try {
                FileReader fr = new FileReader(new File(IoDemoTest.class.getResource("/a.txt").getPath()));
                int i = 0;
                /*System.out.println(1 / 0);*/
                while ((i = fr.read()) != -1) {
                    System.out.print((char) i);
                }
    
                fr.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println();
                System.out.println("无论什么情况都会被执行");
            }
        }
    
        @Test
        public void write() {
            /*Map<String, String> getenv = System.getenv();
            for(Map.Entry<String,String> en :getenv.entrySet()){
                System.out.println("key值是" + en.getKey());
                System.out.println("value值是" + en.getValue());
            }*/
    
            /*String pwd = System.getenv("PWD");
            System.out.println(pwd);*/
    
            String getdir = System.getProperty("user.dir");
            /*System.out.println(getdir);*/
    
            File file = new File(getdir + "/src/main/resources/b.txt");
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    FileWriter fw = new FileWriter(file);
                    fw.write("io写数据");
                    fw.flush();
                    /*fw.close();*/
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        @Test
        public void copy0() throws Exception {
            fr = new FileReader("C:\Users\15082157\Desktop\自我介绍.txt");
            /*int i = 0;
            while ((i = fr.read()) != -1) {
                System.out.print((char) i);
            }*/
    
            String getdir = System.getProperty("user.dir");
            file = new File(getdir + "/src/main/resources/copy0.txt");
            if (!file.exists()) {
                file.createNewFile();
                /*FileWriter fw = new FileWriter(file);
                while ((i = fr.read()) != -1) {
                    fw.write((char) i);
                }
                fw.flush();
                fr.close();
                fw.close();*/
                characterCopy();
                System.out.println("新创建文件,再copy字符");
            } else {
                /*FileWriter fw = new FileWriter(file);
                while ((i = fr.read()) != -1) {
                    fw.write((char) i);
                }
                fw.flush();
                fr.close();
                fw.close();*/
                characterCopy();
                System.out.println("已有文件,直接copy字符");
            }
    
        }
    
        public void characterCopy() throws Exception {
            int i = 0;
            fw = new FileWriter(file);
            long beginTime = getCurrentTime1();
            while ((i = fr.read()) != -1) {
                fw.write((char) i);
            }
            fw.flush();
            long endTime = getCurrentTime1();
            float time = (endTime - beginTime) / 1000F;
            System.out.println("copy时间为" + time + "s");
            fr.close();
            fw.close();
    
        }
    
        @Test
        public void getCurrentTime(){
            SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ssSSS");
            /*System.out.println(df);*/
            System.out.println(df.format(new Date()));
        }
    
    
        public long getCurrentTime1(){
            long currentTime= System.currentTimeMillis();
            System.out.println(currentTime);
            return currentTime;
        }
    }

    windows获取工程当前路径使用这个方法  System.getProperty("user.dir");

  • 相关阅读:
    js_阶乘
    python 最新方案-解决编码错误问题:UnicodeEncodeError: 'ascii' codec can't encode characters in position
    RabbitMQ Python端发送消息给Java端 接受消息全数字问题
    【Hadoop】第六天 Hive
    【Hadoop 】第五天 Hadoop-HA 搭建
    Redis面试常问点
    python md5 salt 摘要算法
    【Hadoop】第四天 Shuffle MapReduce组件全貌
    【Hadoop】第三天 mapreduce的原理和编程
    【Hadoop 】第二天 hdfs的原理和使用操作、编程
  • 原文地址:https://www.cnblogs.com/KevinFeng/p/12955210.html
Copyright © 2020-2023  润新知