java.io
流的定义
IO指的是Input/Output IO流:输入输出流。统称为数据流。
流向划分:输入流(Input),输出流(Output)
数据传输单位方式划分:字节流(InputStream/OutputStream),字符流(Reader/Writer)
按功能划分:1.节点流:用于直接操作哟目标设备的流
2.处理流:是对一个已存在的流的连接和封装,通过对数据的处理为程序提供给更强大,灵活的读写功能。
File:操作文件或目录的各项属性和文件或者目录的创建删除。(对文件没有读写功能)
字节流:图片,音频,视频等原始字节文件
FileInutStream
输入流如果文件不存在,则抛出FileNotFoundException。
public class BJ1 { public static void main(String[] args) throws IOException { //构造方法 File file=new File("C:\\Users\\Administrator\\Desktop\\飞思学习课件\\Byte.png"); FileInputStream file1=new FileInputStream(file); //FileInputStream file2=new FileInputStream("C:\\Users\\Administrator\\Desktop\\飞思学习课件\\Byte.png"); //read方法 try { file1.read(); int a=file1.read(); byte b[]=new byte[1024]; while(a!=-1){ file1.read(b); } } catch (IOException e) { e.printStackTrace(); }finally { //关闭流的方法 if(file1!=null){ file1.close(); } } } }
FileOutStream
输出流如果不存在,则自动创建文件然后写入数据。
FileOutputStream file4=new FileOutputStream(file); int m= (int) file.length(); byte c[]=new byte[m]; //写入方法 for(byte j:c){ file4.write(c); //计算机首先在文件缓存区对文件进行修改,刷新可以将文件缓存区文件再刷新到磁盘文件里,确保了文件能够在磁盘被修改 file4.flush(); } //关流释放空间 file4.close();
字符流:字符
FileReader(输入流)
read(char[] cbuf, int off, int len) 将字符读入数组的一部分
read(char[] cbuf) 将字符读入数组。
mark(int readAheadLimit) 标记流中的当前位置。
markSupported() 告诉这个流是否支持mark()操作。
ready() 告诉这个流是否准备好被读取
reset() 重置流。
skip(long n) 跳过字符
FileWriter(输出流)
FileWriter file= null; try { file = new FileWriter("C:\\Users\\Administrator\\Desktop\\Hello.txt"); //在文件中写入内容 file.write("HelloJavaWorld你好世界"); } catch (IOException e) { e.printStackTrace(); }finally { //关流 if(file!=null){ try { file.close(); } catch (IOException e) { e.printStackTrace(); } } }
append(char c) 将指定的字符附加到此作者。
append(CharSequence csq) 将指定的字符序列附加到此作者。
close() 关闭流,先刷新。
flush() 刷新流。
write(char[] cbuf) 写入一个字符数组。
write(String str, int off, int len)` 写一个字符串的一部分。
缓冲流
BufferReader
从字符输入流读取文本,缓冲字符,以提供字符,数组和行的高效读取。
可以指定缓冲区大小,或者可以使用默认大小。默认值足够大,可用于大多数用途。
public static void main(String[] args) throws IOException { FileReader file=new FileReader("C:\\Users\\Administrator\\Desktop\\hello.txt"); BufferedReader br=new BufferedReader(file); //读取 String msg=null; /*if((msg=br.readLine())!=null){ System.out.println(msg); }*/ //标记 System.out.println(br.readLine()); br.mark(0); //重置(从标记处重置) br.reset(); System.out.println("-------------------------"); System.out.println(br.readLine()); System.out.println(br.readLine()); //关流 br.close(); file.close(); }
BufferWriter
文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写入。
public static void main(String[] args) throws IOException { FileWriter file=new FileWriter("C:\\Users\\Administrator\\Desktop\\hello.txt"); BufferedWriter bw=new BufferedWriter(file); //写入文件 String msg=("HelloWorld"); bw.write(msg); //写一行分隔符(由于不同操作系统的换行符不同,用此方法的兼容性更好) bw.newLine(); //记得刷新(当然关流也可以刷新 //关流,先创建的后关流 bw.close(); file.close(); }
内存流
ByteArrayInputStream
ByteArrayOutputStream
包含一个内部缓冲区,其中包含可以从流中读取的字节,数据可以使用toByteArray()和toString()。
public static void main(String[] args) throws IOException { //先从应用程序写入到内存里 ByteArrayOutputStream f1=new ByteArrayOutputStream(); //写 f1.write("ldmvkjvbs".getBytes()); f1.flush(); //然后从内存读到应用程序 ByteArrayInputStream f2=new ByteArrayInputStream(f1.toByteArray()); byte ch[]=new byte[1024]; f2.read(ch); System.out.println(new String(ch)); }
CharArrayReader
CharArrayWriter
该类实现了可以用作Writer的字符缓冲区。 当数据写入流时,缓冲区会自动增长。 可以使用toCharArray()和toString()检索数据
public static void main(String[] args) throws IOException { //先从应用程序写入到内存里去 CharArrayWriter f1=new CharArrayWriter(); //写 f1.write("你好啊"); f1.flush(); //从内存读到应用程序 CharArrayReader f2=new CharArrayReader(f1.toCharArray()); //读取 char c[]=new char[1024]; f2.read(c); System.out.println(c); }
转换流
将字节流转换为字符流
InputStreamReader
public static void main(String[] args) throws IOException { //首先创建字节流 FileInputStream f=new FileInputStream("C:\\Users\\Administrator\\Desktop\\hello.txt"); //创建转换流 InputStreamReader f1=new InputStreamReader(f); //读 char c[]=new char[1024]; f1.read(c); System.out.println(new String(c)); //关流 f1.close(); f.close(); }
OutputStreamWriter
public static void main(String[] args) throws IOException { //首先创建字节流 FileOutputStream f=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\hello.txt",true); //转换 OutputStreamWriter f1=new OutputStreamWriter(f); //写 f1.write("Windows操作系统"); //刷新 f1.flush(); f1.close(); f.close(); }
打印流
PrintStream:为零一个输出添加了功能,既能够方便的我打印各种数据值的表示。
public static void main(String[] args) throws FileNotFoundException { FileOutputStream fos = new FileOutputStream("C:\\Users\\lcl0411\\Desktop\\demo.txt",true); PrintStream ps = new PrintStream(fos,true); ps.println("XXXX,hello!"); //ps.flush(); ps.close(); }
PrintWriter:将对象的格式打印到文本输出。
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException { PrintWriter pw = new PrintWriter("C:\\Users\\lcl0411\\Desktop\\demo.txt","UTF-8"); //字节信息中: 数据信息+数据类型 pw.write(12); pw.flush(); pw.close(); }
对象流
ObjectOutputStream:对象信息->字节流(对象信息持久化)序列化
ObiectOutputStream将java对象的原始数据类型和图形写入OutputStream。
ObjectInputStream:字节流(对象信息持久化)->程序中->java对象信息(反序列化)
使用ObiectInputStream读取(重构)对象。可以通过使用流的文件来实现对象的持久存储。
//Student类 public class Student implements Serializable { private String name; private int sid; private char sex; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student(String name, int sid, char sex, int age) { this.name = name; this.sid = sid; this.sex = sex; this.age = age; } public Student(){} @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", sid=" + sid + ", sex=" + sex + ", age=" + age + '}'; }
//Student的序列化与反序列化 public static void main(String[] args) throws IOException, ClassNotFoundException { //序列化 //File f=new File("D:\\练习\\a.txt"); //f.mkdirs(); FileOutputStream file=new FileOutputStream("D:\\练习\\a.txt"); ObjectOutputStream f1=new ObjectOutputStream(file); Student student[]=new Student[3]; student[0]=new Student("张三",306,'男',20); student[1]=new Student("李四",308,'女',20); student[2]=new Student("王五",310,'男',21); for (int i = 0; i <3 ; i++) { f1.writeObject(student[i]); } f1.close(); file.close(); //反序列化 FileInputStream file1=new FileInputStream("D:\\练习\\a.txt"); ObjectInputStream f2=new ObjectInputStream(file1); //放入list集合 List<Student> list=new LinkedList<>(); for (int j = 0; j < 3; j++) { Student student1= (Student) f2.readObject(); list.add(student1); } System.out.println(list); f2.close(); file1.close(); }
注意:
-
没有字符输入/输出流。
-
序列化不会写出任何不实现java.io.Serializable接口的对象的字段(Serializable 默认的序列化)。
-
实现Externalizable接口允许对象完全控制对象的序列化表单的内容和格式。 调用Externalizable接口writeExternal和readExternal的方法来保存和恢复对象的状态。
-
static或者transient修饰的字段是不能被序列化的。