zoukankan      html  css  js  c++  java
  • 第57节:Java中流的操作以及编码解码

    标题图

    我的博客:
    https://huangguangda.cn/
    https://huangguangda.github.io/

    前言:

    编码解码:编码时将信息从一种形式变成为另一种形式,成为编码.编码为coding,逆过程为解码.编码时用代码表示的,解码为Decoding,有了编码就有相关的编码表,是对生活中的文件和计算机进行二进制的对应关系.

    ascii,GB2312,unicode,UTF-8
    

    把文字进行转变为二进制位编码,把二进制转变为文字为解码.

    把字符串转变为字节数组为编码,把字节数组转变为字符串为解码.字符串的表示为:string,而字节数组的表现形式为byte[],

    string–>byte[]: 字符串变字符数组,使用getBytes()方法,字节数组变字符串,使用new String((byte[]))方法.

    java.langstring
    java.lang.object->java.lang.string

    实现的接口:

    serializable,charsequence,comparable<string>

    public final class String extends Object
    implements Serializable, Comparable<String>, CharSequence

    String代表字符串,字符串是常量,字符串的值在创建之后就不能更改,只有字符串缓冲区可以支持可变的字符串.

    string对象是不可以变的,但是可以共享.

    String str = "abc";

    =

    char data[] = ['a', 'b', 'c'];
    String str = new String(data)

    String类的方法可以用来查询单个字符,进行比较字符串,提取字符串等等.字符串的串联可以通过StringBuilderStringBuffer的类来实现.而字符串的转换可以通过toString的方法来实现.

    String类表示字符串类型,表示一个UTF-16格式的字符串.

    import java.io.UnsupportedEncodingException;
    public class EnDemo {
    	public static void main(String[] args) throws UnsupportedEncodingException {
    		String str = "abc";
    		byte[] buf1 = str.getBytes("utf-8");
    		String s1 = new String(buf1,"utf-8");
    		System.out.println(s1);
    	}
    }
    

    实例:

    public class CutStringTest {
     public static void main(String[] args){
      String str = "你好aa,谢谢";
      byte[] buf = str.getBytes("GBK");
      for (int i = 0; i < buf.length; i++) {
       String s = cutString(str,i+1);
       System.out.println(str+",截取"+(i+1)+"个结果是:"+s);
      }
      int len = 5;
      String s = cutString(str,len);
      System.out.println(s);
     }
     public static String cutString(String str,int len) throws UnsupportedEncodingException {
      // 中文由两个字节组成
      // 将字符串编码成字节数组
      byte[] buf = str.getBytes("GBK");
      int count = 0;
      // 对数组进行遍历, 从截取位置开始往回遍历
     for(int i = len -1; i >= 0; i--){
      // 判断最后的截取位是否是负数
      if(buf[i]<0){
       count++;
      }else{
       break;
      }
     }
      // 判断奇偶数
     if(count%2==0){
       return new String(buf, 0, len);
     }else{
      return new String(buf,0,len-1);
      // 舍弃最后一个。
     }
      return null;
     }
    }
    

    联通:

    联通

    移动:

    移动

    public class Test {
    	public static void main(String[] args) {
    		String str = "联通";
    		byte[] buf = str.getBytes();
                    // 二进制
    		for(byte b : buf){
    			System.out.println(Integer.toBinaryString(b&255));
    		}
    	}
    }
    

    utf

    字符流的缓冲区

    public class BufferedStreamDemo {
    	public static void main(String[] args) throws IOException {
    		BufferedReader bufr = new BufferedReader(new FileReader("Test.java"));
    		String line = null;
    		while((line=bufr.readLine())!=null){
    			System.out.println(line);
    		}
    		bufr.close();
    	}
    }
    

    readLine和缓冲区

    BufferedReader();字符数组 fr.read(char[]);

    FileReader(); read()字符

    readLine(); 一次读取一行

    readLine():
    调用缓冲区的read方法,是将读取到的字符存储到另一个容器,当读取到终止符时,就将临时容器中存储的数据转换为字符串返回.

    缓冲区:
    使用底层流对象从具体设备上获取数据,并将存储到缓冲区中的数据内,通过缓冲区中read()方法,可以从缓冲区中获取具体的字符数据,可以提高效率,如果用read()方法读取字符数据,并存储到另一个容器中,直到读取到了换行符,这时另一个容器的临时存储的数据就会换成字符串返回,就会形成readLine()的功能.

    实例:
    自定义一个字符流缓冲区,用来缓冲字符数据,从而提高效率,可以使用缓冲区数据的方法.

    缓冲区应该具备什么?
    1,要有数组。
    2,对数组进行操作,对数组操作一定要有角标。

    BufferedReader

    read

    public class MyBufferedReader {
    	private Reader r;
    	// 定义一个字符数组,作为缓冲区。
    	private char[] buf = new char[1024];
    	// 定义了一个索引,用于操作数组中的元素。
    	private int index = 0;
    	// 定义了一个变量,用于记录读取字符的个数。
    	private int count = 0;
    
    	// 需要一初始化就具备一个流对象。
    	public MyBufferedReader(Reader r) {// 可以对Reader的所有子类进行高效读取。
    		this.r = r;
    	}
    
    	public int read() throws IOException {
    
    		/*
    		 * 1,需要先通过流对象从底层设备上获取一定数据的数据到缓冲区数组中。 使用流对象read(char[]);
    		 */
    		//如果count记录字符个数的变量为0,说明缓冲区已经没有字符数据。
    		if(count==0){
    			//需要从设备上获取一定数量的数据存储到缓冲区中,并用count记录存储字符的个数。
    			count = r.read(buf);
    			//每取一次新的数据,就需要将角标归0.
    			index = 0;
    		}
    		//如果count小于0,说明到-1,没有数据了,程序直接返回-1.
    		if(count<0){
    			return -1;
    		}
    		//从缓冲区中取出一个字符。
    		char ch = buf[index];
    		//角标自增。
    		index ++;
    		//计数器要自减。
    		count --;
    		
    		return ch;
    	}
    	
    	public String readLine() throws IOException{
    		//1,定义一个临时容器。
    		StringBuilder sb = new StringBuilder();
    		
    		//2,调用本类中的read方法,从缓冲区中读取一个字符,存储到临时容器中。
    		//存的时候要注意:必须判断,如果是行终止符就不要存储了。就将临时容器中的
    		//字符转成字符串返回。
    		
    		int ch = 0;
    		while((ch=this.read())!=-1){
    			if(ch=='
    '){
    				continue;
    			}
    			
    			if(ch=='
    '){
    				return sb.toString();
    			}
    			
    			sb.append((char)ch);//将读取到的字符数字转成char类型,存储到sb中。
    			
    		}
    		
    		//万一文本中最后以后没有行终止符,判断一下sb中是否有内容,如果有则返回。
    		if(sb.length()!=0){
    			return sb.toString();
    		}
    		
    		return null;
    	}
    	
    	// 关闭流资源。
    	public void close() throws IOException {
    		// 其实内部就是关闭具体的流。
    		r.close();
    	}
    }
    
    public class MyBufferedReaderDemo {
    
    	/**
    	 * @param args
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws IOException {
    		// 验证自定义的缓冲区。
    		MyBufferedReader myBufr = new MyBufferedReader(new FileReader("dashucoding\1.txt"));
    		
    		String line = null;
    		
    		while((line=myBufr.readLine())!=null){
    			System.out.println(line);
    		}
    		
    		myBufr.close();
    	}
    }
    

    字节流:
    FileInputStream FileOutputStream
    BufferedInputStream BufferedOutputStream

    字符流:
    FileReader FileWriter
    InputStreamReader OutputStreamWriter
    BufferedReader BufferedWriter

    public class ReadDemo {
    	public static void main(String[] args) throws IOException {
    
    		/*
    		 * 思路:
    		 * 将数据存储到的文件
    		 * 怎么获取数据来源呢?键盘录入怎么弄呢?
    		 * 键盘录入是输入
    		 * 在System类找到了标准输入流
    		 * System.in 对应的类型是InputStream。字节输入流。
    		 */
    		//获取了键盘录入的输入流对象。可以不用关闭。
    		InputStream in = System.in;
    		
    		int ch = in.read();
    		System.out.println(ch);
    
    		int ch1 = in.read();
    		System.out.println(ch1);
    
    		int ch2 = in.read();
    		System.out.println(ch2);
    
    		int ch3 = in.read();
    		System.out.println(ch3);
    		
    	}
    }
    

    Reader

    Reader

    读取的:

    InputStream

    此抽象类表示字节输入流的所有类的超类

    字节输入流转成字符输入流

    BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
    

    字符流

    public class DataToFileTest {
    
    	public static void main(String[] args) throws IOException {
    
    		//键盘录入。
    		BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
    		//目的是文件。
    //		FileWriter fw = new FileWriter("dashucoding\key.txt");
    //		BufferedWriter bufw = new BufferedWriter(fw);
    		BufferedWriter bufw = new BufferedWriter(new FileWriter("dashucoding\key.txt"));
    		
    		String line = null;
    		while((line=bufr.readLine())!=null){
    			if("over".equals(line)){
    				break;
    			}
    			bufw.write(line);
    			bufw.newLine();
    			bufw.flush();
    		}
    		
    		bufw.close();
    	}
    }
    

    IO流

    操作的数据是数据源还是数据

    源: InputStream Reader
    目的: OutputStream Writer

    要操作的设备是字节还是文本

    源: 字节:InputSteam 文本:Reader
    目的: 字节:OutputSteam 文本:Writer

    具体设备:

    源设备: 硬盘:文件File,内存:数组,字符串,键盘:System.in,网络:Socket

    目的设备: 硬盘:文件File,内存:数组,字符串,屏幕:System.out,网络:Socket

    需求

    源:字符串,目的:文件, OutputStream, Writer

    FileWriter fw = new FileWriter("a.txt");
    fw.write(string);
    

    高效

    BufferedWriter bw= new BufferedWriter(new FilewWriter("a.txt"));
    bw.write(string);
    bw.newLine();
    bw.flush();
    

    复制文本文件

    源:InputStream Reader
    目的:OutStream Writer

    (可有过滤需求)

    FileInputStream fis = new FileInputStream("a.txt");
    FileOutputStream fos = new FileOutputStream("b.txt");
    
    FileReader fr = new FileReader("a.txt");
    FileWriter fw = new FileWriter("b.txt");
    

    高效

    BufferedReader br = new BufferedReader(new FileReader("a.txt"));
    BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
    String line = br.readLine();
    line = line.replace("dashucoding","达叔小生");
    bw.write(line);
    

    读取键盘录入到文件中(一般都是文本)

    源: InputStream Reader
    目的: OutputStream Writer

    InputStream in = System.in;
    FileWriter fw = new FileWriter("a.txt");
    
    byte[] buf = new byte[1024];
    int len = in.read(buf);
    String str = new String(buf,0,len);
    fw.write(str);
    

    字节转字符 InputStreamReader

    InputStreamReader isr=new InputStreamReader(System.in);
    FileWriter fw = new FileWriter("a.txt");
    

    高效

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt");
    

    读取文件到屏幕

    FileReader fr = new FileReader("a.txt");
    OutputStream out = System.out;
    System.out.println();
    

    out

    printstream

    继承

    读取文件到文本数据,将数据按照utf-8存储到文件中

    FileReader fr=new FileReader("a.txt");
    FileWriter fw=new FileWriter("b.txt");
    

    转换

    BufferedReader br = new BufferedReader(new FileReader("a.txt"));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("b.txt"),"utf-8"));
    

    达叔小生:往后余生,唯独有你
    You and me, we are family !
    90后帅气小伙,良好的开发习惯;独立思考的能力;主动并且善于沟通
    简书博客: 达叔小生
    https://www.jianshu.com/u/c785ece603d1

    结语

    • 下面我将继续对 其他知识 深入讲解 ,有兴趣可以继续关注
    • 小礼物走一走 or 点赞
  • 相关阅读:
    java内部类自我总结
    eclipse中调试第三方jar中的代码
    java提升性能的好习惯(转)
    WMI获取驱动版本
    cmd中的特殊符号
    DISM命令应用大全
    C#自检系统
    注册表检查
    PictrueBox 显示Resources里面的图片
    Linq to XML
  • 原文地址:https://www.cnblogs.com/dashucoding/p/11932417.html
Copyright © 2011-2022 走看看