IO流概述
input-输入
input-输出
流的概念:字节的序列
1.流的最小单位是字节
2.对于不同的序列,流也就不同
FileInputStream和FileOutputStream
public static void main(String[] args) throws IOException {
//实例化一个文件对象,在后续流的读取上有很大的作用
File file = new File("D:\\2.txt");
//先判断文件是否存在
if (file.exists()) {
System.out.println("该文件已经存在");
} else
//不存在就先用file的createNewFile去创建一个
file.createNewFile();
//在这里采取的是文件流之间的读写,所以这里便用到了 FileInputStream
FileInputStream is = new FileInputStream("D:\\a12.txt");
//在之前我们先实例了文件对象,并且这就是我们想放的地方
FileOutputStream os = new FileOutputStream(file);
//一次只能传输一些内容,而我们想要传输完所有的内容就自然而然的想到循环
int i = 0;
//而在最后没有内容的时候i会变成-1,这同样也就是循环的出口
while (i != -1) {
//实现读取
i = is.read();
//实现写
os.write(i);
}
is.close();
os.close();
/**
* 这一个例子实现简单的文件流的读取和写入比较需要注意的点就是在多次读取写入的过程中
* 需要注意当读取搭配没有内容的时候i会返回-1 以此就可以来做循环的出口
*/
}
在使用byte类型的读写的方式 会存在 如果文件是字节类型
比如音频 视频 图片 -----文件完整性是能够有保证的
但是如果是文字 字符型的数据 那么数组当中会因为最后一次读取的时候
Byte字节数不够1024个单位 数组当中会存有上次循环读取的数据
也会写入到文件中。
public static void main(String[] args) throws IOException {
File file = new File("D:\\3.txt");
if (file.exists())
{
System.out.println("文件存在");
}
else
file.createNewFile();
FileInputStream is = new FileInputStream("D:\\a12.txt");
FileOutputStream ot = new FileOutputStream(file);
byte[] by = new byte[1024]; //在此创建了一个数组对象,每次以一个数组大小进行读和写
int i = 0;
while((i = is.read(by))!= -1) //!在此处的判断条件非常重要!
{
ot.write(by,0,i); //写操作
}
is.close(); //关流也是不可缺少的
ot.close();
}
byteArrayInputStream和DataInputStream
ByteArrayInputStrem ----主要用于处理缓冲的字节数据
DataInputStream -----主要用于处理 处理缓冲字节数据数组 还用于处理基本数据类型的数据
// ByteArrayInputStrem ----主要用于处理缓冲的字节数据
// DataInputStream -----主要用于处理 处理缓冲字节数据数组 还用于处理基本数据类型的
public static void main(String[] args) throws IOException {
//先写入数据
ByteArrayOutputStream bo = new ByteArrayOutputStream();
//DataOutputStream要和ByteArrayOutputStream连用
DataOutputStream Do = new DataOutputStream(bo);
//写入两个基本类型
Do.writeDouble(3.14);
Do.writeBoolean(false);
ByteArrayInputStream bi =
new ByteArrayInputStream(bo.toByteArray()); //在这里的这个构造方法必须放数组
DataInputStream Di = new DataInputStream(bi);
//记得此处的顺序!
double d = Di.readDouble();
Boolean b = Di.readBoolean();
System.out.println(d);
System.out.println(b);
}
字节流和字符流转换
public static void main(String[] args) throws IOException {
//先放入一个目标文件
// FileInputStream file = new FileInputStream("D:\\2.txt");
// //转换到字符流
// InputStreamReader isr = new InputStreamReader(file);
// //缓冲
// BufferedReader br = new BufferedReader(isr);
// BufferedReader br = new BufferedReader(
// new InputStreamReader(
// new FileInputStream("D://2.txt")));
// 两种方式 第一种代码量较长但是逻辑便于理解,第二种建议在熟练运用之后使用
//读取文件
FileInputStream file = new FileInputStream("D:\\2.txt");
InputStreamReader isr = new InputStreamReader(file);
BufferedReader br = new BufferedReader(isr);
//写
BufferedWriter bw =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("E:\\3.txt")));
String result = ""; //用来放
StringBuffer sb = new StringBuffer(); //用来显示我们写入了什么
while((result = br.readLine())!=null){ //注意此处的判断条件是空不是-1了
// 写的操作
bw.write(result); //写
bw.newLine(); //换行
sb.append(result+"\n");
}
System.out.println(sb.toString()); //输出
bw.flush();
br.close();
bw.close();
}