Java IO流

来源:互联网 发布:dotnet可以用java吗 编辑:程序博客网 时间:2024/06/01 09:16

                                                          JAVA IO流

一.File类

        在整个IO包中,唯一与文件本身有关的类就是File类

       1.1 File类的常用方法和常量

            pathSeparator  //表示路径的分隔符(windows是:“;”)


            separator   //表示路径的分隔符(widows是:“\”)


            public File(String pathname) //构造   创建File类对象,传入完整路径


            createNewFile() //创建新文件


            delete()   //删除文件


            isDrectory() //判断给定的路径是否是一个目录


            length()    //返回文件的大小


            list()   //列出指定目录的全部内容,只是列出了名称


            listFiles() //列出指定目录的全部内容,会列出路径


            mkdir()    //创建一个目录


            renameTo(File dest)    //为已有的文件重新命名


   二. 字节流和字符流

        2.1字节流
           2.1.1 InputStream

                字节输入流

          

public class Demo2 {public static void main(String[] args) throws IOException {/*什么叫做IO?什么叫做流? * 按流向分分为:输入流和输出流  * 1.流向分:输出流    输入流 * 基类: * InputStream Reader(都是抽象类) * OutputStream Writer * 他们的实现类都是以他们的名字作为后缀 * 2.数据单位来分:字节流  字符流 * InputStream OutputStream 字节流 *  Reader   Writer  字符流 * 3.功能来分:节点流   处理流  * 节点流:直接操作数据 *  处理流:经过节点流包装进行数据操作 * *///字节流                         读文件File file=new File("E:"+File.separator+"rename.txt");try {FileInputStream fileInputStream=new FileInputStream(file);try {//批量读取(自定义缓冲区读取)byte[] by=new byte[50];int num=0; //定义真实读取数据数量//num=fileInputStream.read(by);//System.out.println(num);//String str=new String(by,0,num);//System.out.println(str);String str=null;/*while((num=fileInputStream.read(by))!=-1){str=new String(by,0,num);}*/while((num=fileInputStream.read(by,0,num))!=-1){//str=new String(num);}System.out.println(str);} catch (Exception e) {e.printStackTrace();}finally {fileInputStream.close();}} catch (FileNotFoundException e) {e.printStackTrace();}}}


        2.2.2 OutputStream

                字节输出流

            

import java.io.File;import java.io.FileOutputStream;import java.io.IOException;public class Demo3 {public static void main(String[] args) throws IOException {/* * 字节流的写操作 *  *  */File file=new File("E:\\aa.txt");//if(!file.exists()){//if(file.createNewFile()){//System.out.println("创建成功");//}else{//System.out.println("文件已经存在");//}FileOutputStream fo=new FileOutputStream(file,true);//fo.write('h');//fo.write('e');//fo.write('l');//fo.write('l');//fo.write('o');//System.out.println("写入成功");  String str="how do you do";  byte [] by=str.getBytes();   fo.write(by);   fo.flush();   System.out.println("写入成功");   fo.close();}}



       2.2 字符流
          2.2.1 Reader

                字符输入流

      

import java.io.File;import java.io.FileReader;import java.io.IOException;import java.io.Reader;public class Demo2 {public static void main(String[] args) throws IOException {File f=new File("D:"+File.separator+"char.txt");Reader reader=null;reader=new FileReader(f);int len=0;char [] c=new char[1024];int temp=0;while((temp=reader.read())!=-1){c[len]=(char)temp;len++;}reader.close();System.out.println("内容为:"+new String(c,1,len--));System.out.println(len);}}


         2.2.2 Writer

             字符输出流

              

import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import com.c.IoUtils;public class CharDemo {public static void main(String[] args) throws IOException {/*字符流 *  *  *  */File f=new File("D:"+File.separator+"char.txt");WriteContent(f);ReadContent(f);}public static void WriteContent(File f) throws IOException{IoUtils.checkFile(f);FileWriter fw=null;try {fw=new FileWriter(f);String str="hello world";String str2="深圳";fw.write(str);fw.write(str2);fw.flush();} catch (Exception e) {}finally {fw.close();}}public static void ReadContent(File f) throws IOException{IoUtils.checkFile(f);FileReader fr=null;try {fr=new FileReader(f);int len=0;//char[] by =new char[1024];while((len=fr.read())!=-1){System.out.print(len);}} catch (Exception e) {}finally {fr.close();}}}


    三.字节缓冲流和字符缓冲流

        3.3字节缓冲流

           3.3.1 BufferedInputStream

                    缓冲字节输入流

         

import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class ByteBufferDemo {public static void main(String[] args) throws IOException {/* *  * 字节缓冲流:提高读取数据的速率,安全  * BufferedInputStream  输入字节缓冲流 * BufferedOutStream    输出字节缓冲流 *///BufferedInputStream bi=new BufferedInputStream(in);File f =new File("D:"+File.separator+"out.txt");IoUtils.checkFile(f);readContent(f);FileOutputStream fou=null ;BufferedOutputStream bo=null;try {fou=new FileOutputStream(f);bo=new BufferedOutputStream(fou);//写入内容String str="hello world";try {bo.write(str.getBytes());bo.flush();System.out.println("写入成功");} catch (Exception e) {// TODO: handle exception}} catch (FileNotFoundException e) {e.printStackTrace();}finally {try {bo.close();   //关闭缓冲流即可} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}//缓冲流读内容public static void readContent(File f) throws IOException{IoUtils.checkFile(f);BufferedInputStream bi=null;try {bi=new BufferedInputStream(new FileInputStream(f));//读操作int len=0;byte[] by=new byte[1024];while((len=bi.read(by))!=-1){System.out.println(new String(by,0,len));}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{bi.close();}}}


           3.3.2 BufferedOutStream

                 缓冲字节输出流


        3.4字符缓冲流
            3.4.1  BufferedReader

                     缓冲字符输入流

        

public class BufferReadDemo {public static void main(String[] args) {/* *字符流的缓冲流应用;  * BufferedReader  :缓冲输入流 *  * BufferedWriter  :缓冲输出流 *  *///读 File file=new File("E:"+File.separator+"n.txt");IoUtils.checkFile(file);//BufferedWriter bw=new BufferedWriter(arg0);try {BufferReadContent(file);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void BufferReadContent(File file) throws IOException{FileReader fr=null;BufferedReader br=null;try {fr=new FileReader(file);br=new BufferedReader(fr);int len=0;char[] ch=new char[1024];String str=null;while((len=br.read(ch))!=-1){str=new String(ch,0,len);System.out.println(str);}} catch (Exception e) {}finally {br.close();}}}


         3.4.2  BufferedWriter
                 缓冲字符输出流
        
import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import com.c.IoUtils;public class CharBufferDemo1 {public static void main(String[] args) {/* * 字符缓冲流的写 */File f =new File("D:"+File.separator+"char02.txt");IoUtils.checkFile(f);BufferedWriter bw=null;try {bw=new BufferedWriter(new FileWriter(f));//写bw.write("中国");bw.newLine();   //换行bw.write("123");bw.write("hello");bw.flush();} catch (IOException e) {e.printStackTrace();}finally{try {bw.close();} catch (Exception e2) {}}}}