zoukankan      html  css  js  c++  java
  • Java SE之IO

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    /**
     * @author Kevin 2018-1-10
     * 
     * io操作工具类。读取office(word)内容建议POI,pdf建议iText。
     * 
     * 学习:http://ifeve.com/java-io/
     *
     */
    public class IOUtil {
    	
    	private static final int DEFAULT_BYTE_SIZE = 1024;
    
    	//can not create object IOUtil
    	private IOUtil(){}
    	
    	/**
    	 * 复制文件
    	 * @param srcPath
    	 * @param descPath
    	 */
    	private static void copyFile(String srcPath, String descPath) {
    		
    		InputStream in = null;
    		OutputStream out = null;
    		
    		try {
    			in = new FileInputStream(new File(srcPath));
    			byte[] bytes = new byte[DEFAULT_BYTE_SIZE];
    			
    			out = new FileOutputStream(descPath);
    			
    			int len = 0;
    			while((len = in.read(bytes)) != -1){
    				out.write(bytes, 0, len);
    			}
    		} catch (Exception e) {
    			
    		} finally {
    			if(in != null){
    				try {
    					in.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    			
    			if(out != null){
    				try {
    					out.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    	
    	/**
    	 * 读取文件为字节数组。
    	 * @param path
    	 * @return
    	 */
    	public static byte[] readFile2Byte(String path){
    		InputStream in = null;
    				
    		try {
    			in = new FileInputStream(new File(path));
    			byte[] bytes = new byte[in.available()];
    			
    			int len = 0;
    			for(int i = 0;(len=in.read())!=-1;i++){
    				bytes[i] = (byte) ((char)len);
    			}
    			return bytes;
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			if(in != null){
    				try {
    					in.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    		return null;
    	}
    	
    }
    
  • 相关阅读:
    利用python在windows环境下爬取赶集网工作信息。
    扔骰子
    python 输入英语单词,查看汉语意思
    获取指定日期的上一个月日期
    爬取代理IP,并判断是否可用。
    递归实现 十进制转换其他进制(2-16)
    特殊回文数
    python 实现无序列表
    python 实现剪刀石头布(三局两胜)
    python 实现简单语音聊天机器人
  • 原文地址:https://www.cnblogs.com/Kevin-1992/p/12608396.html
Copyright © 2011-2022 走看看