• 读取文件内容


    练习1:

    将源文件内容拷贝到目标文件< ./src/txt/1.txt--->./src/txt/2.txt >

    public class UseBuffer {
        public static  void main(String[] args) throws IOException{
                              final int  SIZE = 1024;
                              File fileIn = new File ("./src/txt/","1.txt");
                              FileInputStream in = 
                                            new FileInputStream(fileIn);
                              File fileOut = new File("./src/txt/","2.txt");
                              FileOutputStream out = 
                                             new FileOutputStream(fileOut);
                            //创建byte数组
                              byte[] buff = new byte[SIZE];
                            
                              int num = in.read(buff);
                              while(num!=-1){
                                     out.write(buff,0,num); 
                                     num = in.read();
                              }
                              in.close();
                              out.close();
        }
    }    

    练习2: 

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class FileTest2 {
        public static void main(String[] args) throws IOException {
            //将变量存入 System
            System.setProperty("use.dir", "./src/picture");
            //得到Properties
            String usedir = System.getProperty("use.dir");
            System.out.println("用户路径为  :"+usedir);
            
            File file = new File(usedir,"IMAG0056.jpg");
            System.out.println("check whether the file is exsist : "+file.isFile());
            System.out.println("check the length of the file :"+file.length());
            
            File fileOut = new File(usedir,"IMAG0057.jpg");
            //输入流
            FileInputStream in = new FileInputStream(file);
            //输出流 
            FileOutputStream out =  new FileOutputStream(fileOut);
            
            //建立缓冲区
            int SIZE  = 1024;
            byte[] buff = new byte[SIZE];
            
            //从输入流中读取内容
            int num = in.read(buff);
            //写入到输出流之中
            while(num!=-1){
                out.write(buff);
                num = in.read(buff);
            }
            in.close();
            out.close();
        }
    }

     练习4:递归法测试将源文件内容读取到目的文件内

    public class TestWrite {
           //源文件
        private  File inFile = new File("./src/com/cici/test/1.txt");
          //目的文件
        private  File outFile = new File("./src/com/cici/test/2.txt");
          // 输入流
        private FileInputStream in ;
         // 输出流
        private  FileOutputStream out ;
         //buff字节字符串
        byte []  buff = new byte[190];
        
        public TestWrite() throws FileNotFoundException {
             in = new FileInputStream(inFile);
             out = new FileOutputStream(outFile);
        }
        
        public static void main(String[] args) throws Exception {
            TestWrite tw = new TestWrite();
            tw.methodReadWrite(0,0);
        }
    
        public void methodReadWrite(int begin,int length) throws Exception{
                   
            int num = in.read(buff);
            System.out.println("num ---- "+num);
            if(begin==0)
                out.write(buff, 0, num);
            if(num!=-1){
                begin = num;
                length = 2*num;
                methodReadWrite(num, num+num);
            }else{
                return;
            }
        }
    }
     

  • 相关阅读:
    每日日报110
    每日日报109
    每日日报108
    每日日报107
    每日日报106
    每日日报105
    每日日报104
    eclipse新建Maven Project项目后出现The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path问题的解决办法
    0proxy/reflect
    toRefs/toRef/unref/customRef
  • 原文地址:https://www.cnblogs.com/cici-new/p/3589847.html
Copyright © 2020-2023  润新知