获取文件字节数,获取文件换行符,追加字符串到文件指定行

public static int getBytePosition(int rowNumber,String filePath){
	    	BufferedReader bufReader = null;
	    	String lineStr = "";
	    	StringBuffer nContent = null;
	    	int byteLength = 0;
	    	
	    	try {
	    		String lineSeparator = retrieveLineSeparator(new File(filePath));
	    		bufReader = new BufferedReader(new FileReader(org.apache.camel.util.FileUtil.normalizePath(filePath)));
	    		nContent = new StringBuffer();
	    		for (int i = 0; i < rowNumber; i++) {
	    			if((lineStr = bufReader.readLine()) != null){
	    				nContent.append(lineStr);
	    				if(StringUtil.isNotNullOrNotEmptryString(lineSeparator) && lineSeparator.equals("\n")){
	    					byteLength  = nContent.toString().getBytes().length+(rowNumber-1);
	    				} else {
	    					byteLength  = nContent.toString().getBytes().length+(2*rowNumber-2);
	    				}
	    				
		    		}
				}	    		
			} catch (Exception e) {
				log.error("[RatingEngine] error occurred: ERROR ", e);
			}finally{
				//Closing stream				 
	    		try {
	    			if(bufReader != null){  
	    				bufReader.close();     
	    			}
	    		} catch (IOException e) {
	    			log.error("[RatingEngine] error occurred: ERROR ", e);
	    		}
				
			}
			return byteLength;
	    
	    }
	  //add for check Line Separator
	  public static String retrieveLineSeparator(File file) throws IOException {
		    char current;
		    String lineSeparator = "";
		    FileInputStream fis = new FileInputStream(file);
		    try {
		        while (fis.available() > 0) {
		            current = (char) fis.read();
		            if ((current == '\n') || (current == '\r')) {
		                lineSeparator += current;
		                if (fis.available() > 0) {
		                    char next = (char) fis.read();
		                    if ((next != current)
		                            && ((next == '\r') || (next == '\n'))) {
		                        lineSeparator += next;
		                    }
		                }
		                return lineSeparator;
		            }
		        }
		    } finally {
		        if (fis!=null) {
		            fis.close();
		        }
		    }
		    return null;
		}
    
    
    
    /* 
     * Insert contents to the specified location  
     * @param fileName File path+File name
     * @param pos Insert position 【the first row is 0】 
     * @param insertContent  Data that needs to be inserted 
     */  
    public static void appendContents(String filename,int pos,String insertContent){ 
    	File tmp = null;
    	RandomAccessFile raf = null;
    	BufferedOutputStream tmpOut = null;
    	BufferedInputStream tmpIn = null;
    	byte[] buf;
    	try {
    		tmp = File.createTempFile("tmp", null);  
    		raf = new RandomAccessFile(org.apache.camel.util.FileUtil.normalizePath(filename), "rw");  
    		tmpOut = new BufferedOutputStream(new FileOutputStream(tmp));  
    		tmpIn = new BufferedInputStream(new FileInputStream(tmp));  
    		System.out.println(raf.length());  
    		raf.seek(pos);
    		buf = new byte[64];  
    		int hasRead;  
    		//System.out.println(raf.read(buf));  
    		while((hasRead = raf.read(buf))>0){  
    			//Reading and putting the original contents into temporary files 
    			tmpOut.write(buf, 0, hasRead);  
    		}  
    		tmpOut.flush();  
    		raf.seek(pos);  
    		raf.write(insertContent.getBytes());  
    		//Additional temporary file contents
    		while((hasRead = tmpIn.read(buf))>0){  
    			raf.write(buf,0,hasRead);  
    		}  
    	} catch (Exception e) {
    		log.error("[RatingEngine] error occurred: ERROR ", e);
    	}finally{
    		//Closing stream				 
    		try {
    			if(tmpIn != null){  
    				tmpIn.close();     
    			}
    			if (tmpOut!=null) {
    				tmpOut.close(); 
    			}
    			if (raf!=null) {
    				raf.close();
    			}
    			//Deleting temporary files
    			if (tmp!=null&&tmp.exists()) {
    				tmp.deleteOnExit();
				}
    		} catch (IOException e) {
    			log.error("[RatingEngine] error occurred: ERROR ", e);
    		}
    	}  
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值