Java文件操作:拷贝、读取文件等

来源:互联网 发布:ug8.5编程实例教程图文 编辑:程序博客网 时间:2024/06/08 02:18

Java文件拷贝,可以使用通道进行,即NIO方式,能提高文件拷贝速度

private static void nioTransferCopy(String sourcePath, String targetPath) throws IOException{File source = new File(sourcePath);File target = new File(targetPath);if(source == null || target == null)return;if( !source.exists())return ;FileChannel in = null;FileChannel out = null;FileInputStream inStream = null;FileOutputStream outStream = null;try {inStream = new FileInputStream(source);outStream = new FileOutputStream(target);in = inStream.getChannel();out = outStream.getChannel();in.transferTo(0, in.size(), out);out.transferFrom(in, 0, in.size());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(inStream != null)inStream.close();if(outStream != null)outStream.close();if(in != null)in.close();if(out != null)out.close();}}

为能得到文件拷贝的实时进度,可以采用ByteBuffer来拷贝:

private static void nioBufferCopy(File source, File target) throws IOException{FileChannel in = null;FileChannel out = null;FileInputStream inStream = null;FileOutputStream outStream = null;try{inStream = new FileInputStream(source);outStream = new FileOutputStream(target);in = inStream.getChannel();out = outStream.getChannel();ByteBuffer buffer = ByteBuffer.allocate(4096);while(in.read(buffer) != -1){buffer.flip();out.write(buffer);buffer.clear();}}catch(Exception e){e.printStackTrace();}finally{if(inStream != null)inStream.close();if(outStream != null)outStream.close();if(in != null)in.close();if(out != null)out.close();}}

文件的拷贝还可以利用系统命令来执行,即使用Runtime

/** *  * @param sourcePath  this is include the file name * @param targetPath  just the target folder on windows */private static void copyFileBySystem(String sourcePath, String targetPath){String strFileSper = File.separator;File sourceFile = new File(sourcePath);File targetFile = new File(targetPath);if( !sourceFile.exists() ){System.out.println(sourcePath + " is not exists");return;}if( !targetFile.exists() )targetFile.mkdirs();try{if(strFileSper.equals("/")){Process process = Runtime.getRuntime().exec("cp -f "+sourcePath+" "+targetPath);//process.destroy();}else if(strFileSper.equals("\\")){Process process = Runtime.getRuntime().exec("cmd /c COPY "+ sourcePath + " "+targetPath);//process.destroy();}}catch(Exception e){e.printStackTrace();}}

采用NIO方式读取文件内容:

private static String nioReadFile(String filePath,int allocateSize) throws FileNotFoundException{StringBuffer resultSB = new StringBuffer();File file  = new File(filePath);FileChannel in = null;FileInputStream inStream = null;inStream = new FileInputStream(file);in = inStream.getChannel();int len;ByteBuffer buffer = ByteBuffer.allocate(allocateSize);Charset charset = Charset.forName("UTF-8");CharsetDecoder decoder = charset.newDecoder();try {while( (len = in.read(buffer))!= -1){buffer.flip();CharBuffer charBuffer = decoder.decode(buffer);buffer.clear();resultSB.append(charBuffer.toString());}} catch (CharacterCodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return resultSB.toString();}

对于文件读写,在NIO中还额外提供了一种将文件直接映射到内存的方法。其读写操作更快,对于小文件,对内存要求不大的文件可以采取这种方式:

写文件:

public static void writeFileByMappedBuffer(String sourcePath) throws IOException{FileChannel fc = new RandomAccessFile(sourcePath,"rw").getChannel();int numOfInts = 100;IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, numOfInts * 4).asIntBuffer();for(int i=0; i<numOfInts; i++){//wtrite to fileib.put(i);}if(fc != null)fc.close();}

读文件:

public static void readFileByMappedBuffer(String sourcePath) throws IOException{FileChannel fc = new FileInputStream(sourcePath).getChannel();IntBuffer ib = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asIntBuffer();while(ib.hasRemaining()){//read all dataib.get();}if(fc != null){fc.close();}}


0 0
原创粉丝点击