• JAVA NIO


    转载请注明出处:https://i.cnblogs.com/EditPosts.aspx?opt=1

    我们在学习的过程中可以形成自己的代码库,即将一些常用的类,函数,接口,配置文件等单独拎出来,下次使用时直接复制过来,这样就可以重复造轮子,早点下班回家。

    java NIO是一个高效的处理文件的API, 比传统文件流要高效许多。

    public class MyFileTools {
        
        public void copyFile(String inFile,String outFile,boolean tail){
            FileInputStream fin = null;
            FileOutputStream fout = null;
            
            FileChannel fcin = null;
            FileChannel fcout = null;
            
            try {
                fin = new FileInputStream(inFile);
                fout = new FileOutputStream(outFile,tail);
                
                fcin = fin.getChannel();
                fcout = fout.getChannel();
                
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                while(true){
                    buffer.clear();
                    int read = fcin.read(buffer);
                    if(read == -1){
                        break;
                    }
                    buffer.flip();
                    fcout.write(buffer);
                    
                }
                
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(fcin != null){
                    try {
                        fcin.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(fcout != null){
                    try {
                        fcout.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        public static void main(String[] args){
            MyFileTools mft = new MyFileTools();
            mft.copyFile("/sandbox-local/rding/HelloCopy.py", "/sandbox-local/rding/HelloPython2.py",true);
            
        }
    
    }
  • 相关阅读:
    kill tomcat with netstat
    windows cmd命令显示UTF8设置
    rtx没有振动功能
    手动加载rvm
    RESTful Java client with Apache HttpClient
    Set Up Git on windows also use github
    lcs.py 最长公共子串算法
    如何:对代理使用 IP 切换
    这个博客站点不错
    a case study
  • 原文地址:https://www.cnblogs.com/lfdingye/p/7550898.html
Copyright © 2020-2023  润新知