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
- Java文件操作:拷贝、读取文件等
- JAVA读取等操作Excel文件
- java 文件拷贝操作
- Android 文件操作,删除,拷贝文件等
- java 拷贝文件和读取ini文件
- Java读取文件操作
- JAVA 文件读取操作
- JAVA读取大文件操作
- Java读取操作Property文件
- java文件读取操作笔记
- java目录,文件,等操作
- java对文件拷贝的简单操作
- java 实现 文件操作工具集。包括文件、目录树的拷贝、删除、移动、查找等工具函数
- JAVA读取文本.XML等文件
- java文件的读取、粘贴、移动等
- 文件的拷贝操作
- JavaIO操作-拷贝文件
- JAVA读取PROPERTIY文件/JAVA拷贝一个对象的方式
- greenDao的使用--- 配置 androidstudio
- Kerberos 安装
- C# ListView用法详解
- Android多种方式实现自定义Dialog对话框
- IOS navigationController.popToViewController的使用
- Java文件操作:拷贝、读取文件等
- Composite
- Android Paint
- Hibernate Eager vs Lazy Fetch Type
- http请求中session的创建时期
- Android源码开发之来电接听振动
- HttpUrlConnection Post提交数据到服务器、并得到服务器返回的数据
- JavaIO 系统
- python中inspect使用