1.字节数组流(用于操作其他电脑的内存数数据):
我在这里写的程序是操作自己的字节数组所以看着有些别扭:
package othersIO;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import byteIO.InputStream;
/**
*
* 这里我们介绍字节数组流
* 字节数组流一般适用于两台电脑之间(两个不同的内存)的操作
*
* @author Wang
*
*/
public class ByteArrayIO01 {
public static void main(String[] args) throws IOException {
ByteArrayRead();
ByteArrayWrite();
}
public static void ByteArrayRead() throws IOException {
//建立源
String str = "我的老家就住在这里 只想遇到最好的自己";
byte[] src = str.getBytes();
//src = str.getBytes();
//选择流
BufferedInputStream is = new BufferedInputStream(
new ByteArrayInputStream(
src));
byte[] buffer = new byte[1024];
int length = 0;
while((length = is.read(buffer)) != -1) {//把src读到buffer里面然后输出buffer
System.out.println(new String(buffer,0,length));
}
//释放资源关闭文件
is.close();
}
public static void ByteArrayWrite() throws IOException {
//建立目的地
byte[] target = new byte[1024];
//选择流
ByteArrayOutputStream bos = new ByteArrayOutputStream();//这里有不同点 这里有新增方法不适用多态 还有就是他不与目的地建立连接
//操作输出
String src = "操作方法基本与文件输入流一样 但是也有不同的地方";
byte[] srcByte = src.getBytes();
bos.write(srcByte,0,srcByte.length);
//读取数据
target = bos.toByteArray();
//释放资源
bos.close();
System.out.println(new String(target));
}
}
我们使用字节数组流,实现文件的copy
package othersIO;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
*
* 在这里我们使用字节数组流来实现文件的Copy
*
*1、文件 --程序-> 字节数组
* 文件输入流
* 字节数组输出流
*
*2、字节数组 --程序-> 文件
* 字节数组输入流
* 文件输出流
*
* @author Wang
*
*/
public class ByteArrayIO02 {
public static void main(String[] args) throws IOException {
//File src = new File("F:/testIO/java.png");
byte[] srcByteArray = fileToByteArray("F:/testIO/java.png");
byteArrayToFile(srcByteArray,"F:/testIO/java1.png");
}
/**
* 1、文件 --程序-> 字节数组
* 文件输入流
* 字节数组输出流
* @throws IOException
*/
public static byte[] fileToByteArray(String srcPath) throws IOException {
File srcFile = new File(srcPath);
byte[] srcByteArray = null;
BufferedInputStream is = new BufferedInputStream(new FileInputStream(srcFile));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while((length = is.read(buffer)) != -1) {
bos.write(buffer,0,length);
}
bos.flush();// 写文件的时候要强制刷出
srcByteArray = bos.toByteArray(); //先接收一下写入的byte文件
bos.close();//然后在进行关闭
is.close();
return srcByteArray;
}
/**
* 2、字节数组 --程序-> 文件
* 字节数组输入流
* 文件输出流
* @throws IOException
*/
public static void byteArrayToFile(byte[] srcByteArray,String targetPath) throws IOException {
File target = new File(targetPath);
BufferedOutputStream bos = new BufferedOutputStream(//文件流的输出
new FileOutputStream(
target));
BufferedInputStream bis = new BufferedInputStream(//字节数组的读取
new ByteArrayInputStream(
srcByteArray));
byte[] buffer = new byte[1024];
int length = 0;
while((length = bis.read(buffer)) != -1) {
bos.write(buffer,0,length);
}
bos.flush();
bos.close();
bis.close();
}
}
2.处理流(基本数据类型流)
package othersIO;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 数据类型(基本+String)处理流
* 特点就是可以保留数据的类型(只能处理数据的基本类型) 处理的时候不用在考率数据类型的转换
*
* 1、输入流 DataInputStream readXxx()
* 2、输出流 DataOutputStream writeXxx()
* 新增方法不能使用多态
*
* java.io.EOFException :读取到文档末尾也没有读取到相关的内容
*
* @author Wang
*
*/
public class DateIO01 {
public static void main(String[] args) {
//File src = new File("F:/testIO/4.txt");
try {
write("F:/testIO/4.txt");
read("F:/testIO/4.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void write(String targetPath) throws IOException {
double a = 3.1415926;
int b = 4;
String str = "其实我并不喜欢打代码,我就是想整点钱";
File target = new File(targetPath);//创建源
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(
target)));//选择流
//写入数据 写入数据的顺序要与读取的时候一直不然会抛出异常java.io.EOFException :读取到文档末尾也没有读取到相关的内容
dos.writeDouble(a);
dos.writeInt(b);
dos.writeUTF(str);
dos.flush(); //记住只要是输出流也就是写东西的时候就一定要刷出
//释放资源
dos.close();
}
public static void read(String srcPath) throws IOException {
File src = new File(srcPath); //创建源
DataInputStream dis = new DataInputStream( //选择流
new BufferedInputStream(
new FileInputStream(
src)));
//操作 读取的顺序与写出一致 必须存在才能读取
//不一致,数据存在问题
double num1 =dis.readDouble();
int num2 =dis.readInt();
String str =dis.readUTF();
dis.close();
System.out.println(num1+"-->"+str);
}
}
package othersIO;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 我们用字节数组流来操作
*
* @author Wang
*
*/
public class DataIO02 {
public static void main(String[] args) throws IOException {
read(write());
}
public static byte[] write() throws IOException {
byte[] targetByte = null;//目标数组
double a = 3.1415926;
int b = 4;
String str = "其实我并不喜欢打代码,我就是想整点钱";
ByteArrayOutputStream bos =new ByteArrayOutputStream();//这个选择流的过程有点特殊
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(
bos));//选择流 //注意这个再写的时候没有指定地点
//写入数据 写入数据的顺序要与读取的时候一直不然会抛出异常java.io.EOFException :读取到文档末尾也没有读取到相关的内容
dos.writeDouble(a);
dos.writeInt(b);
dos.writeUTF(str);
dos.flush(); //记住只要是输出流也就是写东西的时候就一定要刷出
targetByte = bos.toByteArray();
//释放资源
dos.close();
return targetByte;
}
public static void read(byte[] srcByte) throws IOException {
//File src = new File(srcPath); //创建源
ByteArrayInputStream bais = new ByteArrayInputStream(srcByte);
DataInputStream dis = new DataInputStream( //选择流
new BufferedInputStream(
bais));
//操作 读取的顺序与写出一致 必须存在才能读取
//不一致,数据存在问题
double num1 =dis.readDouble();
int num2 =dis.readInt();
String str =dis.readUTF();
dis.close();
System.out.println(num1+"-->"+str);
}
}
我们在用基本数据流的时候 写入文件里面的东西我们是看不懂的;是下面这个样子 这是给机器看的
注意:
写文件的时候要强制刷出 flush();
在ByteArray中关闭不关闭文件其实都一样 ,因为你读写的一般都是别人的电脑或别的内存上的(他们会自动回收),但是我们统一关闭一下没有什么坏处;
基本数据类型的处理流,保留了数据加类型,因为是处理流所以还要套节点流;
字节数组流的写入字节数组时 也就是Output的时候 是不关联写入地点的(特别注意这一点)
ByteArrayOutputStream bos = new ByteArrayOutputStream();//这里有不同点 这里有新增方法不适用多态 还有就是他不与目的地建立连接
他在使用DateOutputStream()流的时候也很特殊 他要这样写
ByteArrayOutputStream bos =new ByteArrayOutputStream();//这个选择流的过程有点特殊
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(
bos));//选择流 //注意这个再写的时候没有指定地点
因为 他没有建立写入地点 归根结底是因为这个方法 targetByte = bos.toByteArray(); 所以必须保证有这一句ByteArrayOutputStream bos =new ByteArrayOutputStream();