• 使用字符流 创建文件 写入文件 复制文件




    import java.io.*;

        /**
     
           * 使用字符流创建文件复制文件


           * 在盘符中文件夹
           * 在文件夹中添加文件
           * 在文件中写文字
           *   复制文件

         * */



    public class Iof {
        public static void main(String[] args) {
            try {
                fun();
                    System.out.println();
                fun1();
                fun2();
            } catch (Exception e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            
            
            
        }
        
        
        
              public static void fun() throws IOException{
    //                创建IO文件夹
                    File file=new File("d:\IO");
    //                判断是否被创建 如果被创建就返回false
                    boolean mkd = file.mkdir();
                    System.out.println(mkd);
            
    //                  写入文本文件 在文本文件中写入内容
                    FileWriter filew=new FileWriter("D:\IO\IO.txt");


                    filew.write("abcdefghijk");
            
    //                关闭IO流
                  filew.close();
            
            
    //                打印O.txt文件
                  FileReader fileR = new FileReader("D:\IO\IO.txt");


                    for(;;){
                
                
    //                  打印单个字符
                          int read = fileR.read();
    //                   如果值为-1时跳出打印


                          if(read==-1)


                              break;


                        System.out.print((char)read);
                
                
            }
                      fileR.close();
            
        }
        
        
              public static void fun1() throws IOException{
    //              创建IO文件夹
            
                  File file=new File("D:\IO1");
                    file.mkdir();
            
            
            
    //              写入文本文件 在文本文件中写入内容
            
                  FileWriter fileW=new FileWriter("D:\IO1\java.txt");


                  fileW.write("abcdefghj");
            
    //                刷新IO流


                    fileW.flush();


    //                关闭IO流
            
                    fileW.close();
        
        
        
              FileReader fileR=new FileReader("D:\IO1\java.txt");
        
    //          设置一个文本的大小为1kb  这样的话文本少的话可以一次性打出来


                char [] ch=new char[1024];


    //             先让字节数等于零


                int num=0;
        
    //    打印的字节长度不能等于-1


        while ((num=fileR.read(ch))!=-1) {
            
    //        打印输出
            System.out.println((new String(ch,0,num)));
            
        }

        
            
    //        关闭IO流
            
            fileW.close();;

        }


    //    复制文件


        public static void fun2() throws Exception{


    //        创建要复制的路径


                FileWriter fileW=null;
                FileReader fileR=null;


    //            先读取文件


                fileR=new FileReader("D:\IO1\java.txt");


    //        要写的文件路经


            fileW=new FileWriter("D:\IO1\java1.txt");


    //          读取速度为1M


              char[] ch=new char[1024*10];


    //          先定义长度为0


                int len=0;


    //          如果长度扥等于-1 就跳出程序


                while((len=fileR.read(ch))!=-1){


                    fileW.write(ch,0,len);
            }    
            
    //              关闭IO流


                  fileR.close();
            
    //              刷新IO流

                  fileW.flush();
            
        }
    }

  • 相关阅读:
    Apollo简介及项目集成
    Apollo源码打包及部署
    idea中的maven模块变成灰色的可能原因
    IDEA 不能显示项目里的文件结构
    Idea不能新建package的解决
    sourceTree 基础使用
    服务器负载均衡是什么意思?
    Spring中的代理模式
    ZooKeeper启动报错 JAVA_HOME is incorrectly set
    @Controller和@RestController的区别?
  • 原文地址:https://www.cnblogs.com/hph1728390/p/10595600.html
Copyright © 2020-2023  润新知