编码
gbk编码中文占2个字节,字符占1个字节;utf-8中文占3个字节,字符占1个字节;java是双字节编码(utf-16be编码),中文占2个字节,字符也占2个字节;文本文件就是字节系列,可以是任意编码的字节序列,如果我们在中文机器上直接创建文本文件,那么该文本文件只认识ansi编码。
public static void main(String[] args) throws Exception {
final String s = "百度ABC";
byte[] bytes1 = s.getBytes(); //转换成字节序列用的是项目默认的编码
for (byte b : bytes1) {
//把字节(转换成了int)以16进制的方式显示
System.out.print(Integer.toHexString( b & 0xff ) + " ");
}
System.out.println();
byte[] bytes2 = s.getBytes("gbk"); //可以指定编码格式
//gbk编码中文占用2个字节,英文占用1个字节
for (byte b : bytes2) {
System.out.print(Integer.toHexString( b & 0xff) + " ");
}
System.out.println();
byte[] bytes3 = s.getBytes("utf-8");
//utf-8编码中文占用3个字节,英文占用1个字节
for (byte b : bytes3) {
System.out.print(Integer.toHexString( b & 0xff) + " ");
}
System.out.println();
//java是双字节编码 utf-16be,一个字符占2个字节
byte[] bytes4 = s.getBytes("utf-16be");
for (byte b : bytes4) {
System.out.print(Integer.toHexString( b & 0xff) + " ");
}
System.out.println();
/*
* 当你的字节序列是某种编码时,这个时候想把字节序列变成
* 字符串,也需要这种编码方式,否则会出现乱码
*/
String str1 = new String(bytes4); //用项目默认的编码,出现乱码
System.out.println(str1);
String str2 = new String(bytes4, "utf-16be");
System.out.println(str2);
/*
* 文本文件就是字节系列,可以是任意编码的字节序列
* 如果我们在中文机器上直接创建文本文件,那么该文本文件
* 只认识ansi编码
*/
}
运行结果:
File类的使用
java.io.File类用于表示文件(目录)
File类只用于表示文件(目录)的信息(名称、大小等),不能用于文件内容的访问
File常用API
public static void main(String[] args) {
File file = new File("C:"+File.separator+"java_io");
System.out.println(file.exists());
if(!file.exists()){
file.mkdir(); //file.mkdirs();多级目录的创建
}
//是否是一个目录,如果是目录返回true,如果不是目录or目录不存在返回的是false
System.out.println(file.isDirectory());
//是否是一个文件
System.out.println(file.isFile());
//File file2 = new File("C:"+File.separator+"java_io\\1.txt");
File file2 = new File("C:"+File.separator+"java_io", "1.txt");
if(!file2.exists()){
try {
file2.createNewFile(); //创建新的文件
} catch (IOException e) {
e.printStackTrace();
}
}else{
file2.delete();
}
//常用的File对象API
System.out.println(file); //file.toString()内容
System.out.println(file.getAbsolutePath());
System.out.println(file.getName());
System.out.println(file.getParent());
}
RandomAccessFile的使用
RandomAccessFile java提供的对文件内容的访问,既可以读文件,也可以写文件。
RandomAccessFile支持随机访问文件,可以访问文件的任意位置
java文件模型
在硬盘上的文件是byte byte byte存储的,是数据的集合打开文件
有两种模式”rw”(读写) “r”(只读)
RandomAccessFile raf = new RandomAccessFile(file,”rw”)
文件指针,打开文件时指针在开头 pointer = 0;写方法
raf.write(int)–>只写一个字节(后8位),同时指针指向下一个位置,准备再次写入读文件
int b = raf.read()–>读一个字节文件读写完成以后一定要关闭(Oracle官方说明)
raf.close()
简单代码
历出指定目录下的所有文件
/**
* 历出指定目录下(包括子目录)的所有文件
* @param dir
* @throws IOException
*/
public static void listDirectory(File dir) throws IOException{
if(!dir.exists()){
throw new IllegalArgumentException("目录" + dir + "不存在");
}
if(!dir.isDirectory()){
throw new IllegalArgumentException(dir + "不是目录");
}
/*
* String[] filenames = dir.list();返回的是字符串数组, 直接子目录的
* 名称,不包含子目录下的内容
*
* 如果要遍历子目录下的内容就需要构造File对象做递归操作,
* File提供了直接返回File对象的API File.listFiles();
*/
File[] files = dir.listFiles(); //返回的是直接子目录的抽象
if(files != null && files.length>0){
for (File file : files) {
if(file.isDirectory()){
//递归
listDirectory(file);
}else{
System.out.println(file);
}
}
}
}