一、字节流
字节输入流:InputStream:以字节为单位从文件中读取数据
字节输出流:OutputStream:以字节为单位向目标文件中写出数据
文件输入流:FileInputStream:字节输入流是抽象类,不能实例化对象,FileInputStream是InputStream的子类
文件输出流:FileOutputStream:字节输出流是抽象类,不能实例化对象,FileOutputStream是OutputStream的子类
package com.hsj.client; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * 在java中最基础的流类有哪些? * 字节流: * 字节输入流(InputStream(抽象类)):以字节为单位读取数据的流 * 字节输出流(OutputStream(抽象类)):以字节为单位写出数据的流 * * InputStream:以字节为单位读取数据 * | --- FileInputStream:以字节为单独读取文件 * * OutputStream:以字节为单位向目标对象写出数据 * |-- FileOutputStream:以字节为单位向目标文件中写出数据 * * 字符流: * 字符输入流:Reader * 字符输出流:Writer * @author apple * */ public class FileStreamDemo01 { public static void main(String[] args) { String path="hsj.txt"; write(path); read(path); } /** * 读取文件内容 * @param path */ private static void read(String path) { InputStream inputStream=null; try { //1.实例化文件输入流对象并指定要读取的文件路径 inputStream=new FileInputStream(path); //2.读取文件内容 byte[] buffer=new byte[4]; int len=0; while((len=inputStream.read(buffer))!=-1){ String str=new String(buffer,0,len); System.out.println(str); } //inputStream.read(buffer):从文件中读取buffer字节大小的数据并存入到buffer数组中 //返回实际读取到的字节数 /*len=inputStream.read(buffer);//buffer=help System.out.println("len="+len); String str=new String(buffer); System.out.println(str); //len:表示read()方法本次操作实际读取到的字节数 len=inputStream.read(buffer);//buffer= mep System.out.println("len="+len); str=new String(buffer,0,len); System.out.println(str);*/ //int ch=inputStream.read();//每次从文件中读取以字节,当读取不到时返回-1 /*int ch=0; while((ch=inputStream.read())!=-1){ System.out.println((char)ch); } */ /*System.out.println((char)ch); ch=inputStream.read(); System.out.println((char)ch); ch=inputStream.read(); System.out.println((char)ch); ch=inputStream.read(); System.out.println((char)ch); ch=inputStream.read(); System.out.println((char)ch); ch=inputStream.read(); System.out.println((char)ch); ch=inputStream.read(); System.out.println((char)ch); ch=inputStream.read(); System.out.println(ch);*/ } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(inputStream!=null){ try { //3.关闭流 inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * 向指定文件中写出内容 * @param path */ static void write(String path){ OutputStream outputStream=null; try { //1.实例化文件输出流对象并指定文件路径 //如果指定文件不存在则直接创建文件,如果已经存在则直接覆盖 outputStream=new FileOutputStream(path); //第二个参数表示是否采用追加模式,如果为true则为追加模式,否则是覆盖模式 //outputStream=new FileOutputStream(path,true); //2.执行写出操作 outputStream.write("help me".getBytes()); //outputStream.write("love me".getBytes()); System.out.println("文件写出成功!"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(outputStream!=null){ try { //3.关闭流 outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } |
二、字符流
字符输入流:Reader:以字符为单位从文件中读取数据
字符输出流:Writer:以字符为单位向目标文件写出数据
文件读取流和文件写出流:
package com.hsj.client; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; /** * 字符流: * Reader:以字符为单位读取数据的流 * |-- InputStreamReader:转换流,将字节流转换成字符流的桥梁 * |--- FileReader:文件读取流,从文件中读取字符数据的流 * * Writer:以字符为单位写出数据的流 * |-- OutputStreamWriter:转换流,将字符流转换成字节流的桥梁 * |-- FileWriter:文件写出流,将程序中的数据写出到目标文件中 * @author apple * */ public class FileReaderAndWriterDemo02 { public static void main(String[] args) { String path="hsj.txt"; //write(path); read(path); } private static void read(String path) { Reader reader=null; try { //1.实例化字符输入流对象并指定文件路径 reader=new FileReader(path); //2.执行读取操作 int len=0; char[] buffer=new char[2]; //len=reader.read(buffer):每次从reader中读取指定数量的数据并存入到buffer中,返回本次操作实际读取到的字符数 while((len=reader.read(buffer))!=-1){ System.out.println(new String(buffer,0,len)); } /*len=reader.read(buffer); System.out.println(new String(buffer,0,len)); System.out.println("len="+len); len=reader.read(buffer); System.out.println(new String(buffer,0,len)); System.out.println("len="+len); len=reader.read(buffer); System.out.println(new String(buffer,0,len)); System.out.println("len="+len);*/ //int ch=0; /*//ch=reader.read():每次从reader关联的对象中读取一个字符,当读取到最后以后再读取就返回-1,表示读取完毕了 while((ch=reader.read())!=-1){ System.out.println((char)ch); }*/ /*int ch=reader.read(); System.out.println("ch="+(char)ch); ch=reader.read(); System.out.println("ch="+(char)ch); ch=reader.read(); System.out.println("ch="+(char)ch); ch=reader.read(); System.out.println("ch="+(char)ch); ch=reader.read(); System.out.println("ch="+(char)ch); ch=reader.read(); System.out.println("ch="+ch);*/ } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 以字符为单位向目标文件中写出数据 * @param path */ private static void write(String path) { Writer writer=null; try { //1.实例化Writer对象并指定文件路径 writer=new FileWriter(path); //2.向目标文件中写出数据 writer.write("hello"); writer.flush();//刷新缓冲区(将缓冲区中的数据马上写出到目标对象中) writer.write(",world!"); writer.flush(); writer.write("love me"); //writer.close(); writer.write(",love my dog");//java.io.IOException: Stream closed /* * flush()和close()方法有什么不同? * 相同点:都会刷新缓冲区 * 不同点: * flush():只是刷新缓冲区,之后可以继续写出数据 * close():先刷新缓冲区,然后关闭流对象,之后在写出数据就报异常了 */ System.out.println("数据写出成功!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(writer!=null){ try { //3.关闭字符输出流对象 writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } |
BufferedReader和BufferedWriter:
package com.hsj.client; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /** * Reader * |-- BufferedReader * * Writer * |--BufferedWriter * @author apple * */ public class BufferedReaderAndBufferedWriterDemo03 { public static void main(String[] args) { String path="hsj.txt"; write(path); read(path); } private static void read(String path) { BufferedReader br=null; try { //1.实例化缓冲输入流对象 br=new BufferedReader(new FileReader(path)); String line=null; //2.读取数据 while((line=br.readLine())!=null){ System.out.println(line); } /*String line=br.readLine(); System.out.println("line="+line); line=br.readLine(); System.out.println("line="+line); //读取数据时,如果读取不到则返回null line=br.readLine(); System.out.println("line="+line);*/ } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(br!=null){ try { //3.关闭缓冲输入流对象 br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } private static void write(String path) { BufferedWriter bw=null; try { //1.实例化缓冲输出流对象 bw=new BufferedWriter(new FileWriter(path)); //2.写出数据 bw.write("hello"); bw.flush(); //自动写出换行符 bw.newLine(); bw.write("world"); bw.flush(); System.out.println("数据写出成功!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(bw!=null){ try { //关闭缓冲输出流对象 bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } |
DataInputStream和DataOutputStream:
package com.hsj.client; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * InputStream * |--FileInputStream * |--FilterInputStream * |--DataInputStream:提供读取java八大基本数据类型及String数据类型的能力 * * * OutputStream * |--FileOutputStream * |--FilterOutputStream * |--DataOutputStream:提供了写出java八大基本数据类型及String数据类型的能力 * @author apple * */ public class DataInputStreamAndDataOutputStreamDemo05 { public static void main(String[] args) { String path="hsj.txt"; write(path); read(path); } private static void read(String path) { DataInputStream dataInputStream=null; try { //1.实例化数据输入流对象 dataInputStream=new DataInputStream(new FileInputStream(path)); //2.读取java八大基本数据类型及String数据类型,切记:读取数据的顺序必须和写出数据的顺序一致,否则会发生数据错乱 byte b=dataInputStream.readByte(); short s=dataInputStream.readShort(); int i=dataInputStream.readInt(); long l=dataInputStream.readLong(); float f=dataInputStream.readFloat(); double d=dataInputStream.readDouble(); char ch=dataInputStream.readChar(); boolean bool=dataInputStream.readBoolean(); String str=dataInputStream.readUTF(); System.out.println("b="+b+",s="+s+",i="+i+",l="+l+",f="+f+",d="+d+",ch="+ch+",bool="+bool+",str="+str); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(dataInputStream!=null){ //3.关闭数据输入流对象 try { dataInputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * DataOutputStream给我们提供了一种方式能够写出java八大基本数据类型及String数据类到文件中 * @param path */ private static void write(String path) { DataOutputStream dataOutputStream=null; try { //1.实例化数据输出流对象,目的是写出java八大基本数据类型及String数据类型的能力 dataOutputStream=new DataOutputStream(new FileOutputStream(path)); dataOutputStream.writeByte(1); dataOutputStream.writeShort(10); dataOutputStream.writeInt(20); dataOutputStream.writeLong(30); dataOutputStream.writeFloat(5.6f); dataOutputStream.writeDouble(100.5); dataOutputStream.writeChar('a'); dataOutputStream.writeBoolean(true); dataOutputStream.writeUTF("hello"); System.out.println("数据写出完毕"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(dataOutputStream!=null){ try { //3.关闭数据输出流对象 dataOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } |
ObjectInputStream和ObjectOutputStream:
package com.hsj.client; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Date; import com.hsj.domain.Person; /** * InputStream * |--ObjectInputStream:读取对象 * * OutputStream * |-- ObjectOutputStream:写出对象的 * @author apple * */ public class ObjectInputStreamAndObjectOutputStreamDemo06 { public static void main(String[] args) { String path="hsj.txt"; write(path); read(path); } private static void read(String path) { ObjectInputStream objectInputStream=null; try { //1.实例化对象输入流 objectInputStream=new ObjectInputStream(new FileInputStream(path)); //2.执行读取操作 String name=objectInputStream.readUTF(); Date date=(Date) objectInputStream.readObject(); Person p=(Person) objectInputStream.readObject(); System.out.println("name="+name+",date="+date+",p="+p); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if(objectInputStream!=null){ //3.关闭对象输出流 try { objectInputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } private static void write(String path) { ObjectOutputStream objectOutputStream=null; try { //1.实例化对象输出流对象 objectOutputStream=new ObjectOutputStream(new FileOutputStream(path)); //2.执行写出对象的操作 objectOutputStream.writeUTF("张三"); objectOutputStream.writeObject(new Date()); objectOutputStream.writeObject(new Person("张三",30)); System.out.println("对象写出成功!"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(objectOutputStream!=null){ //3.关闭对象输出流 try { objectOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } |