package io;
import java.io.FileInputStream;
import java.io.IOException;
/**
* 文件输入流,用于读取文件数据
* @author ChenYi
* @date 2019.08.28
*/
public class FISDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("fos.txt");
byte[] data=new byte[100];
int len=fis.read(data);
System.out.println("实际读取了"+len+"个字节");
String str=new String(data,0,len,"gbk");
System.out.println(str);
fis.close();
}
}
package io;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* java标准的IO:输入与输出
* 标准的IO是让我们可以用相同的读写方式读写一切需要读写的外界设备
*
* java.io.InputStream和OutputStream
* 他们是所有字节输入流和输出流的超类,并且他们是抽象类自身是不能实例化的,但是规定了所有流都必须具备的读写方法
*
* java将流按照方向划分为输入和输出,输入流用于读数据,输出流用于写数据
*
* java将流分为两类:节点流和处理流
* 节点流也成为了低级流,是实际连接程序和数据源的管道,负责实际读写数据的流,读写数据一定是建立在节点流的基础上进行的
*
* 处理流也称为高级流,不能独立存在,必须连接在其他流上,目的是当数据流经当前流时可以对数据进行加工处理,简化我们对数据的相关操作工作
*
* 串联一组高级流并最终连接到低级流上,使得读写数据以流水线式的方式进行,这种操作称为流的连接,这也是学习IO的精髓所在
*
* 流读写数据时是顺序读写的,即:读写只能顺序向后进行,不能回退
* @author ChenYi
* @date 2019.08.28
*/
public class FOSDemo {
public static void main(String[] args) throws IOException {
/*
* 常用构造方法
* FileOutputStream(File file)
* FileOutputStream(String path)
* 以上两种创建方式为覆盖写模式,即:若创建文件流时发现该文件已经存在,
* 则会先将该文件原有数据清除,然后通过该流写出的数据会作为文件数据保存
*
* FileOutputStream(File file,boolean append)
* FileOutputStream(String path,boolean append)
* 文件流构造器有一个重载,就是再传入一个boolean类型的参数,若该值为true,则是追加模式,
* 即:原有数据保留,写出的内容会被追加到文件中
*/
FileOutputStream fos=new FileOutputStream("fos.txt");
String str="回首~掏~";
fos.write(str.getBytes("gbk"));
System.out.println("写完了");
fos.close();
}
}
package io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
/**
* 对象输入流
* 工作是进行对象的反序列化
* @author ChenYi
* @date 2019.08.28
*/
public class OISDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
FileInputStream fis=new FileInputStream("person.zip");
ObjectInputStream ois=new ObjectInputStream(fis);
/*
* 对象输入流在还原对象时要求读取的字节必须是通过对象输出流写出对象所序列化的一组字节,
* 否则会抛出异常ClassNotFoundException
*
* 对象经过oos写出时,oos首先将该对象按照其结构转换为了一组字节,这个过程称为:对象序列化
*
* 序列化后的字节再经过fos写入文件(写入磁盘)做长久保存,这个过程称为:数据持久化
*/
Person p=(Person)ois.readObject();
System.out.println(p);
ois.close();
}
}
package io;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
/**
* java.io.ObjectOutputStream
* java.io.ObjectInputStream
*
* 对象流是一对高级流,作用是方便读写java对象,他们可以负责将对象与字节进行相互转换
* @author ChenYi
* @date 2019.08.28
*/
public class OOSDemo {
public static void main(String[] args) throws IOException {
String name="苍老师";
int age=18;
String gender="女";
String[] otherInfo= {
"是一名演员",
"来自岛国",
"已婚",
"广大男性的启蒙老师"
};
Person p=new Person(name, age, gender, otherInfo);
System.out.println(p);
FileOutputStream fos=new FileOutputStream("person.zip");
ObjectOutputStream oos=new ObjectOutputStream(fos);
/*
* 写出的对象所需的类必须实现可序化接口
* 否则会抛出NotSerializableException
*/
oos.writeObject(p);
System.out.println("写出完毕");
oos.close();
}
}
package io;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 缓冲流写出数据的缓冲区问题
* @author ChenYi
* @date 2019.08.28
*/
public class FlushDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("bos.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
String str="hello";
bos.write(str.getBytes("gbk"));
/*
* void flush();
* 强制将缓冲流的缓冲区中已经缓存的数据写出
*/
bos.flush();
System.out.println("挟持完毕");
//缓冲区close时会自动调用一次flush()
bos.close();
}
}
package io;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 使用文件流完成文件的复制
* @author ChenYi
* @date 2019.08.28
*/
public class CopyDemo {
public static void main(String[] args) throws IOException {
/*
* 1.创建文件输入流用于读写源文件
* 2.创建文件输出流用于写复制文件
* 3.循环利用文件输入流读取源文件中的字节通过文件输出流写出到复制文件中完成工作
*/
FileInputStream fis=new FileInputStream("卡布奇诺.mp3");
FileOutputStream fos=new FileOutputStream("卡布奇诺3.mp3");
byte[] data=new byte[1024*10];
int len=-1;
while ((len=fis.read(data))!=-1) {
fos.write(data,0,len);
}
System.out.println("复制成功");
fis.close();
fos.close();
}
}
package io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 使用缓冲流完成文件复制
*
* 缓冲流:
* java.io.BufferedOutputStream
* java.io.BufferedInputStream
* 缓冲流是一对高级流,作用是提高数据的读写效率
* @author ChenYi
* @date 2019.08.28
*/
public class CopyDemo2 {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("卡布奇诺.mp3");
BufferedInputStream bis=new BufferedInputStream(fis);
FileOutputStream fos=new FileOutputStream("卡布奇诺1.mp3");
BufferedOutputStream bos=new BufferedOutputStream(fos);
int d=-1;
long start=System.currentTimeMillis();
/*
* 缓冲流内部有一个字节数组作为缓冲区,默认为8K.
* 缓冲流会将读写数据统一转换为块读写操作,因此可以提高读写效率
*/
while ((d=bis.read())!=-1) {
bos.write(d);
}
long end=System.currentTimeMillis();
System.out.println("复制成功,耗时:"+(end-start)+"ms");
bis.close();
bos.close();
}
}
package io;
import java.io.Serializable;
import java.util.Arrays;
/**
* 使用当前类实例测试对象流的对象读写操作
* @author ChenYi
* @date 2019.08.28
*/
public class Person implements Serializable{
private String name;
private int age;
private String gender;
/*
* 被transient修饰的属性在序列化时会被忽略,忽略不必要的属性可以达到对象序列化瘦身操作
*/
private String[] otherInfo;
public Person(String name, int age, String gender, String[] otherInfo) {
super();
this.name = name;
this.age = age;
this.gender = gender;
this.otherInfo = otherInfo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String[] getOtherInfo() {
return otherInfo;
}
public void setOtherInfo(String[] otherInfo) {
this.otherInfo = otherInfo;
}
public String toString() {
return name+","+age+","+gender+","+Arrays.toString(otherInfo);
}
}
package io;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 使用转换流读取文本数据
* @author ChenYi
* @date 2019.08.29
*/
public class ISRDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("./src/io/ISRDemo.java");
InputStreamReader isr=new InputStreamReader(fis,"gbk");
int d=-1;
while ((d=isr.read())!=-1) {
char c=(char)d;
System.out.print(c);
}
isr.close();
}
}
package io;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
* 字符流
* Java将流按照读写单位划分为:字节流与字符流
* 字节流:InputStream,OutputStream作为超类
* 读写数据是以字节为单位读写的
*
* 字符流:Reader,Writer作为超类
* 读写数据是以字符为单位读写的,因此字符流天生具有编解码能力,只适合读文本数据
*
* 转换流:InputStreamReader和OutputStreamWriter
* 他们是字符流中非常常用的一套实现类,在流连接中是非常重要的一环
* 但是我们很少直接操作这两个类
*
* 其他的字符流在流连接中使用时有一个要求,只能连接在其他字符流上,但是我们常用的低级流通常都是字节流,这会导致不能串联在一起,
* 而转换流的意义就在这里,他们是可以连接在字节流上的字符流,而其他的字符流又可以连接在他们上面,因此起到“转换器”的作用
*
* @author ChenYi
* @date 2019.08.29
*/
public class OSWDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("osw.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos,"gbk");
/*
* 通过转换流写出的文字,会按照创建转换流时指定的字符集转换为字节后写出
*/
osw.write("来了老弟");
osw.write("来了来了");
System.out.println("写出完毕");
osw.close();
}
}
package io;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 缓冲字符输入流:BufferedReader
* 块读,并且可以按行读取字符串
* @author ChenYi
* @date 2019.08.29
*/
public class BRDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("./src/io/BRDemo.java");
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
/*
* String readLine()
* 连续读取若干字符,直到换行符为止,然后将换行符之前的字符以一个字符串形式返回,
* 若返回值为null,则表示流读取到了末尾
*/
while (true) {
String line=br.readLine();
System.out.println(line);
if (line==null)
break;
}
br.close();
}
}
package io;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 缓冲字符流
* java.io.BufferedWriter
* java.io.BufferedReader
* 缓冲字符流读写文本数据效率高,并且可以按行读写文本数据
*
* java.io.PrintWriter
* 具有自动行刷新功能的缓冲字符输出流(内部连接了BufferedWrite作为缓冲功能)
* @author ChenYi
* @date 2019.08.29
*/
public class PWDemo1 {
public static void main(String[] args) throws IOException {
/*
* PrintWrite提供了对文件写操作的构造方法
* PrintWrite(File file)
* PrintWrite(String path)
*
* 重载的构造方法可以指定写出时的字符集
* PrintWrite(File file,String csn)
* PrintWrite(String path,String csn)
* csn:charset name 字符集的名字
*/
PrintWriter pw=new PrintWriter("pw.txt");
pw.println("左手小灵通");
pw.println("右手大哥大");
System.out.println("输出完毕");
pw.close();
}
}
package io;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
/**
* 自行使用流连接完成对文件的按行写出操作
* @author ChenYi
* @date 2019.08.29
*/
public class PWDemo2 {
public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("pw2.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos,"gbk");
BufferedWriter bw=new BufferedWriter(osw);
PrintWriter pw=new PrintWriter(bw);
pw.println("托儿索");
pw.println("钢闪斩");
pw.println("风之壁障");
pw.println("E来E去");
pw.println("死亡龙卷风");
System.out.println("输出完毕");
pw.close();
}
}
package io;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* 完成简易记事本
* @author ChenYi
* @date 2019.08.29
*/
public class PWDemo3 {
public static void main(String[] args) throws IOException {
Scanner scan=new Scanner(System.in);
System.out.println("请输入文件名");
String file=scan.nextLine();
FileOutputStream fos=new FileOutputStream(file,true);
OutputStreamWriter osw=new OutputStreamWriter(fos);
BufferedWriter bw=new BufferedWriter(osw);
/*
* PrintWrite的构造方法如果第一个参数是流,那么就是一个boolean类型的第二个参数,若该值为true时
* 就具有了自动行刷新功能,每当调用println方法时会自动flush
*/
PrintWriter pw=new PrintWriter(bw,true);
System.out.println("请开始你的写作之旅,输入exit退出");
while (true) {
String xz=scan.nextLine();
if ("exit".equals(xz)) {
System.out.println("退出");
break;
}
pw.println(xz);
}
System.out.println("输入完成");
pw.close();
}
}