java 文件读取

来源:互联网 发布:2016网络神曲排行榜 编辑:程序博客网 时间:2024/04/25 14:28

File copy

public void fileCopy(String file,String target){       try(InputStream inputStream = new FileInputStream(file)){           try(OutputStream outputStream = new FileOutputStream (target)){                     byte [] buffer = new byte[4096];               int bytesToRead;               while((bytesToRead=inputStream.read(buffer))!=-1){                   outputStream.write(buffer,0,bytesToRead);               }           }       } catch (IOException e) {        e.printStackTrace();    }    }    public void copyNio(String file,String target){        try(FileInputStream inputStream = new FileInputStream(file)){            try(FileOutputStream outputStream = new FileOutputStream(target)){                FileChannel inChannel = inputStream.getChannel();                FileChannel oUTChannel = outputStream.getChannel();                java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate(4096);                while(inChannel.read(buffer)!=-1){                    buffer.flip();                    oUTChannel.write(buffer);                    buffer.clear();                }            }        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }
0 0
原创粉丝点击