IO分两种流

字节流 InputStream OutputStream

字符流 Reader  Writer

他们都是抽象类

具体实现
字节流 FileInputStream  FileOutputStream
字符流 FileReader    FileWriter


字节流转换成字符流可以用 InputSteamReader  OutputStreamWriter

转换成BufferdReader  BufferedWriter 他们具有缓冲区

例如:读取文件 从字节流输入到字符流输入
定义一个字节流:
Java代码
  1. FileInputStream fileInputStream = new FileInputStream("d:/text.txt");  // 定义一个指向D:/TEXT.TXT 的字节流   
  2.   
  3. InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);   
  4. //字节流转换成InputStreamReader   
  5.   
  6. BufferedReader bufferedReader = new BufferedReader(inputSteamReader);   
  7. //InputStreamReader 转换成带缓存的bufferedReader  


可以把读出来的内容赋值给字符
Java代码
  1.   
  2. String ss = new String();   
  3. String s;   
  4. while((s = bufferedReader.readLine())!=null){   
  5.     ss += s;   
  6. }  

例如:写入文件  从字节流输出到字符流输出
Java代码
  1.   
  2. FileOutputStream fileOutputStream = new FileOutputStream("d:/text.txt"); //定义一个   
  3.   
  4. 指向D:/TEXT.TXT文件    
  5.   
  6. OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);   
  7.   
  8. BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);   
  9.   
  10. bufferedWriter.write(s);   
  11.   
  12. bufferedWriter.close();   
  13. outputStreamWriter.close();   
  14. fileOutputStream.close();  
范例代码如下:
package com.ljj.moshi.factory3;
import java.util.*;   
import java.io.*;   
interface Fruit{   
        public void grow();   
        public void eat();   
}   
class Apple implements Fruit{   
        public void grow(){   
                System.out.println("苹果在生长...");   
        }   
        public void eat(){   
                System.out.println("吃苹果...");   
        }   
}   
class Orange implements Fruit{   
        public void grow(){   
                System.out.println("橘子在生长...");   
        }   
        public void eat(){   
                System.out.println("吃橘子...");   
        }   
}   
class Banana implements Fruit{   
        public void grow(){   
                System.out.println("香蕉在生长...");   
        }   
        public void eat(){   
                System.out.println("吃香蕉...");   
        }   
}   
class Factory{   
        public static Fruit getFruit(String className){   
                Fruit f = null;   
                try{   
                        f = (Fruit)Class.forName(className).newInstance();   
                }catch (Exception e){}   
                return f;   
        }   
}   
class PropertyOperate{   
        private Properties pro = null;   
        public PropertyOperate(){   
                this.pro = new Properties();   
                this.load();   
        }   
        //设置一个返回方法   
        public Properties getPro(){   
                return this.pro;   
        }   
        //从文件中读出属性,如果文件不存在,则创建一个默认的   
        private void save(){   
                pro.setProperty("a","Apple");   
                pro.setProperty("o","Orange");   
                //保存在文件之中   
                try{   
                        pro.storeToXML(new FileOutputStream(new File("f:\\fruit.xml")),"FRUIT FACTORY  TEST LJJ");   
                }catch (Exception e){}   
        }   
        private void load(){   
                File f = new File("f:\\fruit.xml");   
                if(f.exists()){   
                        //文件存在则可以读取   
                        try{   
                                pro.loadFromXML(new FileInputStream(f));   
                        }catch (Exception e){}   
                }else{   
                        //进行创建   
                        this.save();   
                }   
        }   
}   
class InputData{   
    private BufferedReader buf = null;   
    public InputData(){   
            this.buf = new BufferedReader(new InputStreamReader(System.in));   
    }   
    public String getString(){   
            String str =null;  
            try{   
                    str = this.buf.readLine();   
            }catch(Exception e){}   
            return str;   
    }   
}   

public class Demo09{   
        public static void main(String args[]){   
                Properties p = new PropertyOperate().getPro();   
                System.out.println(p);   
                System.out.print("请选择所需要的类型:");   
                String str = new InputData().getString();   
                //进一步扩展,现在可以由用户自己输入要使用的类型   
                Fruit f = Factory.getFruit(p.getProperty(str));   
                f.grow();   
        }   
}