IO流(流?)
1.0 概念和分类
2.0 字节输出流
(1)输入一个字节
import java.io.FileOutputStream; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileOutputStream fos=new FileOutputStream("Demo01\\a.txt"); fos.write(97); fos.close(); } }
(2)多个字节的输入
import java.io.FileOutputStream; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileOutputStream fos=new FileOutputStream("Demo01\\b.txt"); byte bytes[]={65,66,67,68};//ABCD //第一种 fos.write(bytes); //第二种 fos.write(bytes,1,3); //第三种 String s="你好,很高兴认识你"; byte[] bytes1 = s.getBytes(); System.out.println(bytes1);//[B@1c53fd30 fos.write(bytes1); fos.close(); } }
字节输出流的续写和换行
import java.io.FileOutputStream; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileOutputStream fos=new FileOutputStream("Demo01\\c.txt",true); fos.write("我说很高兴我能遇见你".getBytes()); for (int i = 0; i <5 ; i++) { fos.write("我也是".getBytes()); fos.write("\r\n".getBytes()); } fos.close(); } }
3.字节输入流
单个字符读取
import java.io.FileInputStream; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("Demo01\\b.txt"); int len=0; while((len=fis.read())!=-1) { System.out.print(len+" "); } fis.close(); } }
打印结果:
97 98 99
多个字符的读取
import java.io.FileInputStream; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("Demo01\\b.txt"); byte []bytes=new byte[1024]; int len=0; while((len=fis.read(bytes))!=-1) { System.out.println(len); String s = new String(bytes, 0, len); System.out.println(s); } } }
打印结果:
8
ABCDEabc
4.字符输入流
import java.io.FileReader; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileReader fr=new FileReader("Demo01\\b.txt"); int len=0; /*读取一个字符 while ((len=fr.read())!=-1) { System.out.print((char)len); }*/ /*读取多个字符 char chars[]=new char[1024]; while((len=fr.read(chars))!=-1) { System.out.println(new String(chars,0,len)); }*/ fr.close(); } }
5.字符输出流
import java.io.FileWriter; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileWriter fw=new FileWriter("Demo01\\b.txt"); fw.write(97); fw.flush(); //fw.close(); } }
flush()和close()的区别
字节输出流的续写和换行
6.try...catch()处理流中的异常
import java.io.FileWriter; import java.io.IOException; public class Main{ public static void main(String[] args) { FileWriter fs=null; try{ fs=new FileWriter("Dem01\\b.txt"); for(int i=0;i<5;i++) { fs.write("你好"+i+"\r\n"); } } catch(IOException e) { System.out.println(e); } finally { if(fs!=null) { try { fs.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
JDK 7对其优化
6.属性集
import java.util.Enumeration; import java.util.Properties; import java.util.Set; public class Main{ public static void main(String[] args) { Properties prop=new Properties(); prop.setProperty("Jin","27"); prop.setProperty("V","25"); prop.setProperty("JK","25"); Setset = prop.stringPropertyNames(); for (String s : set) { System.out.println(prop.get(s)); } } }
store方法
import java.io.FileWriter; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; import java.util.Set; public class Main{ public static void main(String[] args) throws IOException { Properties prop=new Properties(); prop.setProperty("Jin","27"); prop.setProperty("V","25"); prop.setProperty("JK","23"); prop.store(new FileWriter("Demo01\\b.txt"),"practice"); } }
load方法
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Properties; import java.util.Set; public class Main{ public static void main(String[] args) throws IOException { Properties prop=new Properties(); prop.load(new FileReader("Demo01\\b.txt")); Setset = prop.stringPropertyNames(); for (String s : set) { System.out.println(s+"="+prop.getProperty(s)); } } }
缓冲流
1.0 字节缓冲输出流
2.字节缓冲输入流
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("Demo01\\c.txt"); BufferedInputStream input=new BufferedInputStream(fis); int len=0; byte []bytes=new byte[1024]; while((len=input.read(bytes))!=-1) { System.out.println(new String(bytes,0,len)); } input.close(); } }
3.字符缓冲输出流
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { BufferedWriter bw=new BufferedWriter(new FileWriter("Demo01\\c.txt")); for(int i=0;i<5;i++) { bw.write("你好,我叫赤木晴子"); bw.newLine(); } bw.flush(); bw.close(); } }
打印结果:
4.字符缓冲输入流
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new FileReader("Demo01\\c.txt")); String s; while((s=br.readLine())!=null) { System.out.println(s); } br.close(); } }
转换流
1.0 OutputStreamWrite
2.InputStreamReader
转换流的练习
将GBK文件转化为UTF-8文件
import java.io.*; public class Main{ public static void main(String[] args) throws IOException { InputStreamReader isr=new InputStreamReader(new FileInputStream("Demo01\\我是一个GBK文件.txt"),"GBK"); OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("Demo01\\转化后的UTF-8文件"),"UTF-8"); int len=0; while((len=isr.read())!=-1) { osw.write(len); } isr.close(); osw.close(); } }
序列化与非序列化
序列化
ObjectOutputStream
import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Main{ public static void main(String[] args) throws IOException { ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("Demo01\\a.txt")); oos.writeObject(new Person("百里玄策",18)); oos.close(); } }
ObjectInputStream
import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class Main{ public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream ois=new ObjectInputStream(new FileInputStream("Demo01\\a.txt")); Object o = ois.readObject(); ois.close(); System.out.println(o); } }
transient关键字修饰的成员变量不能被序列化(瞬态关键字)
import java.io.Serializable; public class Person implements Serializable { private String name; private transient int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } public void setAge(int age) { this.age = age; } public Person() { } }
import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class Test { public static void main(String[] args) throws IOException,ClassNotFoundException { ObjectInputStream ois=new ObjectInputStream(new FileInputStream("Demo01\\a.txt")); Object o = ois.readObject(); ois.close(); System.out.println(o); } }
打印结果:
Person{name='百里玄策', age=0}
打印流
import java.io.FileNotFoundException; import java.io.PrintStream; public class Main{ public static void main(String[] args) throws FileNotFoundException { System.out.println("我在控制台输出"); PrintStream ps=new PrintStream("输出流"); System.setOut(ps); System.out.println("我在输出流中输出"); } }