1. 程式人生 > >獲取檔案位元組數,獲取檔案換行符,追加字串到檔案指定行

獲取檔案位元組數,獲取檔案換行符,追加字串到檔案指定行

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); } } }