1、分为字节流和字符流;
字符流中封装的有编码表,对字符的操作很方便;
下面代码为字符流的应用:
//将C盘一个文本文件复制到D盘。 /* 复制的原理: 其实就是将C盘下的文件数据存储到D盘的一个文件中。 步骤: 1,在D盘创建一个文件。用于存储C盘文件中的数据。 2,定义读取流和C盘文件关联。 3,通过不断的读写完成数据存储。 4,关闭资源。 */ import java.io.*; class CopyText { public static void main(String[] args) throws IOException { copy_2(); } public static void copy_2() { FileWriter fw = null; FileReader fr = null; try { fw = new FileWriter("SystemDemo_copy.txt"); fr = new FileReader("SystemDemo.java"); char[] buf = new char[1024]; int len = 0; while((len=fr.read(buf))!=-1) { fw.write(buf,0,len); } } catch (IOException e) { throw new RuntimeException("读写失败"); } finally { if(fr!=null) try { fr.close(); } catch (IOException e) { } if(fw!=null) try { fw.close(); } catch (IOException e) { } } } //从C盘读一个字符,就往D盘写一个字符。 public static void copy_1()throws IOException { //创建目的地。 FileWriter fw = new FileWriter("RuntimeDemo_copy.txt"); //与已有文件关联。 FileReader fr = new FileReader("RuntimeDemo.java"); int ch = 0; while((ch=fr.read())!=-1) { fw.write(ch); } fw.close(); fr.close(); } }
2、下面代码为字节流实例:
/* 复制一个图片 思路: 1,用字节读取流对象和图片关联。 2,用字节写入流对象创建一个图片文件。用于存储获取到的图片数据。 3,通过循环读写,完成数据的存储。 4,关闭资源。 */ import java.io.*; class CopyPic { public static void main(String[] args) { FileOutputStream fos = null; FileInputStream fis = null; try { fos = new FileOutputStream("c:\\2.bmp"); fis = new FileInputStream("c:\\1.bmp"); byte[] buf = new byte[1024]; int len = 0; while((len=fis.read(buf))!=-1) { fos.write(buf,0,len); } } catch (IOException e) { throw new RuntimeException("复制文件失败"); } finally { try { if(fis!=null) fis.close(); } catch (IOException e) { throw new RuntimeException("读取关闭失败"); } try { if(fos!=null) fos.close(); } catch (IOException e) { throw new RuntimeException("写入关闭失败"); } } } }
3、字节流和字符流之间的转换
字节流转为字符流:
import java.io.*; class TransStreamDemo { public static void main(String[] args) throws IOException { //获取键盘录入对象。 //InputStream in = System.in; //将字节流对象转成字符流对象,使用转换流。InputStreamReader //InputStreamReader isr = new InputStreamReader(in); //为了提高效率,将字符串进行缓冲区技术高效操作。使用BufferedReader //BufferedReader bufr = new BufferedReader(isr); //键盘的最常见写法。 BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); // OutputStream out = System.out; // OutputStreamWriter osw = new OutputStreamWriter(out); // BufferedWriter bufw = new BufferedWriter(osw); BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out)); String line = null; while((line=bufr.readLine())!=null) { if("over".equals(line)) break; bufw.write(line.toUpperCase()); bufw.newLine(); bufw.flush(); } bufr.close(); } }
3、File类:
由于文件时数据操作的基本单位,所以将File封装为一个类;
File file=new File("sd.txt")即可;
在流中,使用流的方法创建的文件其效果与这个一样;
4、一种特殊文件,Properties文件是以键值对存储数据的;
它是HashTable的子类,具备Map的特点,是集合中和IO相结合的一个类;
import java.io.*; import java.util.*; class PropertiesDemo { public static void main(String[] args) throws IOException { //method_1(); loadDemo(); } public static void loadDemo()throws IOException { Properties prop = new Properties(); FileInputStream fis = new FileInputStream("info.txt"); //将流中的数据加载进集合。 prop.load(fis); prop.setProperty("wangwu","39"); FileOutputStream fos = new FileOutputStream("info.txt"); prop.store(fos,"haha"); // System.out.println(prop); prop.list(System.out); fos.close(); fis.close(); } //演示,如何将流中的数据存储到集合中。 //想要将info.txt中键值数据存到集合中进行操作。 /* 1,用一个流和info.txt文件关联。 2,读取一行数据,将该行数据用"="进行切割。 3,等号左边作为键,右边作为值。存入到Properties集合中即可。 */ public static void method_1()throws IOException { BufferedReader bufr = new BufferedReader(new FileReader("info.txt")); String line = null; Properties prop = new Properties(); while((line=bufr.readLine())!=null) { String[] arr = line.split("="); ///System.out.println(arr[0]+"...."+arr[1]); prop.setProperty(arr[0],arr[1]); } bufr.close(); System.out.println(prop); } // 设置和获取元素。 public static void setAndGet() { Properties prop = new Properties(); prop.setProperty("zhangsan","30"); prop.setProperty("lisi","39"); // System.out.println(prop); String value = prop.getProperty("lisi"); //System.out.println(value); prop.setProperty("lisi",89+""); Set<String> names = prop.stringPropertyNames(); for(String s : names) { System.out.println(s+":"+prop.getProperty(s)); } } }
5、其他常见IO流对象:ByteArrayStream、DataStream、RandomAccessFile、
PipedStream、PrintWrite以及IO的合并流;
其中RandomAccessFile直接继承于Object;
6、
序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操作时所引发的问题。序列化的实现:将需要被序列化的类实现Serializable接口,该接口没有需要实现的方法,implements Serializable只是为了标注该对象是可被序列化的,然后使用一个输出流(如:FileOutputStream)来构造一个ObjectOutputStream(对象流)对象,接着,使用ObjectOutputStream对象的writeObject(Object obj)方法就可以将参数为obj的对象写出(即保存其状态),要恢复的话则用输入流。
转载于:https://blog.51cto.com/seasky09/1251248