zoukankan      html  css  js  c++  java
  • IO流[字符和字节的读取]

    1、IO流用来处理设备之间的数据传输。

    2、Java对数据的操作是通过流的方式。

    3、Java用于操作流的对象都封装在IO包中。

    4、流按操作数据分为两种:字节流与字符流。

    5、流按流向分为:输入流,输出流。

    字符流的抽象基类

     Reader,Writer

    字节流的抽象基类

     InputStrea,OutputStream

    注意:由这四个类派生出来的子类名称都是以其父类名作为子类明的后缀。

    package TestDemo;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Reader;
    import java.io.Writer;
    
    /**
     * 字符的 输入和输出
     * @author Mine
     *
     */
    public class RWtest {
            public  static void main(String[] args){
                StringBuffer buffer=new StringBuffer();//缓冲区
                  /*输入流*/
                Reader reader=null;
                try {
                    //1.打开流
                    reader=new FileReader("D:\test\one.txt");
                    //2.读
                    char [] ch=new char[128];//字符缓冲区
                    int len;
                    do{
                            len=reader.read(ch);
                            if(len==-1){
                                break;
                            }else{
                                buffer.append(new String(ch,0,len));//否则就拼接
                            }
                    }while(len!=-1);
                    System.out.println(buffer.toString());
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    //3.关闭流
                    if(reader!=null){
                        try {
                            reader.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                
                /*输出流*/
                Writer writer=null;
                File file=new File("D:\test\Dmeo\three.txt");
                if(!file.getParentFile().exists()){//测试此抽象路径名表示的文件或目录是否存在。
                    file.getParentFile().mkdirs();//如果没有就创建所有必需但不存在的父目录。
                }
                
                try {
                    //1.打开流
                    writer=new FileWriter(file);
                    //2.写
                    writer.write(buffer.toString());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    if(writer!=null){
                        try {
                            writer.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
    }

    字节流基类 

    InputStream

    OutputStream

    package shiXun;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class IOfileDemo {
            public static void main(String[] args){
                FileOutputStream out=null;
                FileInputStream in=null;
                try {
                    //打开输出流
                    out=new FileOutputStream(new File("D:\mycopyfile\test.txt"),true);
                    in=new FileInputStream("D:\mycopyfile\test.txt");
                    //写入
                    out.write("HelloWorld".getBytes());
                    //读取
                    byte [] b= new byte[1024];
                    int len;
                    while((len=in.read(b))!=-1){
                        //输出
                            System.out.println(new String(b,0,len));
                        }
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    if(out!=null){
                        try {
                            out.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    if(in!=null){
                        try {
                            in.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                
            }
    }
  • 相关阅读:
    Java8实战之Stream流式操作
    类、对象、引用
    java对象的四种引用:强引用、软引用、弱引用和虚引用
    《将博客搬至CSDN》
    **JAVA参数传递方式 (按值传递与引用传递区别)
    JavaWeb--ServletContext
    HttpSessionListener的用法
    SpringMVC优雅的获取HttpSevletRequest及HttpServletResponse简录
    setnx
    JVM命令工具开发
  • 原文地址:https://www.cnblogs.com/Melony/p/4455539.html
Copyright © 2011-2022 走看看