文件传输学习笔记

文件传输学习笔记

文件编码

String s = "黑猫BlackCat";
        byte[] byte1 = s.getBytes();//转换成字节序列用的是项目默认的编码gdk
        for (byte b : byte1){
            //把字节(转换成了int)以16进制的方式去展示
            System.out.print(Integer.toHexString(b &0xff)+" ");
        }
        System.out.println();

gdk

 byte[] byte2 = s.getBytes("gbk");
        //gbk编码中文占用2个字节,英文占用1个字节
        for (byte b : byte2){
            System.out.print(Integer.toHexString(b &0xff)+" ");
        }
        System.out.println();

结果

ba da c3 a8 42 6c 61 63 6b 43 61 74 

utf-8

byte[] byte3 = s.getBytes("utf-8");
        //utf-8编码中文占用3个字节,英文占用1个字节
        for (byte b : byte3){
            System.out.print(Integer.toHexString(b &0xff)+" ");
        }
        System.out.println();

结果

e9 bb 91 e7 8c ab 42 6c 61 63 6b 43 61 74 

java编码

//java是双字节编码 utf-16be
        byte[] byte4 = s.getBytes("utf-16be");
        //utf-16be编码中文占用2个字节,英文占用2个字节
        for (byte b : byte4){
            System.out.print(Integer.toHexString(b &0xff)+" ");
        }
        System.out.println();

结果

9e d1 73 2b 0 42 0 6c 0 61 0 63 0 6b 0 43 0 61 0 74 

乱码

        /**
         * 当你的字节序列式某种编码时,这个时候想把字节序列变成
         * 字符串,也需要用这种编码方式,否则会出现乱码
         */
        String str = new String(byte4);//用项目默认的编码
        System.out.println(str);
        String str2 = new String(byte4,"utf-16be");
        System.out.println(str2);

结果

��s+ B l a c k C a t
黑猫BlackCat

文本文件,就是字节序列,可以是任意编码的字节序列。如果我们在中文机器上直接创建文本文件,那么该文本文件只认识ansi编码 。

File类的使用

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

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

FIle常用API

        //了解构造函数的情况,查帮助
        File file = new File("C:\\Users\\DELL\\Desktop\\图片库\\text.txt");

        //File.separator设置分隔符
        File file1 = new File("C:"+File.separator);
        System.out.println(file.exists());

        if (!file.exists())
            //mkdir() 创建文件夹
            file.mkdir();
            //file.mkdirs(); 多级目录
        else
            //delete() 删除文件/文件夹
            file.delete();

       //判断是否是一个目录;如果是目录返回true,如果不是目录or目录不存在返回的是false
        System.out.println(file.isDirectory());
        //判断是否是一个文件
        System.out.println(file.isFile());

        //File file2 = new File("C:\\Users\\DELL\\Desktop\\图片库\\黑猫.txt");
        File file2 = new File("C:\\Users\\DELL\\Desktop\\图片库","黑猫.txt");
        if (!file2.exists())
            //创建新的文件
            file2.createNewFile();
        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());//父路径
    }


遍历目录(FileUtils)

/**
     * 列出指定目录下(包括其子目录)的所有文件
     * @param dir
     * @throws IOException
     */
    public static void listDirectory(File dir) throws IOException{
        
        if (!dir.exists()){
            throw new IllegalArgumentException("目录:"+dir+"不存在。");
        }
        //isDirectory()方法用于判断FIle类的对象是否是目录
        if (!dir.isDirectory()){
            throw new IllegalArgumentException(dir+"不是目录");
        }
        
        /*//返回的是字符串数组,直接子的名称,不包含子目录下的内容
        String[] filenames = dir.list();//list()方法用于列出当前目录下的子目录和文件
        for (String string:filenames){
            System.out.println(string);
        }*/
        
        //如果要遍历子目录下的内容就需要构造File对象做递归操作,File提供了直接返回File对象的API
        File[] files = dir.listFiles();//返回的是直接子目录(文件)的对象
        if (files!=null && files.length>0){
            for (File file: files){
                if (file.isDirectory()){
                    //递归
                    listDirectory(file);
                }else {
                    System.out.println(file);
                }
            }
        }
        
        
    }

RandomAccessFile类的使用

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官方说明)

       //没有规定位置的话就在目录下建立
       File demo = new File("demo");
        if (!demo.exists()){
            demo.mkdir();
        }
        File file = new File(demo,"raf.dat");
        if (!file.exists()){
            file.createNewFile();
        }

        RandomAccessFile raf = new RandomAccessFile(file,"rw");
        //指针的位置
        System.out.println(raf.getFilePointer());

        raf.write('a');//只写了一个字节
        System.out.println(raf.getFilePointer());
        raf.write('b');

        int i = 0x7fffffff;
        //用write方法每次只能写一个字节,如果要把i写进去就得写四次
        raf.write(i>>>24);//高八位
        raf.write(i>>>16);
        raf.write(i>>>8);
        raf.write(i);
        System.out.println(raf.getFilePointer());

        //可以直接写一个int
        raf.writeInt(i);

        String s= "黑猫";
        byte[] utf = s.getBytes("utf-8");
        raf.write(utf);
        System.out.println(raf.length());

        //读文件,必须把指针移到头部
        raf.seek(0);
        //一次性读取,把文件中的内容都读取到字节数组中
        byte[] buf = new byte[(int)raf.length()];
        raf.read(buf);
        System.out.println(Arrays.toString(buf));

        String s1 = new String(buf);
        Syst em.out.println(s1);

        raf.close();

IO流(输入流,输出流)

字节流

1)InputStream,OutputStream

InputStream抽象了应用程序读取数据的方式

OutputStream抽象了应用程序写出数据的方式

2)EOF=End 读到-1就读到结尾

3)输入流基本方法

int b =in.read();读取一个字节无符号填充到int低八位。-1是EOF
in.read(byte[] buf);读取数据填充到字符数组buf
in.read(byte[] buf,int start,int size);读取数据到字节数组buf,从buf的start位置开始,存放size长度 的数据

4)输出流基本方法

out.write(int b) 写出一个byte到流,b的低八位
out.write(byte[] buf)将buf字节数组都写入到流
out.write(byte[] buf,int start,int size);字节数组buf从start位置,开始写size长度的字节到流

5)FileInputStream—>具体实现了在文件上读取数据

    /**
     * 读取指定文件内容,按照16进制输出到控制台
     * 并且每输出10个byte换行
     * @param fileName
     */
    public static void printHex(String fileName)throws IOException {
        //把文件做为字节流进行读操作
        FileInputStream in = new FileInputStream(fileName);
        int b ;
        int i =1;
        while ((b=in.read())!=-1){
            if (b<=0xf){
                //单位数前面补0
                System.out.print("0");
            }
            System.out.print(Integer.toHexString(b)+" ");
            if (i++%10==0){
                System.out.println();
            }
        }
        in.close();
    }

6)FileOutputStream 实现了向文件中写出byte数据的方法

 public static void printHexByByteArray(String fileName)throws IOException{
        FileInputStream in = new FileInputStream(fileName);
        byte[] buf = new byte[20*1024];
        /**
         * 从in中批量读取字节,放入到buf这个字节数组中,
         * 从第0个位置开始放,最多放buf.length个,
         * 返回的是读到的字节的个数
         */
        /*int bytes = in.read(buf,0,buf.length);//一次性读完,说明字节数组足够大
        int j = 1;
        for (int i=0;i<bytes;i++){
            if (buf[i]<=0xf){
                System.out.print("0");
            }
            System.out.print(Integer.toHexString(buf[i])+" ");
            if (j++%10==0){
                System.out.println();
            }
        }*/
        int bytes = 0;
        int j =1;
        while ((bytes=in.read(buf,0,buf.length))!=-1){
            for (int i =0;i<bytes;i++){
                //byte类型8位,int类型32位,为了避免数据转换错误,通过&0xff将高24位清零
                System.out.print(Integer.toHexString(buf[i]&0xff)+" ");
                if (j++%10==0){
                    System.out.println();
                }
            }
        }
    }

public static void copyFile(File srcFile, File destFile)throws IOException{
        if (!srcFile.exists()){
            throw new IllegalArgumentException("文件:"+srcFile+"不存在");
        }
        if (!srcFile.isFile()){
            throw new IllegalArgumentException(srcFile+"不是文件");
        }
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);
        byte[] buf = new byte[8*1024];
        int b;
        while ((b=in.read(buf,0,buf.length))!=-1){
            out.write(buf,0,b);
            out.flush();//最好加上
        }
        in.close();
        out.close();
    }

7)DataOutputStream/DataInputStream

对“流“功能的扩展,可以更加方面的读取int,long,字符等类型数据

DataOutputStream

writeInt()/writeDouble()/writeUTF()

DOS

  public static void main(String[] args)throws IOException{
        String file ="demo/dos.dat";
        DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
        dos.writeInt(10);
        dos.writeInt(-10);
        dos.writeLong(10l);
        dos.writeDouble(10.5);
        //采用utf-8编码写出
        dos.writeUTF("黑猫");
        //采用utf-16be编码写出
        dos.writeChars("黑猫");
        dos.close();
        IOUtil.printHex(file);
    }

DIS

 public static void main(String[] args)throws IOException{
        String file = "demo/dos.dat";
        IOUtil.printHex(file);
        DataInputStream dis = new DataInputStream(new FileInputStream(file));
        int i = dis.readInt();
        System.out.println(i);
        i=dis.readInt();
        System.out.println(i);
        long I = dis.readLong();
        System.out.println(I);
        double d =dis.readDouble();
        System.out.println(d);
        String s = dis.readUTF();
        System.out.println(s);

        dis.close();
    }

8)BufferedInputStream&BufferedOutputStream

这两个流类位IO提供了带缓冲区的操作,一般打开文件进行写入

或读取操作时,都会加上缓冲,这种流模式提高了IO的性能

从应用程序中把输入放入文件,相当于将一缸水导入到另一个缸中;

FIleOutputStream—>write()方法相当于一滴一滴把水“转移过去”

DataOutputStream—>writeXXX()方法会方便一些,相当于一瓢一瓢把水“转移”过去

BufferedOutputStream---->write方法更方便,相当于一瓢一瓢先放入桶中,再从桶中倒入到另一个缸中,性能提高了

 /**
     * 文件拷贝,字节批量读取
     * @param srcFile
     * @param destFile
     * @throws IOException
     */
    public static void copyFile(File srcFile, File destFile)throws IOException{
        if (!srcFile.exists()){
            throw new IllegalArgumentException("文件:"+srcFile+"不存在");
        }
        if (!srcFile.isFile()){
            throw new IllegalArgumentException(srcFile+"不是文件");
        }
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);
        byte[] buf = new byte[8*1024];
        int b;
        while ((b=in.read(buf,0,buf.length))!=-1){
            out.write(buf,0,b);
            out.flush();//最好加上
        }
        in.close();
        out.close();
    }


    /**
     * 进行文件的拷贝,利用带缓冲的字节流
     * @param srcFile
     * @param destFile
     * @throws IOException
     */
    public static void copyFIleByBuffer(File srcFile, File destFile)throws IOException{
        if (!srcFile.exists()){
            throw new IllegalArgumentException("文件:"+srcFile+"不存在");
        }
        if (!srcFile.isFile()){
            throw new IllegalArgumentException(srcFile+"不是文件");
        }
        BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(destFile));
        int c ;
        while ((c=bis.read())!=-1){
            bos.write(c);
            bos.flush();//刷新缓冲区
        }
        bis.close();
        bos.close();
    }


    /**
     * 单字节,不带缓冲进行文件拷贝
     * @param srcFile
     * @param destFile
     * @throws IOException
     */
    public static void copyFileByByte(File srcFile, File destFile)throws IOException{
        if (!srcFile.exists()){
            throw new IllegalArgumentException("文件:"+srcFile+"不存在");
        }
        if (!srcFile.isFile()){
            throw new IllegalArgumentException(srcFile+"不是文件");
        }
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);
        int c ;
        while ((c=in.read())!=-1){
            out.write(c);
            out.flush();
        }
        in.close();
        out.close();
    }

字符流

1)编码问题

2)认识文本和文本文件

java的文本(char)是16位无符号整数,是字符的Unicode编码(双字节编码)

文件是byte byte byte ···的数据序列

文本文件是文本(char)序列按照某种棉麻方式(utf-8,utf-16be,gbk)序列化为byte

3)字符流(Reader,Writer)—>操作的是文本文本文件

字符的处理,一次处理一个字符

字符的底层任然是基本的字符序列

字符流的基本实现

InputStreamReader 完成byte流解析为char流,按照编码解析
OutputStreamWriter 提供char流到byte流,按照编码处理

    public static void main(String[] args)throws IOException {
        FileInputStream in = new FileInputStream("C:\\Users\\DELL\\Desktop\\图片库\\hs.png");
        InputStreamReader isr = new InputStreamReader(in);//默认项目的编码
        /*int c;
        while ((c=isr.read())!=-1){
            System.out.print((char)c);
        }*/
        char[] buffer = new char[8*1024];
        int c;
        /*批量读取,放入buffer这个字符数组,从第0个位置开始放置,最多放buffer.length个
        返回的是读到的字符的个数
         */
        while ((c = isr.read(buffer,0,buffer.length))!=-1){
            String s = new String(buffer,0,c);
            System.out.print(s);
        }
    }

FileReader/FileWriter

 public static void main(String[] args)throws IOException {
        FileReader fr = new FileReader("C:\\Users\\DELL\\Desktop\\图片库\\text.txt");
        //在文件内容后面追加内容
        FileWriter fw = new FileWriter("C:\\Users\\DELL\\Desktop\\图片库\\2233.txt",true);
        char[] buffer = new char[2056];
        int c ;
        while ((c=fr.read(buffer,0,buffer.length))!=-1){
            fw.write(buffer,0,c);
            fw.flush();
        }
        fr.close();
        fw.close();
    }

字符流的过滤器

BufferedReader--->readLine一次读一行
BufferedWriter/PrintWriter ---->写一行

    public static void main(String[] args)throws IOException{
        //对文件进行读写操作
        BufferedReader br = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream("C:\\Users\\DELL\\Desktop\\图片库\\2233.txt")
                ));
        /*BufferedWriter bw  =new BufferedWriter(
                new OutputStreamWriter(
                        new FileOutputStream("C:\\Users\\DELL\\Desktop\\图片库\\2244.txt")
                )
        );*/
        PrintWriter pw = new PrintWriter("C:\\Users\\DELL\\Desktop\\图片库\\23.txt");
        String line;
        while ((line = br.readLine())!=null){
            System.out.println(line);//一次读一行,并不能识别换行
            /*bw.write(line);
            //单独写出换行操作
            bw.newLine();//换行操作
            bw.flush();*/
            pw.println(line);
            pw.flush();
        }
        br.close();
//        bw.close();
        pw.close();
    }

对象的序列化和反序列化

1)对象序列化,就是讲Object转换成byte序列,泛指叫对选哪个的反序列化

2)序列化流(ObjectOutputStream),是过滤流----writeObject

反序列化(ObjectInputStream)—>readObject

3)序列化接口(Serializable)

对象必须实现序列化接口,才能进行序列化,否则将出现异常

这个接口,没有任何方法,只是一个标准

4)transient//该元素不会进行jvm默认的序列化,也可以自己完成这个元素的序列化

private void writeObject(java.io.ObjectOutputStream s)throws java.io.IOException{
    s.defaultWriteObject();//把jvm能默认序列化的元素进行序列化操作
    s.writeInt(stuage);//自己完成stuage的序列化
}
private void readObject(java.io.ObjectInputStream s)throws java.io.IOException,ClassNotFoundException{
    s.defaultReadObject;//把jvm能默认反序列化的元素进行反序列化操作
    this.stuage = s,readInt();//自己完成stuage的反序列化
}

5)序列化中,一个课实现了序列化接口,那么其子类都可以进行序列化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值