package fengke.filedemo; |
import java.io.File; |
import java.io.FileNotFoundException; |
import java.io.IOException; |
import java.io.RandomAccessFile; |
/** |
* RandomAccessFileDemoChina乱码问题 |
* @author 锋客 |
* 内容: |
* String s=new String(raf_open.readLine().getBytes("ISO-8859-1"),"utf-8"); |
*/ |
public class RandomAccessFileDemoChina { |
public static void main(String[] args) throws IOException { |
//写入 |
File file= new File( "e:\\luanma.txt" ); |
file.createNewFile(); |
try { |
RandomAccessFile raf= new RandomAccessFile(file, "rw" ); |
raf.writeUTF( "晋城,锋客到此一游!" ); |
raf.close(); |
} catch (FileNotFoundException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
//输出 |
RandomAccessFile raf_open= new RandomAccessFile(file, "r" ); |
System.out.println( "乱码:" +raf_open.readLine()); |
raf_open.seek( 0 ); //必须规到0处 |
//readLine的方式会自动将编码变成ISO-8859-1,后一个是自己的文本编码格式 |
String s= new String(raf_open.readLine().getBytes( "ISO-8859-1" ), "utf-8" ); |
System.out.println(s); |
} |
} |