解决RandomAccessFile写下字符串乱码

解决RandomAccessFile写入字符串乱码
使用RandomAccessFile有时,写入文件时,会成乱码,原因是由于字符串编码不一致造成的,在写入的时候,加入转码后,在获取字节即可,
代码如下:

public static void solveData()throws Exception{
		
		
		RandomAccessFile r=new RandomAccessFile("data.txt", "rw");//读取一个文件
		RandomAccessFile w=new RandomAccessFile("D://result.txt", "rw");//判断写入另一个文件
		
		String temp="";
		Pattern p=Pattern.compile("\\((.+?)\\)");//匹配()
		int count=0;
		while((temp=r.readLine())!=null){
			
			Matcher m=p.matcher(temp);
			//System.out.println(m.find());
			StringBuffer sb=new StringBuffer();
			while(m.find()){
				String pr=m.group();
			    sb.append("        ").append(pr).append("         \n");
			}
			if(sb.toString().trim().length()>0){
				String h=new String((temp+sb.toString()).getBytes("iso-8859-1"),"UTF-8");//加入乱码控制
				System.out.println(h);//输出是否为正常编码
			    w.write(h.getBytes());//获取字节输出
			    count++;
			}
		}
		r.close();
		w.close();
		System.out.println("本次,检测共有"+count+"条数据");
		
		
		
		
	}


如果遇到乱码异常,可在写入时进行适当转码即可。