


可以读写基本数据类型的数据 数据输入流:DataInputStream DataInputStream(InputStream in) 数据输出流:DataOutputStream DataOutputStream(OutputStream out) ===================================public static void main(String[] args) throws IOException { // 写 // write(); // 读 read(); } private static void read() throws IOException { // DataInputStream(InputStream in) // 创建数据输入流对象 DataInputStream dis = new DataInputStream( new FileInputStream("dos.txt")); // 读数据 byte b = dis.readByte(); short s = dis.readShort(); int i = dis.readInt(); long l = dis.readLong(); float f = dis.readFloat(); double d = dis.readDouble(); char c = dis.readChar(); boolean bb = dis.readBoolean(); // 释放资源 dis.close(); System.out.println(b); System.out.println(s); System.out.println(i); System.out.println(l); System.out.println(f); System.out.println(d); System.out.println(c); System.out.println(bb); } private static void write() throws IOException { // DataOutputStream(OutputStream out) // 创建数据输出流对象 DataOutputStream dos = new DataOutputStream(new FileOutputStream( "dos.txt")); // 写数据了 dos.writeByte(10); dos.writeShort(100); dos.writeInt(1000); dos.writeLong(10000); dos.writeFloat(12.34F);//有一个类型转换 dos.writeDouble(12.56); dos.writeChar('a'); dos.writeBoolean(true); // 释放资源 dos.close(); }==========================================注意一个问题,如果只有DataOutputStream来写文件,直接双击打开文件,读到的数据是乱码


对于ByteArrayOutputStream:
此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用toByteArray()
和 toString()
获取数据。
关闭 ByteArrayOutputStream
无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException
。(说白了就是不用close方法了)
对于ByteArrayInputStream
:
ByteArrayInputStream
包含一个内部缓冲区,该缓冲区包含从流中读取的字节。内部计数器跟踪 read
方法要提供的下一个字节。
关闭 ByteArrayInputStream
无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException
。
public static void main(String[] args) throws IOException { // 写数据 // ByteArrayOutputStream() ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 写数据 for (int x = 0; x < 10; x++) { baos.write(("hello" + x).getBytes()); } // 释放资源 // 通过查看源码我们知道这里什么都没做,所以根本需要close() // baos.close(); // public byte[] toByteArray() byte[] bys = baos.toByteArray(); // 读数据 // ByteArrayInputStream(byte[] buf) ByteArrayInputStream bais = new ByteArrayInputStream(bys); int by = 0; while ((by = bais.read()) != -1) { System.out.print((char) by); } // bais.close();//不需要close } 4.打印流的概述和特点 打印流 字节流打印流 PrintStream 字符打印流 PrintWriter 打印流的特点: A:只有写数据的,没有读取数据。只能操作目的地,不能操作数据源。 B:可以操作任意类型的数据。 C:如果启动了自动刷新,能够自动刷新。(如果不启动的话,不刷新文件(就是flush或者close)里面是没有数据的) D:该流是可以直接操作文本文件的。 哪些流对象是可以直接操作文本文件的呢? FileInputStream FileOutputStream FileReader FileWriter PrintStream PrintWriter 看API,查流对象的构造方法,如果同时有File类型和String类型的参数,一般来说就是可以直接操作文件的。 流: 基本流:就是能够直接读写文件的 高级流:在基本流基础上提供了一些其他的功能












// 需求:把下面的三个文件的内容复制到pwsum.txt中 // SequenceInputStream(Enumeration e) // 通过简单的回顾我们知道了Enumeration是Vector中的一个方法的返回值类型。 // Enumeration<E> elements() Vector<InputStream> v = new Vector<InputStream>(); InputStream is1 = new FileInputStream("pw.txt"); InputStream is2 = new FileInputStream("pw2.txt"); InputStream is3 = new FileInputStream("pw3.txt"); v.add(is1); v.add(is2); v.add(is3); Enumeration<InputStream> e = v.elements(); SequenceInputStream sis = new SequenceInputStream(e);//接收一个参数 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("pwsum.txt")); byte[] bys = new byte[1024]; int len = 0; while ((len = sis.read(bys)) != -1) { bos.write(bys, 0, len); } sis.close(); bos.close(); } 14.序列化流和反序列化流的概述和使用序列化流 ObjectOutputStream反序列化流 ObjectInputStream 序列化流:把对象按照流一样的方式存入文本文件或者在网络中传输。对象 -- 流数据(ObjectOutputStream) 反序列化流:把文本文件中的流对象数据或者网络中的流对象数据还原成对象。流数据 -- 对象(ObjectInputStream) 首先,写序列化流Demo的write方法。public static void main(String[] args) throws IOException, ClassNotFoundException { // 由于我们要对对象进行序列化,所以我们先自定义一个类 // 序列化数据其实就是把对象写到文本文件 write(); } private static void write() throws IOException { // 创建序列化流对象 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream( "oos.txt")); // 创建对象 Person p = new Person("林青霞", 27); // public final void writeObject(Object obj) oos.writeObject(p); // 释放资源 oos.close(); }======================================================然后,创建一个Person类import java.io.Serializable; public class Person implements Serializable { private String name; private int age; public Person() { super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } 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; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }============================要说明的是,在Person类还没实现 Serializable 这个接口的时候,运行会有异常NotSerializableException:未序列化异常
















1:JDK4新IO要了解的类(自己看)
Buffer(缓冲),Channer(通道)
2:JDK7要了解的新IO类
Path:与平台无关的路径。
Paths:包含了返回Path的静态方法。
public static Path get(URI uri):根据给定的URI来确定文件路径。
Files:操作文件的工具类。提供了大量的方法,简单了解如下方法
public static long copy(Path source, OutputStream out) :复制文件
public static Path write(Path path, Iterable<? extends CharSequence> lines, Charset cs, OpenOption... options):
把集合的数据写到文件。
//复制文件
Files.copy(Paths.get("Demo.java"), newFileOutputStream("Copy.Java"));
//把集合中的数据写到文件
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
Files.write(Paths.get("list.txt"), list, Charset.forName("gbk"));
=================================================================================================
注意:ArrayList实现了Iterable接口
因此Iterable<? extends CharSequence> lines对应的参数可以是ArrayList的对象
先测试copy功能
然后测试write功能public static void main(String[] args) throws IOException { ArrayList<String> array = new ArrayList<String>(); array.add("hello"); array.add("world"); array.add("java"); Files.write(Paths.get("array.txt"), array, Charset.forName("GBK")); } ====================================================以下是自己写的代码



