day21操作字节数组流,编码解码
来源:互联网 发布:八孔竖笛淘宝 编辑:程序博客网 时间:2024/06/05 00:56
用于操作字节数组的流对象
ByteArrayInputStream
在构造时,需要接收数据源,而数据源是一个字节数组
ByteArrayOutputStream
在构造时,不用定义数组目的,
因为该对象内部已经封装了一个可变长度的字节数组
这就是数据的目的地
可使用 toByteArray() 和 toString() 获取数据。
因为这两个流对象都是操作数组,并没有使用系统资源
所以,不用进行close关闭
************************************************
在流操作规律
源设备:
键盘System.in,硬盘 FileStream,内存 ArrayStream
目的设备:
控制台System.out,硬盘 FileStream,内存 ArrayStream
用流的思想处理数组数据
*/
class ByteArrayStream
{
public static void main(String[] args)
{
//数据源
ByteArrayInputStream bis=new ByteArrayInputStream("ABCDEFD".getByte());
//数据目的
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int by=0;
while ((by=bis.read())!=-1)//du
{
bos.write(by);//xie
}
System.out.println(bos.size());
System.out.println(bos.toString());
}
}
-------------------------------------------------
/*
操作基本数据类型的流对象
DataInputStream与DataOutputStream
*/
class DataStreamDemo
{
public static void main(String[] args) throws IOException
{
//writeData();
//readData();
writeUTFDemo();
//OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("gbk.txt"),"gbk");
//osw.write("你好");
//osw.close();
}
public static void readUTFDemo()throws IOException
{
DataInputStream dis=new DataInputStream(new FileInputStream("utfdata.txt"));
String s=dis.readUTF();
System.out.println(s);
dis.close();
}
public static void writeUTFDemo()throws IOException
{
DataOutputStream dos=new DataOutputStream(new FileOutputStream("utfdata.txt"));
dos.writerUTF("你好");
dos.close();
}
public static void readData()throws IOException
{
DataInputStream dis=new DataInputStream(new FileInputStream("data.txt"));
int num=dis.readInt();
boolean b=dis.readBoolean();
double d=dis.readDouble();
System.out.println("num="+num);
System.out.println("b="+b);
System.out.println("d="+d);
dis.close();
}
public static void writeData()throws IOException
{
DataOutputStream dos=new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeInt(234);
dos.writeBoolean(true);
dos.writeDouble(9887.543);
dos.close();
}
}
-----------------------------------------
/*
编码:字符串变成字节数组
解码:字节数组变成字符串
String-->byte[]: Str.getBytes(charseName);
byte[]-->String: new String(byte[],charseName);
*/
import java.util.*;
class EncodeDemo
{
public static void main(String[] args)
{
String s="你好";
byte[] b1=s.getBytes("GBK");
System.out.println(Arrays.toString(b1));
//String s1=new String(b1,"GBK");
//System.out.println(s1);
//解码错误的解决方法
String s1=new String(b1,"ISO8859-1");
byte[] b2=s1.getBytes("ISO8859-1");
String s2=new String(b2,"GBK");
System.out.println(s2);
//解码错误解决方法:先再编一次,再解一次
String s1=new String(b1,"UTF-8");
byte[] b3=s1.getBytes("UTF-8");
String s3=new String(b3,"GBK");
System.out.println(s3);
}
}
----------------------------------
/*
字符编码
通过子类转换流完成
InputStreamReader
OutputStreamWriter
在两个对象进行构造的时候加入字符集
*/
import java.io.*;
class EncodeStream
{
public static void main(String[] args) throws IOException
{
writeText();
}
public static void readText()throws IOException
{
InputStreamReader isr=
new InputStreamReader(new FileInputStream("gbk.txt"),"GBK");
char[] buf=new char[10];
int len=isr.read(buf);
String str=new String(buf,0,len);
System.out.println(str);
isr.close();
}
public static void writeText()throws IOException
{
OutputStreamWriter osw=
new OutputStreamWriter(new FileOutputStream("utf.txt"),"UTF-8");
osw.write("你好");
osw.close();
}
}
-------------------------------------
import java.io.*;
class ObjectStreamDemo
{
public static void main(String[] args) throws Exception
{
writeObj();//两对同时使用
readObj();
}
public static void readObj()throws Exception
{
ObjectInputStream ois=
new ObjectInputStream(new FileInputStream("obj.txt"));
Person p=(person)ois.readObject();
System.out.println(p);
ois.close();
}
public static void ()throws IOException
{
ObjectOutputStream oos=
new ObjectOutputStream(new FileOutputStream("obj.txt"));
oos.writeObject(new Person("lisi",25));
oos.close();
}
}
----------------------------------------------
- day21操作字节数组流,编码解码
- day21对象流。管道流。RandomAccessFile。DataStream。字节流。常见的编码表。编码解码。联通问题。
- 操作基本数据类型,字节数组的流 和 字符编码
- 字符串到字节数组和字节数组到字符串的转换(编码和解码问题)
- 字节数组操作流
- 黑马程序员-day21-IO流(字符编码)
- java字节数组流操作
- 对编码后的字节数组字符串进行Base64解码并生成图片
- day21 网络编程 URL 网页地址的编码和解码 tcp/udp socket
- base64编码与解码(双字节,asp)
- base64编码与解码(单字节,asp)
- 字节数组 与对象流操作
- 用于操作字节数组的流对象。
- 字节数组操作
- C# 字节数组操作
- 操作字节数组
- C#字节数组操作
- 黑马程序员_java基础篇操作数据的,操作字节数组的,字符编码Day6
- Redis常见集群方案、Codis实践及与Twemproxy比较
- 文件管理-外存分配方式
- 微信公众平台开发——2、代码模式学习总结,接入自己的服务
- POJ-3734 Blocks
- (第6讲)servlet 分页技术
- day21操作字节数组流,编码解码
- java实现在同一Excel文件上写数据
- sql查询 同一个字段 同时满足多个条件
- python 安装jieba...
- 威佐夫博弈模板
- Sorting Algorithm-PreKnowledge
- leetcode-python 第十周
- PHP扩展及PHP7.0安装
- SJPullDownMenu下拉菜单框架使用