黑马程序员----JAVASE之IO流操作【字节流】

来源:互联网 发布:公司客户数据保密制度 编辑:程序博客网 时间:2024/04/30 10:21

                                                  -------------- android培训java培训、期待与您交流! ---------------

1.字节流

      在处理字节流使用的基类:InputStream,OutputStream,字节流可以处理任何形式的数据。

用法和Writer和Reader的用法基本相同。

不同的是:

           ① 字符流使用的数组是字符数组。char [] buffer      

           ② 字节流使用的数组是字节数组。byte [] buffer

使用字节流在复制图片:

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("写入关闭失败");}}}}

转换流: 

 特点: 
    1,是字节流和字符流之间的桥梁。 
    2,该流对象中可以对读取到的字节数据进行指定编码表的编码转换。   

什么时候使用呢? 
     1,当字节和字符之间有转换动作时。 
    2,流操作的数据需要进行编码表的指定时。   
具体的对象体现: 
    1,InputStreamReader:字节到字符的桥梁。  
    2,OutputStreamWriter:字符到字节的桥梁。


操作文件的字符流对象是转换流的子类。  
     Reader   
           |--InputStreamReader   
                 |--FileReader   
    Writer   
          |--OutputStreamWriter   
                |--FileWriter

读取mp3文件:
import java.io.*;class  CopyMp3{public static void main(String[] args) throws IOException{long start = System.currentTimeMillis();copy_2();long end = System.currentTimeMillis();System.out.println((end-start)+"毫秒");}public static void copy_2()throws IOException{MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("c:\\9.mp3"));BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("c:\\3.mp3"));int by = 0;while((by=bufis.myRead())!=-1){bufos.write(by);}bufos.close();bufis.myClose();}//通过字节流的缓冲区完成复制。public static void copy_1()throws IOException{BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("c:\\0.mp3"));BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("c:\\1.mp3"));int by = 0;while((by=bufis.read())!=-1){bufos.write(by);}bufos.close();bufis.close();}}

3. File类
 该类的出现是对文件系统的中的文件以及文件夹进行对象的封装。   可以通过对象的思想来操作文件以及文件夹。 
      1,构造函数: File(String filename):将一个字符串路径(相对或者绝对)封装成File对象,该路径是可存在的,也可以是不存在。                  File(String parent,String child);   File(File parent,String child);  
     2,特别的字段:separator:跨平台的目录分隔符。 
             例子:File file = new File("c:"+File.separator+"abc"+File.separator+"a.txt"); 
     
    3,File类常见方法:
        1,创建。
      boolean createNewFile():在指定位置创建文件,如果该文件已经存在,则不创建,返回false。
和输出流不一样,输出流对象一建立创建文件。而且文件已经存在,会覆盖。
             boolean mkdir():创建文件夹。
             boolean mkdirs():创建多级文件夹。
       2,删除。
     boolean delete():删除失败返回false。如果文件正在被使用,则删除不了返回falsel。
     v
oid deleteOnExit();在程序退出时删除指定文件。
      3,判断。
    boolean exists() :文件是否存在.
    isFile():
    isDirectory();
    isHidden();
    isAbsolute();
     4,获取信息。
    getName():
    getPath():
    getParent():
    getAbsolutePath() 
    long lastModified() 
    long length() 

列出指定目录下文件或者文件夹,包含子目录中的内容。
import java.io.*;class FileDemo3 {public static void main(String[] args) {File dir = new File("d:\\testdir");System.out.println(dir.delete());}public static String getLevel(int level){StringBuilder sb = new StringBuilder();sb.append("|--");for(int x=0; x<level; x++){sb.insert(0,"|  ");}return sb.toString();}public static void showDir(File dir,int level){System.out.println(getLevel(level)+dir.getName());level++;File[] files = dir.listFiles();for(int x=0; x<files.length; x++){if(files[x].isDirectory())showDir(files[x],level);elseSystem.out.println(getLevel(level)+files[x]);}}public static int getSum(int n){if(n==1)return 1;return n+getSum(n-1);}public static void toBin(int num){if(num>0){toBin(num/2);System.out.println(num%2);}}public static void method(){method();}}

                                                          -------------- android培训java培训、期待与您交流! ---------------

原创粉丝点击