Java输入与输出

Java的5个非常重要的输入输出类:InputStream、OutputStream、Reader、Writer、File

一、输入输出类库

    流

        流,计算机的输入、输出操作中流动的数据序列。

        Java中的流,位流(字节流)和字符流。
        位流:InputStream、OutputStream
        字符流:Reader、Writer

    输入输出流类

        1、InputStream

            (1)、read()方法
                public int read()
                public int read(byte b[])

            (2)、定位指针的方法
                public long skin(long n)
                public void mark()
                public void reset()

            (3)、close()方法
                public void close()

            InputStream类中部分常用子类的继承关系
            InputStream
                FileInputStream
                FilterInputstream
                    DataInputStream
                    BufferedInputStream

        2、OutputStream

            (1)、write()方法
                public void write(int b)
                public void write(byte b[])

            (2)、flush()方法
                public void flush()

            (3)、close()方法
                public void close()

            OutputStream类的部分常用子类的继承关系
            OutputStream
                FileOutputStream
                FilterOutputStream
                    PrintStream
                    DataOutputStream
                    BufferedOutputStream

        3、Reader

            (1)、read()方法
                public int read()
                public int read(char array[])

            (2)、定位指针的方法
                public long skin(long n)
                public void mark()
                public void reset()

            (3)、close()方法
                public void close()

            Reader类中部分常用子类的继承关系
            Reader
                BufferedReader
                    InputStreamReader
                        FileReader

        4、Writer

            (1)、write()方法
                public void write(int c)
                public void write(char array[])

            (2)、flush()方法
                public void flush()

            (3)、close()方法
                public void close()

            Writer类的部分常用子类的继承关系
            Writer
                PrintWriter
                BufferedWriter
                OutputStreamWriter
                    FileWriter

二、标准输入输出

    System.out属于PrintStream类对象
    System.in属于InputStream类对象
    System.err属于PrintStream类对象

    标准输入
        System.in.read()
        new BufferedReader(new InputStreamReader(System.in)).readLine()

    标准输出
        System.out.print()
        System.out.println()

    标准错误
        System.err

三、文件操作

    建立File对象

        1、File(String path)
        2、File(String path, String name)
        3、File(File dir, String name)

    File对象的属性和操作

        1、获得文件或目录名称与路径
            public String getNmae()
            public String getPath()

        2、判断文件或目录是否存在
            public boolean exists()

        3、获取文件长度
            public long length()

        4、获取文件读取属性
            public boolean canRead()
            public boolean canWrite()

        5、比较文件或目录
            public boolean equals(File file)

        6、判断是文件还是目录
            public boolean isFile()
            public boolean isDirectiry()

        7、重命名文件
            public boolean renameTo(File file)

        8、删除文件
            public boolean renameTo(File file)

    实例

        1、以字节流方式读/写文件

            (1)、以字节流方式写入文件
                File file = new File(“C:\\txt.txt”);
                FileOutputStream fout = new FileOutputStream(file);
                char ch = (char)System.in.read();
                fout.write(ch);
                fout.close();

            (2)、以字节流方式读磁盘文件
                File file = new File(“C:\\txt.txt”);
                FileInputStream fin = new FileInputStream(file);
                int ch = fin.read();
                System.out.print((char)ch);
                fin.close();

        2、磁盘文件读写各类数据

            (1)、向磁盘文件写入各类数据
                InputStreamReader iin = new InputStreamReader(System.in);
                BufferedReader bin = new Buffered(iin);
                int I = Integer.parseInt(bin.readLine());
                float f = Float.parseFloat(bin.readLine());
                boolean b = new Boolean(bin.readLine()).booleanValue();
                File file1 = new File(“C:\\txt1.txt”);
                FileOutputStream fout = new FileOutputStream(file1);
                DataOutputStream dout = new DataOutputStream(fout);
                dout.writeInt(i);
                dout.writeFloat(f);
                dout.writeBoolean(b);
                dout..close();

            (2)、从磁盘文件读取各类数据
                File file1 = new File(“C:\\txt1.txt”);
                FileInputStream fin = new FileInputStream(file1);
                DataInputStream din = new DataInputStream(fin);
                int i = din.readInt();
                float f = din.readFloat();
                boolean b = din.readBoolean();
                din.close();
                File file2 = new File(“C:\\txt2.txt”);
                FileOutputStream fout = new FileOutputStream(file2);
                DataOutputStream dout = new DataOutputStream(fout);
                dout.writeInt(i);
                dout.writeFloat(f);
                dout.writeBoolean(b);
                dout.close();
                System.out.println(“整数:”+i);
                System.out.println(“浮点数:”+f);
                System.out.println(“布尔量:”+b);

        3、以字符流方式读写文件

            (1)、以字符流方式写入文件
                InputStreamReader iin = new InputStreamReader(“System.in);
                BufferedReader br = new BufferedReader(iin);
                String s = br.readLine();
                FileWriter fw1 = new FileWriter(“C:\\txt1.txt”);
                BufferedWriter bw = new BufferedWriter(fw1);
                bw.write(s);
                bw.newLine();
                bw.close();

            (2)、以字符流方式读取文件
                FileReader fr = new FileReader(“C:\\txt1.txt”);
                BufferedReader br = new BufferedReader(fr);
                String s = br.readLine();
                BufferedWriter bw = new BufferedWriter(
                    new FileWriter(“C:\\txt2.txt”));
                bw.write(s);
                bw.newLine();
                bw.close();

        4、以字符流方式向显示器输出
            FileReader fr = new FileReader(“C:\\txt.txt”);
            BufferedReader br = new BufferedReader(fr);
            String s = br.readLine();
            BufferedWriter bw = new BufferedWriter(
                new OutputStreamWriter(System.out));
            bw.write(s);
            bw.newLine();
            bw.close();

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值