RandomAccessFile的编码转换问题
来源:互联网 发布:淘宝平板版下载 编辑:程序博客网 时间:2024/06/08 04:11
package june;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* File
* |................***............***.......|
* |................|..............|.........|
* 0XL 1XL 2XL 3XL
*/
public class Demo9 {
static File file=new File("aaa.txt");
class WriteFile extends Thread{
File file;
int block;
int L=18;
public WriteFile(File file,int b){
this.file=file;
this.block=b;
}
public void run(){
RandomAccessFile rdm;
try {
rdm=new RandomAccessFile(file,"rw");
rdm.seek((this.block-1)*L);
//writeBytes(String s)舍弃高八位,所以要转换成ISO8859_1。
//它是单字节编码,汉字不能用它来表示(装不下),但由于是单字节编码,和计算机最基础的表示单位一致,所以仍旧常用,表示汉字的时候只能进行拆分,然后用gbk读取
//可以将一个汉字表示成分开的两个字节 格式是:“中文” d6 d0 ce c4
//GBK一个汉字占两个字节格式是 d6d0 cec4
//UTF8一个汉字占3个字节格式是 e4 b8 ad e6 96 87
//用ISO8859_1解码由字符串所转化而成的byte[]数组,最后转换为unicode(定长双字节)存储,把单字节编码转换成双子节编码并保存,会产生乱码。
String str=new String("我是中国".getBytes("GBK"),"ISO8859_1");
System.out.println(str); //输出一堆乱码
rdm.writeBytes(str); //去文件里查看,结果正确
String str2="asdf";
rdm.writeBytes(str2);
//如果是普通的write(byte[] b)方法,只要编解码操作用同一种编码就可以了
// String str=new String("我是中国".getBytes("UTF8"),"UTF8");
// rdm.write(str.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
if(file.exists()){
file.delete();
Demo9 mut=new Demo9();
mut.new WriteFile(file,1).start();
mut.new WriteFile(file,2).start();
mut.new WriteFile(file,3).start();
mut.new WriteFile(file,4).start();
mut.new WriteFile(file,5).start();
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* File
* |................***............***.......|
* |................|..............|.........|
* 0XL 1XL 2XL 3XL
*/
public class Demo9 {
static File file=new File("aaa.txt");
class WriteFile extends Thread{
File file;
int block;
int L=18;
public WriteFile(File file,int b){
this.file=file;
this.block=b;
}
public void run(){
RandomAccessFile rdm;
try {
rdm=new RandomAccessFile(file,"rw");
rdm.seek((this.block-1)*L);
//writeBytes(String s)舍弃高八位,所以要转换成ISO8859_1。
//它是单字节编码,汉字不能用它来表示(装不下),但由于是单字节编码,和计算机最基础的表示单位一致,所以仍旧常用,表示汉字的时候只能进行拆分,然后用gbk读取
//可以将一个汉字表示成分开的两个字节 格式是:“中文” d6 d0 ce c4
//GBK一个汉字占两个字节格式是 d6d0 cec4
//UTF8一个汉字占3个字节格式是 e4 b8 ad e6 96 87
//用ISO8859_1解码由字符串所转化而成的byte[]数组,最后转换为unicode(定长双字节)存储,把单字节编码转换成双子节编码并保存,会产生乱码。
String str=new String("我是中国".getBytes("GBK"),"ISO8859_1");
System.out.println(str); //输出一堆乱码
rdm.writeBytes(str); //去文件里查看,结果正确
String str2="asdf";
rdm.writeBytes(str2);
//如果是普通的write(byte[] b)方法,只要编解码操作用同一种编码就可以了
// String str=new String("我是中国".getBytes("UTF8"),"UTF8");
// rdm.write(str.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
if(file.exists()){
file.delete();
Demo9 mut=new Demo9();
mut.new WriteFile(file,1).start();
mut.new WriteFile(file,2).start();
mut.new WriteFile(file,3).start();
mut.new WriteFile(file,4).start();
mut.new WriteFile(file,5).start();
}
}
}
阅读全文
0 0
- RandomAccessFile的编码转换问题
- RandomAccessFile的乱码问题
- 关于RandomAccessFile的问题
- RandomAccessFile 关于getFilePointer的问题
- day21对象流。管道流。RandomAccessFile。DataStream。字节流。常见的编码表。编码解码。联通问题。
- js对编码转换的问题
- golang转换gbk编码的问题
- 关于文本编码格式转换的问题
- 详解MySQL字符的编码转换问题
- Python的中文编码转换问题
- JNI处理中文的编码转换问题
- PHP iconv()字符编码转换的问题
- 关于JS的编码转换问题
- python一些编码的转换格式问题
- RandomAccessFile 的默认编码为ISO-8859-1
- 字符集编码转换问题
- 字符编码转换问题
- windows 编码转换问题
- LeetCode1-Two Sum
- android 简单逆向
- 杂谈0627
- 单行函数
- mac本安装mysql&&忘记密码后如何进行修改
- RandomAccessFile的编码转换问题
- javascript逻辑运算符“||”和“&&”
- dfsput操作报“There are 0 datanode(s) running and no node(s) are excluded in this operation”
- URG_PSH区别
- java学习笔记1——面向对象和JVM基础
- pthread_exit-----在linux主线程中的用途
- 【翻译】Faster-R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
- 端午个人赛-A return of Nim (Nim+威左夫,拓:巴什)
- MediaStore.Audio.Media属性(本地媒体库)