java 流 改变编码_Java中流的操作以及编码解码

15d82e52740bd4fd02f285efda4a045e.png我的博客:

https://huangguangda.cn/

https://huangguangda.github.io/

前言:

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

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

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

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

java.lang类string

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

实现的接口:

serializable,charsequence,comparable

public final class String extends Object

implements Serializable, Comparable, CharSequence

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

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

String str = "abc";

=

char data[] = ['a', 'b', 'c'];

String str = new String(data)

String类的方法可以用来查询单个字符,进行比较字符串,提取字符串等等.字符串的串联可以通过StringBuilder或StringBuffer的类来实现.而字符串的转换可以通过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;

}

}联通:

122e39238b833ccc8c14ed17ed540d11.png

联通移动:

76d309a6d05967141ef1ce4f48f6cb08.png

移动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));

}

}

}

48d2cd8fc54ad8710b3767cf626e9297.png

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,对数组进行操作,对数组操作一定要有角标。

da925969bdd84405978ca18bf6826a53.png

BufferedReader

d5adbd8595d764960972a2500fa6223f.png

readpublic 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=='\r'){

continue;

}

if(ch=='\n'){

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 BufferedWriterpublic 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);

}

}

ffa70ee9a7cb18f914cc75d6d2cda051.png

Reader

9cb0c2692bd63d60996f383fce5a7152.png

Reader

读取的:

94d9e6c1d444e4bd5fd1b2d4d21a94a9.png

InputStream

此抽象类表示字节输入流的所有类的超类字节输入流转成字符输入流BufferedReader bufr = new

BufferedReader(new InputStreamReader(System.in));

f0b07c78f62286f91a703a3483d60092.png

字符流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, WriterFileWriter 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 WriterInputStream 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);字节转字符 InputStreamReaderInputStreamReader 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();

24125fcc1dd3bcc846d8b067316c28fb.png

out

7fdc1d8d5c06a1ffe4bd00948cd94551.png

printstream

460cf194f8b0c055a3c0ce4ae7f9e201.png

继承读取文件到文本数据,将数据按照utf-8存储到文件中FileReader fr=new FileReader("a.txt");

FileWriter fw=new FileWriter("b.txt");

15f4437c475d99036de12bb612fe239d.png

转换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 点赞

这是一个有质量,有态度的公众号

喜欢本文的朋友们

欢迎长按下图关注订阅号

收看更多精彩内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值