1. 程式人生 > >RandomAccessFile實現文件的增加修改刪除

RandomAccessFile實現文件的增加修改刪除

檔案增加:

        RandomAccessFile rFile =null;
		try {
			File file=new File(filePath);
			rFile = new RandomAccessFile(file,"rw");//讀取檔案
				
			long point = rFile.length();
            
			rFile.seek(point);// 到達檔案尾
	        byte [] ch = new byte[LINE_NUM];//定義一行有多少個個位元組
	        String str = new String(ch);
	        rFile.writeBytes(str);//初始化 使用空行佔位
	        rFile.seek(point);//重新回到檔案尾
	        rFile.writeBytes(data);//寫入資料
		} catch (Exception e) {
			return false;
		} finally {
			try {
				rFile.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

檔案修改:

            File file = new File(filePath);
            rFile = new RandomAccessFile(file, "rw");
            
			String line=null;
			while(null != (line = rFile.readLine())) {//迴圈遍歷
				byte [] ch = new byte[LINE_NUM-sourceIp.length()-MRLF_NUM];//回車符windows佔兩個位元組,linux和mac佔一個位元組
	            String str = new String(ch); //構造要尋找的行內容
				if(line.equals(sourceIp+str)) {//找到要修改的內容					
					long lastpointer = rFile.getFilePointer();//儲存指標
	                long pointer = lastpointer-LINE_NUM;
	                rFile.seek(pointer);//回到行首
	                
	                if(lastpointer == rFile.length()) {//判斷是否為最後一行
	                	byte [] ch1 = new byte[LINE_NUM];
		                String str1 = new String(ch1);
		                rFile.writeBytes(str1);//構造空字串覆蓋原內容
		                rFile.seek(pointer);
		                rFile.writeBytes(replaceIp);//寫入內容    
	                }else {
	                	byte [] ch1 = new byte[LINE_NUM-MRLF_NUM];
		                String str1 = new String(ch1);
		                rFile.writeBytes(str1);
		                rFile.seek(pointer);
		                rFile.writeBytes(replaceIp);
	                }
	                
				}
			}
			
		} catch (Exception e) {
			//異常處理
        }finally {
			try {
				rFile.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return response;

內容刪除:

思路與修改一樣,只是寫入空字串

            File file = new File(filePath);   
			rFile = new RandomAccessFile(file, "rw");
			
			String line; 
			while (null != (line = rFile.readLine())) {
				String str;
				if(rFile.getFilePointer() == rFile.length()) { //判斷是否最後一行
					byte [] ch = new byte[LINE_NUM-data.length()];
		            str = new String(ch);
				} else {
					byte [] ch = new byte[LINE_NUM-MRLF_NUM-data.length()];
		            str = new String(ch);
				}
				
	            if(line.equals(ip+str)){
		                long lastpointer = rFile.getFilePointer();
		                long pointer = lastpointer-LINE_NUM;
		            
		                rFile.seek(pointer);
		                
		                byte [] ch1 = new byte[LINE_NUM];
		                String str1 = new String(ch1);
		                rFile.writeBytes(str1);
		                
		            }
			} 
		
		} catch (Exception e) {
			
        } finally {
			try {
				rFile.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}