• java 读写文件


    //字节流
    import java.io.*;
    public class testchaer {
        public static void main(String  args[]) {
        FileReader fr=null;
        FileWriter fw=null;
        try {
            fr=new FileReader("C:/Users/Administrator/Desktop/xuexi/jiankongjiao.py");
            fw=new FileWriter("C:/Users/Administrator/Desktop/xuexi/xuexi11.txt");
            char [] buffer=new char[100];
            while (true) {
                int tem=fr.read(buffer, 0, buffer.length);
                if (tem==-1){
                    break;
                }
                fw.write(buffer,0,tem);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        finally {
            try {
                fr.close();
                fw.close();
            } catch (Exception e2) {
                // TODO: handle exception
            }
        }
    }
    }
    //字符流
    import java.io.*;
    public class inpur {
       public static void main(String  args[]) {
        FileInputStream fis=null;
        FileOutputStream fos=null;
           try{
               fis=new FileInputStream("C:/Users/Administrator/Desktop/xuexi/jiankongjiao.py");
               fos= new FileOutputStream("C:/Users/Administrator/Desktop/xuexi/xuexi2.txt");
               byte [] buffer=new byte[100];
               while (true){
                   int temp=fis.read(buffer,0,buffer.length);
                   if(temp==-1){
                       break;
                   }
                   fos.write(buffer,0,temp);
                   String stri=new String(buffer);
                   //调用string对象的tim方法,将会将string的收尾的空格和空字符串
                   stri=stri.trim();
                   System.out.println(stri);
               }  
           }
           catch (Exception e) {
            // TODO: handle exception
               System.out.println(e);
           }
           finally{
                try {
                    fis.close();
                    fos.close();
                } catch (Exception e) {
                    System.out.println(e);
                }
                }
    }
    }

    一行一行的读了,

    package ifwh;
    import java.io.*;
    public class teatsjaa {
     public static void main(String agrs[]) {
        FileReader fileReader=null;
        BufferedReader  bufferedWriter=null;
        try {
            fileReader=new FileReader("C:/Users/Administrator/Desktop/xuexi/jiankongjiao.py");
            bufferedWriter =new BufferedReader(fileReader);
            String line=null;
            while (true) {
                line=bufferedWriter.readLine();
                if (line==null){
                    break;
                }
                System.out.println(line);
            }
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e);
        }finally {
            try {
                fileReader.close();
                bufferedWriter.close();
            } catch (Exception e2) {
                // TODO: handle exception
                System.out.println(e2);
            }
        }
    }
    }
  • 相关阅读:
    简单的Makefile
    共享库
    链接静态库
    /proc/uptime参数的意义
    磁盘挂载失败
    linux环境变量设置
    使用systemd-analyze 工具来分析各个服务进程的启动性能
    面试-2020C/C++后台开发深信服科技股份有限公司一面凉经(一问三不知,我是真的菜。。。)
    笔记-C/C++工程师面试笔记收集整理
    SpringCloud-分布式与集群的使用四(断路器Hystrix)
  • 原文地址:https://www.cnblogs.com/leiziv5/p/7898527.html
Copyright © 2020-2023  润新知