RandomAccessFile类学习,随机修改文件内容

 

package org;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.Properties;

public class ABC {

	public static void main(String[] args) {
		
		String path = "c:/project.properties";
		File file = new File(path);
		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile(file, "rw");
			String ss = null;
			while((ss=raf.readLine()) != null){
				if(ss.startsWith("setup=false")){
					//得到当前指针的位置,这时应该是在setup=false\r\n后面
					long i = raf.getFilePointer();
					//减去2的原因是指的\r\n
					raf.seek(i-ss.length()-2);
					int currentLength = ss.length();
					int targetLength = "setup=true".length();
					if(currentLength>targetLength){
						int tempLength = currentLength-targetLength;
						StringBuilder sb = new StringBuilder("setup=true");
						//目的是防止需要修改的内容没有被替换掉,这样就导致显示是错误的
						//setup=false的长度比setup=true多一位,最后就变成了setup=truee
						for(int a=0;a<tempLength;a++){
							sb.append(" ");
						}
						// \r\n是会回车换行
						sb.append("\r\n");
						raf.writeBytes(sb.toString());
					}
					
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				raf.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
		
	}
	
	/**
	 * 将文件的source字符串全部转换为target字符串
	 * 实际上显示读取源文件,然后将源文件的内容全部读取完修改之后的内容再写入到源文件中
	 * @param filepath
	 * @param source
	 * @param target
	 * 
	 * ABC abc = new ABC();
	   abc.changeOneLineContent(path, "project", "黄彪");
	 */
	public void changeOneLineContent(String filepath,String source,String target){
		try{
			File file = new File(filepath);
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			StringBuilder sb = new StringBuilder();
			String temp;
			while((temp = br.readLine()) != null){
				if(temp.contains(source)){
					temp = temp.replace(source,target);
				}
				//没读取一行,最后都需要添加一个回车换行,因为temp字符串是不包含回车换行的。
				sb.append(temp).append("\r\n");
			}
			
			br.close();
			
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(sb.toString().getBytes());
			fos.close();
			
		}catch(Exception e){
			
		}
		
	}
	
	/**
	 * 修改properties配置文件的方法
	 */
	public void  propertiesFileModified(){
		try {
			String path = "c:/project.properties";
			Properties props = new Properties();
			File file = new File(path);
			props.load(new FileInputStream(file));
			System.out.println(props.get("setup"));
			
			if("false".equals(props.get("setup"))){
				FileOutputStream fos = new FileOutputStream(file);  
				props.setProperty("setup", "true");  
				props.store(fos, "huangbiao 备注");  
	            fos.close(); 
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 

备注:RandomAccessFile类是没有删除文件内容的方法的,因此只能通过重新写入文件的方法或者将指定长度的字符串替换为空格占据这个位置。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值