Java IO 输入输出流 实例解释

  1. package IO;
  2. /*
  3.  * Java In Out 输入输出流
  4.  * Date:2014.02.21
  5.  * Author:璀若星辰
  6.  *reference
  7.  * */
  8. import java.io.BufferedInputStream;
  9. import java.io.BufferedReader;
  10. import java.io.BufferedWriter;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.InputStreamReader;
  14. import java.io.OutputStreamWriter;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. /*主要操作的内容:
  18.  * 1.字节流: 图片,     2.字符流  (字符文件 .html .java .txt)
  19.  *
  20.  *
  21.  * */
  22. public class testIO {
  23.     //读 字符输入流
  24.     public static List<String>testCharInput() throws Exception {
  25.         FileInputStream fis = new FileInputStream(“D:/1.txt”);
  26.         try {
  27.             InputStreamReader isr = new InputStreamReader(fis, “gb2312”);
  28.             try{
  29.                 BufferedReader br = new BufferedReader(isr);
  30.                 try{
  31.                     List<String> all = new ArrayList<String>();
  32.                     String temp = br.readLine();
  33.                     while(temp!=null) {
  34.                         System.out.println(“temp=”+temp);
  35.                         all.add(temp);
  36.                         System.out.println(“all= “+all);
  37.                         temp= br.readLine();
  38.                     }
  39.                     return all;
  40.                 }finally{
  41.                     br.close();
  42.                 }
  43.             }finally{
  44.                 isr.close();
  45.             }
  46.         } finally{
  47.             fis.close();
  48.         }
  49.     }
  50.      // 读 字节流
  51.      public static void testByteInputStream() throws Exception {
  52.             FileInputStream fis = new FileInputStream(“D:/1.txt”);
  53.             try {
  54.               BufferedInputStream bis = new BufferedInputStream(fis);
  55.               try {
  56.                 byte[] bs = new byte[1024];  //1024的长度可改
  57.                 while (bis.read(bs, 0, 1024) != -1) {
  58.                   System.out.print(“bs=”+new String(bs));
  59.                 }
  60.               } finally {
  61.                 bis.close();
  62.               }
  63.             } finally {
  64.               fis.close();
  65.             }
  66.           }
  67.      //写 字符流
  68.      public static void testCharOutput(List<String>list) throws Exception{
  69.          FileOutputStream fos = new FileOutputStream(“d:/1.txt”);
  70.          try{
  71.              OutputStreamWriter osw = new OutputStreamWriter(fos,”utf-8″);//文件的编码
  72.              try{
  73.                  BufferedWriter bw = new BufferedWriter(osw);
  74.                  try{
  75.                      for(String line:list){
  76.                          bw.write(line);
  77.                          bw.newLine();//使用newLine()换行  \n
  78.                      }
  79.                      System.out.println(“success”);
  80.                  }finally{
  81.                      bw.close();
  82.                  }
  83.              }finally{
  84.                  osw.close();
  85.              }
  86.          }finally{
  87.              fos.close();
  88.          }
  89.      }
  90.     public static void main(String[] args) throws Exception {
  91.         //testIO.testCharInput();
  92.         //testIO.testByteInputStream();
  93.         ArrayList<String> list = new ArrayList<String>();
  94.         list.add(“Happy”);
  95.         list.add(“Boy”);
  96.         testIO.testCharOutput(list);
  97.         testIO.testByteInputStream();
  98.     }
  99.     //写入内容时不想删除原来的文本,可以先读出来保存下来,然后和新内容一起写入文本,此处不再赘述
  100. }

标签