小工具知识网

当前位置: 首页 > 行业知识 > 学习JAVA的第二十天(基础)

行业知识

学习JAVA的第二十天(基础)

2026-12-06 11:31:00 来源:互联网转载

目录

字符集

 编码和解码

字符流

FileReader

FileWriter

缓冲流 

字节缓冲流

字符缓冲流

转换流                                                              

序列化流        

反序列化流 

 打印流

字节打印流 

 字符打印流

解压缩流                                

压缩流 


 

            前言:学习JAVA的第十九天(基础)-CSDN博客

字符集

GBK字符集:

                       一个英文占一个字节,二进制的第一位是0

                        一个中文占两个字节,二进制高位字节的第一位是1

Unicode字符集的UTF-8编码格式:

                        一个英文占一个字节,二进制的第一位是0,转成十进制是正数

                        一个中文占三个字节,二进制的第一位是1,第一个字节转成十进制是负数

 编码和解码

方法:

方法名说明
byte[] getBytes()使用默认方式进行编码
byte[] getBytes(String charsetName)使用指定方式进行编码
String(byte[] bytes)使用默认方式进行解码
String(byte[] bytes,String charsetName)使用指定方式进行解码

测试类:

 public static void main(String[] args) throws UnsupportedEncodingException {        //编码        String str = "知其不可奈何而安之若命";        byte[] bytes1 = str.getBytes();        //[-25, -97, -91, -27, -123, -74, -28, -72, -115, -27, -113, -81, -27, -91, -120, -28, -67, -107, -24, -128, -116, -27, -82, -119, -28, -71, -117, -24, -117, -91, -27, -111, -67]        System.out.println(Arrays.toString(bytes1));        byte[] bytes2 = str.getBytes("GBK");        //[-42, -86, -58, -28, -78, -69, -65, -55, -60, -50, -70, -50, -74, -8, -80, -78, -42, -82, -56, -12, -61, -4]        System.out.println(Arrays.toString(bytes2));        //解码        String str2 = new String(bytes1);        System.out.println(str2);//知其不可奈何而安之若命        String str3 = new String(bytes2,"GBK");        System.out.println(str3);//知其不可奈何而安之若命    }

字符流

                字符流的底层就是字节流(字符流=字节流+字符集)

特点:

                输入流:一次读一个字节,遇到中文,一次读多个字节

                输出流:底层会把数据按照指定的编码方式进行编码,变成字节再写到文件中

                对纯文本文件进行操作 

FileReader

测试类:

                                                      空参读取

  public static void main(String[] args) throws IOException {        //创建对象        FileReader fr = new FileReader("student\a.txt");        //读取数据        int ch;        while((ch = fr.read()) != -1 ){//            System.out.print(ch);//260412998326041275153226041275152604129983            System.out.print((char) ch);//方生方*** 方***方生        }        //释放资源        fr.close();    }

                                                         带参读取 

public static void main(String[] args) throws IOException {        //创建对象        FileReader fr = new FileReader("student\a.txt");        //读取数据        char[] chars = new char[4];        int len;        while((len = fr.read(chars)) != -1){            System.out.print(new String(chars,0,len));//方生方*** 方***方生        }        //释放资源        fr.close();    }

FileWriter

                                              FileWriter的构造方法

方法说明
FileWriter(File file)创建字符输出流关联本地文件
FileWriter(String pathname)创建字符输出流关联本地文件
FileWriter(File file,boolean append)创建字符输出流关联本地文件,续写
FileWriter(String pathname,boolean append)创建字符输出流关联本地文件,续写

                                                 FileWriter的成员方法

成员方法说明
void  write(int c )写出一个字符
void write(String str )写出一个字符串
void write(String str,int off ,int len)写出字符串的一部分
void write(char[] cbuf)写出一个字符数组
void write(char[] cbuf,int off,int len)写出字符数组的一部分

缓冲流 

字节缓冲流

 原理:

                底层自带了长度8192的缓冲区提高性能

方法:

方法名说明
BufferedInputStream(InputStream is)把基本流包装成高级流,提高读取数据的性能
BufferedOutputStream(OutputStream os)把基本流包装成高级流,提高写出数据的性能

测试类:

                                                                一次读写一个字节

 public static void main(String[] args) throws IOException {        //创建缓冲流对象        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("student\a.txt"));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("student\copy.txt"));                //循环读取        int b;        while((b = bis.read()) != -1){            bos.write(b);        }        //释放资源        bos.close();        bis.close();    }

                                                                   一次读写多个字节     

  public static void main(String[] args) throws IOException {        //创建对象        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("student\copy.txt"));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("student\copy02.txt"));        //一次读多个字节数据        int len;        byte[] bytes = new byte[1024];        while((len = bis.read(bytes)) != -1){            bos.write(bytes,0,len);        }        //释放资源        bos.close();        bis.close();    }

字符缓冲流

原理:

                底层自带了长度8192的缓冲区提高性能

测试类:

                                                        字符缓冲输入流

 public static void main(String[] args) throws IOException {        //创建对象        BufferedReader br = new BufferedReader(new FileReader("student\a.txt"));        //读取数据//        String str = br.readLine();//        System.out.print(str);//方生方*** 方***方生        //循环读取        String  line;        while((line = br.readLine()) != null){            System.out.println(line);//方生方*** 方***方生        }        //释放资源        br.close();    }

                                                          字符缓冲输出流

 public static void main(String[] args) throws IOException {        //创建字符缓冲输出流对象        BufferedWriter bw = new BufferedWriter(new FileWriter("student\b.txt"));        //写入数据        bw.write("大梦谁先觉,平生我自知");        bw.newLine();        bw.write("叩问本心,明心见性");        bw.newLine();                //释放资源        bw.close();    }

转换流                                                              

                转换流是字符流和字节流之间的桥梁 

测试类:

 public static void main(String[] args) throws IOException {        //创建对象指定字符编码        InputStreamReader isr = new InputStreamReader(new FileInputStream("student\b.txt"),"UTF-8");        //读取数据        int ch;        while((ch = isr.read()) != -1){            System.out.print((char) ch);        }        //释放资源        isr.close();//    }

     

  public static void main(String[] args) throws IOException {        //创建转换流的对象        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("student\b.txt"),"UTF-8");        //写出数据        osw.write("吾谁与归");        //释放资源        osw.close();    }

序列化流        

                可以把JAVA中的对象写到本地文件中

构造方法

说明

public ObjectOutputStream(Outputstream out)把基本流包装成高级流
成员方法 说明

public final void writeObject(object obj)

把对象序列化写入文件中

测试类:

 

public static void main(String[] args) throws IOException {        //创建学生类的对象        Student stu = new Student("吴邪",28);        //创建序列化流的对象        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student\b.txt"));        //写出数据        oos.writeObject(stu);        //释放资源        oos.close();    }

反序列化流 

        可以把序列化到本地文件的对象,读取到程序当中

构造方法说明
public ObjectInputStream(Inputstream out)把基本流变成高级流
成员方法说明
public Object  readObject()可以把序列化到本地文件的对象,读取到程序当中

测试类:

 

public static void main(String[] args) throws IOException, ClassNotFoundException {        //创建反序列化流的对象        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student\b.txt"));        //读取数据        Object obj =  ois.readObject();        System.out.println(obj);//Student{name='吴邪', age=28}        //释放资源        ois.close();    }

 打印流

分类:

                        字节打印流:PrintStream

                        字符打印流: PrintWriter 

特点:

                        打印流只操作文件目的地,不操作数据源

                        特有方法实现数据的原样输写

                        特有方法自动刷新、自动换行

字节打印流 

构造方法说明
PrintStream(OutputStream/File/String)关联字节输出流/文件/文件路径
PrintStream(String fileName ,Charset charset)

指定字符编码

PrintStream(OutputStream out,boolean autoFlush)自动刷新
PrintStream(OutputStream out,boolean autoFlush,String encoding)指定字符编码且自动刷新

 

成员方法说明
write(int b)将指定字节写出
println()打印任意数据 ,自动换行,自动刷新(特有方法)
print()打印任意数据,不换行(特有方法)
printf()带有占位符的打印语句,不换行(特有方法)

测试类:

 

public static void main(String[] args) throws FileNotFoundException {        //创建字节流的对象        PrintStream ps = new PrintStream(new FileOutputStream("student\a.txt"),true, Charset.forName("UTF-8"));        //写出数据        ps.println(56515);        ps.print(false);        ps.printf("%d + %d = %d",1,1,2);        //释放资源        ps.close();    }

 字符打印流

                        构造方法和成员方法与字节打印流类似

测试类:

public static void main(String[] args) throws IOException {        //创建字符打印流的对象        PrintWriter pw = new PrintWriter(new FileWriter("student\a.txt"), true);        //写出数据        pw.println("人生无处不青山");        pw.print(65);        pw.printf("%d + %d = %d",1,1,2);        //释放资源        pw.close();    }

解压缩流                                

 测试类:

 public static void main(String[] args) {         //创建一个File表示解压的压缩包        File src = new File("D:\java.zip");        //创建一个File对象表示解压的目的地        File dest = new File("D:\java");    }    public static void unzip(File src,File dest) throws IOException {        //创建解压流的对象        ZipInputStream zip = new ZipInputStream(new FileInputStream(src));        //获取压缩包中的zipentry对象        ZipEntry entry = zip.getNextEntry();        System.out.println(entry);    }

压缩流 

 测试类:

 public static void main(String[] args) throws IOException {        //创建File对象表示要压缩的文件        File src = new File("D:\java.txt");        //创建File对象表示要压缩的位置        File dest = new File("D:\");        //调用方法        tozip(src,dest);    }    public  static void tozip(File src,File dest) throws IOException {        //创建压缩流关联压缩包        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest,"java.zip")));        //创建ZipEntry对象,表示压缩包里面的文件和文件夹        ZipEntry entry = new ZipEntry("java.txt ");        //把ZipEntry对象放入压缩包中        zos.putNextEntry(entry);        //把src文件的数据写入压缩包中        FileInputStream fis = new FileInputStream(src);        int b;        while((b = fis.read()) != -1 ){            zos.write(b);        }        //释放资源        zos.closeEntry();        zos.close();    }

 

 

上一篇:轻松上手,IP下载助手下载指南全解析

下一篇:服务器有胶布吗怎么用?