黑马程序员———IO(下)

来源:互联网 发布:网络摄像头安装视频 编辑:程序博客网 时间:2024/06/07 00:43
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


 

package cn.itcast.IO3;
import java.io.*;
/*
需求:
     通过字节流的缓冲区完成mp3文件的复制。
思路:
     可以用模板方法设计模式
在定义功能时,功能的一部分是确定的,一部分是不确定的,那么就将不确定的那部分暴露出去,
     让该类的子类去完成。如果要让功能唯一,则用final修饰。

*/
abstract class CopyMusicb
{
     public final void copyMp3()
     {
          long start = System.currentTimeMillis();
          runCode();
          long end = System.currentTimeMillis();
          System.out.println("time: "+(end-start)+"ms");
     }

     abstract void runCode();
         
}

class SubMusic extends CopyMusic
{

     public void runCode()
     {
          BufferedInputStream bfis = null;
          BufferedOutputStream bfos = null;
          try {
               bfis = new BufferedInputStream(new FileInputStream("music.mp3"));
               bfos = new BufferedOutputStream(new FileOutputStream("music_copy.mp3"));
              
               int ch = 0;
               while((ch=bfis.read())!=-1)
               {
                    bfos.write(ch);
               }
          } catch (IOException e) {
               throw new RuntimeException("复制失败");
          } finally {
               try {
                    if(bfis!=null)
                         bfis.close();
               } catch (IOException e) {
                    throw new RuntimeException("读取关闭失败");
               }
               try {
                    if(bfos!=null)
                         bfos.close();
               } catch (IOException e) {
                    throw new RuntimeException("写入关闭失败");
               }
          }
     }
}
public class CopyMp3 {

     public static void main(String[] args) {
          SubMusic sm = new SubMusic();
          sm.copyMp3();
     }
    
    
}
---------------------------------------------------


package cn.itcast.IO3;
import java.io.*;
/*
字符流:
     FileReader
     FileWriter
     BufferedReader
     BufferedWriter
     LineNumberReader
     PrintWriter
字节流:
      FileInputStream
      FileOutStream
      BufferedInputStream
      BufferedOutStream
      PrintStream
*/
public class FileStream {
     public static void main(String[] args) throws IOException
     {
          readFile_3();
     }
     //存入字节数组中,并且明确了文件的长度大小,fis.available(),定义了一个刚刚好的缓冲区
     //当文件相对较小时,可以使用这个,当文件较大时,建议使用readFile_2()的方式.
     public static void readFile_3() throws IOException
     {
          FileInputStream fis = new FileInputStream("fos.txt");
          int ch = 0;
          byte[] buf = new byte[fis.available()];
          while((ch=fis.read(buf))!=-1)
          {
               System.out.println(new String(buf));
          }
     }
     //存入字节数组中。
     public static void readFile_2() throws IOException
     {
          FileInputStream fis = new FileInputStream("fos.txt");
          int ch = 0;
          byte[] buf = new byte[1024];
          while((ch=fis.read(buf))!=-1)
          {
               System.out.println(new String(buf,0,ch));
          }
     }
     //一个一个字节读取,返回作为一个整数读取的字符,需要转型
     public static void readFile() throws IOException
     {
          FileInputStream fis = new FileInputStream("fos.txt");
          int ch = 0;
          while((ch=fis.read())!=-1)
          {
               System.out.println((char)ch);
          }
     }
    
     public static void writeFile() throws IOException
     {
          FileOutputStream fos = new FileOutputStream("fos.txt");
          fos.write("abcdefg".getBytes());//不用刷新, 因为字节流的写入操作就是一个一个字节往目的地传输
     }
}
------------------------------------------------

//键盘录入最常见写法:
          BufferedReader bufr =
                    new BufferedReader(new InputStreamReader(System.in));

          BufferedWriter bufw =
                    new BufferedWriter(new OutputStreamWriter(System.out));

         
-------------------------------------------------
流操作的基本规律:
     流的对象很多,但是要用哪个呢?
通过三个明确来完成。
     1.明确源和目的。
          源:输入流:InputStream       Reader
          目的:输出流:OutputStream     Writer
         
     2.操作的数据是否为纯文本。
          是:使用字符流
          否:使用字节流
         
     3.当体系明确后,再明确要使用哪个具体的对象。
          通过设备来进行区分:
          源设备:    内存     硬盘          键盘
          目的设备:内存     硬盘          控制台


例子
     将键盘录入的数据存到文件中。
          源:InputStream     Reader
         
          是否为纯文本?是,使用字符流Reader
              
          设备:键盘,则对应的是System.in,那么应该使用字节流InputStream。
               但是因为用字符流来操作字符串更为方便快捷,可以使用转换流InputStreamReader。
              
               InputStream in = new InputStream(System.in);
               InputStreamReader isr = new InputStreamReader(in);
              
          需要高效吗?需要。
               BufferedReader bufr = new BufferedReader(isr);
              
          目的:OutputStream     Writer
         
          是否为纯文本?是,使用字符流。
         
          设备:硬盘。
               则使用FileWriter对象。
         
               FileWriter fw = new FileWriter("c.txt");
         
          需要高效吗?需要。
               BufferedWriter bufw = new BufferedWriter(fw);
              
              
         

扩展
     想把录入的数据按照指定的编码表,将数据存到文件中。
     例如:用UTF-8
    
     OutputStreamWriter(OutputStream out,String charsetName)
     创建使用指定字符集的输出转换流。
    
     源:InputStream     Reader
         
          是否为纯文本?是,使用字符流Reader
              
          设备:键盘,则对应的是System.in,那么应该使用字节流InputStream。
               但是因为用字符流来操作字符串更为方便快捷,可以使用转换流InputStreamReader。
              
               InputStream in = new InputStream(System.in);
               InputStreamReader isr = new InputStreamReader(in);
              
          需要高效吗?需要。
               BufferedReader bufr = new BufferedReader(isr);
         
     目的:OutputStream     Writer
         
          是否为纯文本?是,使用字符流Writer。
         
          设备:硬盘。
               则使用FileWriter对象。但是FileWriter是使用默认的编码表GBK。
               但是存储时,需要加入指定编码表UTF-8。而指定编码表只有转换流可以指定。
               所以要使用的对象是OutputStreamWriter。而该转换流对象要接收一个字节输出流,并且是操作文件的。
              
               FileOutputStream fos = new FileOutputStream("c.txt");
               OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
          需要高效吗?需要。
               BufferedWriter bufw = new BufferedWriter(osw);
              
    
     记住:转换流什么时候使用呢?
          字符和字节之间的桥梁,通常,涉及到转换字符编码转换时,需要用到转换流。
         
    
另外小知识:
     
改变标准输入输出:
          System.setIn(new FileInputStream("CopyTextBuBuf.txt"));
          System.setOut(new PrintStream("zz.txt"));


------------------------------------------------

package cn.itcast.IO3;

import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;


/*
异常日志:

printStackTrace();//相当于调用下面这个方法,传入System.in
printStackTrace(PrintStream s);//将此throwable及其追踪输出到指定的输出流。

开发中可以使用log4j包:
     log4j工具包专门用于日志信息的建立。

*/
public class ExceptionInfo {

     public static void main(String[] args) throws IOException {
          try {
               int[] arr = new int[3];
               System.out.println(arr[3]);;
          } catch (Exception e) {
               try {
                    Date d = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String s = sdf.format(d);
                    PrintStream ps = new PrintStream("exception.log");
                    ps.println(s);
                    System.setOut(ps);
                   
               } catch (IOException ex)
               {
                    throw new RuntimeException("日志文件创建失败");
               }
               e.printStackTrace(System.out);
              
              
          }
     }

}
---------------------------------------------

集合和流的结合:Properties。
     属于Map集合的,并且可以结合流使用。

list(PrintStream out)
list(PrintWriter out)
     将属性列表输出到指定的输出流。


 

 

 

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


 

0 0
原创粉丝点击