一、文件编码
1、gbk 编码中文占用 2 个字节,英文占用 1 个字节
String s1 = "编码测试ABC";
System.out.println("“" + s1 + "”进行 gbk 编码:");
byte[] bytes1 = s1.getBytes("gbk");
for (byte b: bytes1) {
if ((b & 0xff) <= 0xf) {
System.out.print("0");
}
System.out.print(
Integer.toHexString(b & 0xff) + " "
);
}
System.out.println();
b1 e0 c2 eb b2 e2 ca d4 41 42 43
2、utf-8 编码中文占用 3~4 个字节,英文占用 1 个字节
String s2 = "编码测试ABC";
System.out.println("“" + s2 + "”进行 utf-8 编码:");
byte[] bytes2 = s2.getBytes("utf-8");
for(byte b: bytes2) {
if ((b & 0xff) <= 0xf) {
System.out.print("0");
}
System.out.print(
Integer.toHexString(b & 0xff) + " "
);
}
System.out.println();
e7 bc 96 e7 a0 81 e6 b5 8b e8 af 95 41 42 43
3、utf-16be 编码中文占用 2 个字节,英文占用 2 个字节
java char 字符的编码,utf-16be 为 Unicode 大头编码。
String s3 = "编码测试ABC";
System.out.println("“" + s3 + "”进行 utf-16be 编码:");
byte[] bytes3 = s3.getBytes("utf-16be");
for(byte b: bytes3) {
if ((b & 0xff) <= 0xf) {
System.out.print("0");
}
System.out.print(
Integer.toHexString(b & 0xff) + " "
);
}
System.out.println();
for(int i=0; i<s3.length(); i++) {
if ((s3.charAt(i)>>>8 & 0xff) <= 0xf) {
System.out.print("0");
}
System.out.print(
Integer.toHexString(s3.charAt(i)>>>8 & 0xff) + " "
);
if ((s3.charAt(i)>>>0 & 0xff) <= 0xf) {
System.out.print("0");
}
System.out.print(
Integer.toHexString(s3.charAt(i)>>>0 & 0xff) + " "
);
}
System.out.println();
7f 16 78 01 6d 4b 8b d5 00 41 00 42 00 43
7f 16 78 01 6d 4b 8b d5 00 41 00 42 00 43
4、当字节序列是某种编码时,这个时候想把字符序列变更字符串,也需要用这种编码,否则会出现乱码
编码缺省情况下,将默认使用项目文件的默认编码。
String s4 = "编码测试ABC";
byte[] bytes4 = s4.getBytes("utf-8");
// 乱码 ---> 缂栫爜娴嬭瘯ABC
String str1 = new String(bytes4, "gbk");
System.out.println(str1);
// 正常 ---> 编码测试ABC
String str2 = new String(bytes4, "utf-8");
System.out.println(str2);
文本文件 就是字节序列 byte byte byte ...,
可以是任意编码的字节序列;
如果我们是在中文机器上字节创建文本文件,那么该文本文件只认识 ansi 编码;
联通、联这是一种巧合,他们正好符合了utf-8编码的规则;
在开发项目中,如果项目设置了全局的编码,那么其下面的文件就也需要是这个编码文件才能正常读取,否则就会出现在中文乱码.
二、File 类
java.io.File 类用于表示文件(目录); File 类只用于表示文件(目录)的信息(名称、大小等),不能用于文件内容的访问。
1、常用方法:
常用方法 | 备注 |
---|---|
new File("D:\\Test\\file\\test") | 构造函数,可以用 \\ 或 / 或 File.separator 做路径分隔符; |
file.exists() | 判断该对象是否存在,存在返回 true,不存在返回 false; |
file.isDirectory() | 判断该对象是否表示一个目录,如果是目录返回 true,如果实体不存在或者不是目录返回 false; |
file.isFile() | 判断该对象是否表示一个文件,如果是文件返回true,如果实体不存在或者不是文件返回 false; |
file.mkdirs() | 如目录不存在,则递归创建目录; |
file.createNewFile() | 如文件不存在,则创建文件,但此时如果目录不存在的话会抛出异常; |
file.listFiles() | 获取目录下的所有实体(含目录、文件) |
file.getPath() | 得到的是构造 file 时候的路径; |
file.getAbsolutePath() | 根据构造 file 时候的路径拼接出来的绝对路径; |
filefile2.getCanonicalPath() | 得到的是文件在系统中的全路径; |
2、列出指定目录下(包括其子目录)的所有文件
public static void listDirectory(File dir) throws IOException {
if (!dir.exists()) {
throw new IllegalArgumentException("目录:" + dir + "不存在!");
}
if (!dir.isDirectory()) {
throw new IllegalArgumentException(dir + "不是目录!");
}
File[] files = dir.listFiles();
for (File file: files) {
System.out.println(file.getCanonicalPath());
if (file.isDirectory()) {
listDirectory(file);
}
}
}
public static void main(String[] args) throws IOException {
listDirectory(new File("D:\\Test"));
}
三、RandomAccessFile 类
RandomAccessFile java 提供的对文件内容的访问,既可以读文件,也可以写文件,支持随机访问文件,可以访问文件的任意位置。
1. java文件模型
在硬盘上的文件是byte byte byte存储,是数据的集合;
2. 打开文件
有两种模式“rw”(读写),“r”(只读):
RandomAccessFile raf = new RandomAccessFile(file,"rw")
文件指针,打开文件时指针在开头 pointer = 0;
3. 写方法
raf.write(int) ---> 只写一个字节(后8位),同时指针指向下一个位置,准备再次写入;
4. 读文件
int b = raf.read() ---> 读一个字节;
5. 文件读写完成以后,一定要关闭;
1、打开文件
File dir = new File("D:\\Test\\file\\test");
if (!dir.isDirectory()) {
dir.mkdirs();
}
File file = new File(dir, "raf.dat");
if (!file.isFile()) {
file.createNewFile();
}
// 写入模式下,如文件不存在,则创建文件,但此时如果目录不存在的话会抛出异常;
RandomAccessFile raf = new RandomAccessFile(file, "rw");
// 刚打开文件时,文件指针处于文件头部,写入将覆盖原有文件的此处的字节内容(如有),文件指针随之后移;
System.out.println(raf.getFilePointer());
// do something ...
raf.close();
2、写入文件
// 只写了一个字节
raf.write('A');
System.out.println(raf.getFilePointer());
// 写入一个整型
int i = 0x7fffffff;
// 用 write 方法每次只能写入一个字节,如果要把i写进去就得写4次
raf.write(i >>> 24);
raf.write(i >>> 16);
raf.write(i >>> 8);
raf.write(i );
// 可以直接写一个int
raf.writeInt(i);
System.out.println(raf.length());
// 写入一个字符串
String s = "测试";
byte[] bytes = s.getBytes("gbk");
raf.write(bytes);
System.out.println(raf.length());
3、读出文件
// 读文件,移动指针移动到主要访问的位置
raf.seek(9);
byte[] buf = new byte[4];
raf.read(buf, 0, 4);
for(byte b: buf) {
if ((b & 0xff) <= 0xf) {
System.out.print("0");
}
System.out.print(
Integer.toHexString(b & 0xff) + " "
);
}
System.out.println();
String s1 = new String(buf, "gbk");
System.out.println(s1);

发表评论