文件的编码,对File的操作和熟悉

文件一般默认保存为ANSI编码格式。

java.io.File类用于表示文件(目录)

File类只用于表示文件(目录)的信息(名称、大小等),不能用于文件内容的访问。

RandomAccessFile java提供的对文件内容的访问,既可以读文件,也可以写文件。

RandomAccessFile支持随机访问文件,可以访问文件的任意位置

 

 (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) 文件读写完成后一定要关闭(Oracle官方说明)

代码如下:

复制代码
public class EncodeDemo {
    public static void main(String[] args) throws Exception {
        String s = "慕课ABC";
        byte[] bytes1 = s.getBytes();//转换成字节序列用的是项目默认的编码gbk
        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
        byte[] bytes4 = s.getBytes("utf-16be");
        //utf-16be编码中文占用2个字节,英文占用2个字节
        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编码
         * 联通、联这是一种巧合,他们正好符合了utf-8编码的规则
         */
    }
}
复制代码

 代码:操作File

复制代码
public class FileDemo {
    public static void main(String[] args) {
        //了解构造函数的情况 查帮助
        File file = new File("E:\\javaio");
//        File file1 = new File("e:" +File.separator);
//        System.out.println(file.exists());
        if(!file.exists()){
            file.mkdir(); //file.mkdirs() 多级
        }else {
            file.delete();
        }
        //是否是一个目录 如果是目录返回true,如果不是目录or目录不存在返回的是false
        System.out.println(file.isDirectory());
        //是否是一个文件
        System.out.println(file.isFile());
        
        File file2 = new File("E:\\javaio","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(file2.getName());
        System.out.println(file.getParent());
        System.out.println(file2.getParent());
        System.out.println(file.getParentFile().getAbsolutePath());
    }
}
复制代码

 

posted @   Resign~as  阅读(249)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示