数据输入和输出流: 数据输入流: DataInputStream 数据输出流: DataOutputStream 特点: 可以写基本数据类型,可以读取基本数据类型
内存操作流的概述 a:操作字节数组 ByteArrayOutputStream ByteArrayInputStream 此流关闭无效,所以无需关闭 b:操作字符数组 CharArrayWrite CharArrayReader c:操作字符串 StringWriter StringReader
构造方法: public ByteArrayOutputStream()
打印流的特点 a: 打印流只能操作目的地,不能操作数据源(不能进行读取数据) - b: 可以操作任意数据类型的数据 调用print() 方法可以写任意数据类型
c: 如果我们启用自动刷新,那么在调用println、printf 或 format 方法中的一个方法的时候,会完成自动刷新 /** 通过以下构造创建对象 能够启动自动刷新 然后调用println、printf 或 format 方法中的一个方法的时候,会完成自动刷新 public PrintWriter(OutputStream out, boolean autoFlush) 启动 自动刷新public PrintWriter(Writer out, boolean autoFlush) 启动自动刷新 */ d: 这个流可以直接对文件进行操作(可以直接操作文件的流: 就是构造方法的参数可以传递文件或者文件路径) PrintWriter实现自动刷新和换行 public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter (new FileOutputStream ("a.txt"), true); out.println ("王五"); out.println ("wdaw "); out.close (); }打印流复制文本文件 分析:
这个打印流只能进行写数据,不能进行读取数据, 那么我们应该找一个可以读取文本文件中的的数据的流对象进行读取操作.而我们非常喜欢高效的流对象,于是我们可以使用BufferedReader进行读取数据. public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader (new FileReader ("fos.txt")); PrintWriter printWriter = new PrintWriter (new FileOutputStream ("f.txt")); String line=null; while ((line=in.readLine ())!=null){ printWriter.println (line); } in.close (); printWriter.close (); }在System这个类中存在两个静态的成员变量: -
public static final InputStream in: 标准输入流, 对应的设备是键盘
public static final PrintStream out: 标准输出流 , 对应的设备就是显示器 System.in的类型是InputStream. System.out的类型是PrintStream是OutputStream的孙子类FilterOutputStream 的子类.
二种方式实现键盘录入 Scanner BufferedReader的readLine方法。 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
RandomAccessFile概述 最大特点 能读能写 RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。 支持对随机访问文件的读取和写入。 RandomAccessFile的父类是Object , 这个流对象可以用来读取数据也可以用来写数据.可以操作任意数据类型的数据. 我们可以通过getFilePointer方法获取文件指针,并且可以通过seek方法设置文件指针
演示:
public static void main(String[] args) throws IOException { //随机访问流:此流最大的特点是能读能写,而来可以设置文件指针的位置 //此类的实例支持对随机访问文件的读取和写入。 //随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。 //存在指向该隐含数组的光标或索引,称为文件指针; //输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。 RandomAccessFile raf = new RandomAccessFile("e.txt", "rw"); boolean b = raf.readBoolean(); //获取当前文件的指针位置 System.out.println("指针位置:"+raf.getFilePointer()); double v = raf.readDouble(); System.out.println("指针位置:" + raf.getFilePointer()); int i = raf.readInt(); System.out.println("指针位置:" + raf.getFilePointer()); String s = raf.readUTF(); System.out.println("指针位置:" + raf.getFilePointer()); System.out.println(b); System.out.println(v); System.out.println(i); System.out.println(s); //设置指针的位置 raf.seek(13); String s2 = raf.readUTF(); System.out.println(s2); } private static void writeData() throws IOException { RandomAccessFile raf = new RandomAccessFile("e.txt", "rw"); raf.writeBoolean(true); raf.writeDouble(3.14); raf.writeInt(1000); //首先,把两个字节从文件的当前文件指针写入到此文件, //类似于使用 writeShort 方法并给定要跟随的字节数 raf.writeUTF("你好"); raf.close(); }序列化流的概述 所谓的序列化:就是把对象通过流的方式存储到文件中.注意:此对象 要重写Serializable 接口才能被序列化 反序列化:就是把文件中存储的对象以流的方式还原成对象 序列化流: ObjectOutputStream 反序列化流: ObjectInputStream
像这样一个接口中如果没有方法,那么这样的接口我们将其称之为标记接口(用来给类打标记的,相当于猪肉身上盖个章)
一个对象可以被序列化的前提是这个对象对应的类必须实现Serializable接口
使用transient关键字声明不需要序列化的成员变量
private transient int age ;// 可以阻止成员变量的序列化使用transient
通过序列化流与反序列化流完成从file.txt文件存取对象的操作
public class Test4 { public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream ("file.txt")); out.writeObject (new Phone ("苹果",6000)); out.close (); ObjectInputStream in = new ObjectInputStream (new FileInputStream ("file.txt")); Object o = in.readObject (); Phone phone= (Phone) o; System.out.println (phone.getBrand ()+"-----"+phone.getPrice ()); } } class Phone implements Serializable{ private String Brand; private int Price; public Phone(String brand, int price) { Brand = brand; Price = price; } public String getBrand() { return Brand; } public void setBrand(String brand) { Brand = brand; } public int getPrice() { return Price; } public void setPrice(int price) { Price = price; } }Properties的概述 Properties 类表示了一个持久的属性集。 Properties 可保存在流中或从流中加载。 属性列表中每个键及其对应值都是一个字符串。 Properties父类是Hashtable - 属于双列集合,这个集合中的键和值都是字符串 Properties不能指定泛型
public Object setProperty(String key,String value) public String getProperty(String key) public Set stringPropertyNames()
Properties和IO流进行配合使用: - public void load(Reader reader): 读取键值对数据把数据存储到Properties中
public void store(Writer writer, String comments)把Properties集合中的键值对数据写入到文件中, comments注释案例: 我有一个文本文件,我知道数据是键值对形式的,但是不知道内容是什么。 请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其值为”100” 分析: a: 把文本文件中的数据加载到Properties集合中
b: 判断这个集合中是否有"lisi"这个键 如果有直接修改其值为100 c: 把集合中的数据再次存储到文本文件中代码:
public static void main(String[] args) throws IOException { Properties properties = new Properties (); properties.load (new FileInputStream ("file.txt")); Set<String> strings = properties.stringPropertyNames (); FileOutputStream out = new FileOutputStream ("file.txt"); for (String string : strings) { if (string.equals ("lisi")){ properties.setProperty ("lisi","100"); } } properties.store (out,""); out.close (); }SequenceInputStream 表示其他输入流的逻辑串联。 它从输入流的有序集合开始, 并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取, 依次类推,直到到达包含的最后一个输入流的文件末尾为止 a:构造方法 SequenceInputStream(InputStream s1, InputStream s2) 通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,先读取 s1,然后读取 s2), 以提供从此 SequenceInputStream 读取的字节。 b:构造方法 SequenceInputStream(Enumeration<? extends InputStream> e) 通过记住参数来初始化新创建的 SequenceInputStream,该参数必须是生成运行时类型为 InputStream 对象的 Enumeration 型参数。
案例: 将两个mp3文件合成为1个mp3文件
public static void main(String[] args) throws IOException { FileInputStream in1 = new FileInputStream ("G:\\有损音乐\\张学友 - 情书 .mp3"); FileInputStream in2 = new FileInputStream ("G:\\有损音乐\\许巍 - 蓝莲花.mp3"); FileOutputStream out = new FileOutputStream ("C:\\Users\\Dell\\Desktop\\合成音乐.mp3"); SequenceInputStream sequenceInputStream = new SequenceInputStream (in1, in2); byte[] bytes = new byte[1024]; int len =0; while ((len=sequenceInputStream.read (bytes))!=-1){ out.write (bytes,0,len); out.flush (); } sequenceInputStream.close (); out.close (); }