`

java-io初步:Reader、Writer、Buf…

 
阅读更多

java四个基本的io基类:

字节流:InputStream、OutputStream

字符流:Reader、Writer.——作为硬盘的文件使用的两个子类:FileWriter、FileReader.

<wbr></wbr>

1、java早期只有字节流,后边由于处理不同字符的编码需求,出现了字符流.

2、java io体系中子类的命名通常都是以父类的名称作为后缀.

<wbr></wbr>

<wbr></wbr>

import java.io.*;
class IODemo
{
<wbr>public static void main(String[] args) // throws Exception<br><wbr>{<br><wbr><wbr>//fileWrite();<br><wbr><wbr>//fileRead();<br><wbr><wbr>//bufferdWrite();<br><wbr><wbr>//bufferdRead();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>//通过缓冲区拷贝一个.java文件<br><wbr><wbr>copyJavaFile();<br><wbr>}</wbr></wbr></wbr></wbr></wbr>

<wbr>public static void fileWrite()<br><wbr>{<br><wbr><wbr>FileWriter fw = null;<wbr> //fw.close()调用需要引用fw.<br><wbr><wbr>try<br><wbr><wbr>{<br><wbr><wbr><wbr>fw = new FileWriter("iodemo.txt", false); //第二个参数决定是否向覆盖原文件.<br><wbr><wbr><wbr>fw.write("sddddddd");<wbr><wbr><wbr><br><wbr><wbr><wbr>fw.write("\r\naaa");<wbr>//windows\r\n为一个回车符.<wbr><wbr><br><wbr><wbr>}<br><wbr><wbr>catch (Exception e)<br><wbr><wbr>{<br><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr>}<br><wbr><wbr>finally<br><wbr><wbr>{<br><wbr><wbr><wbr>try<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>fw.close();<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>catch (Exception e)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>public static void fileRead()<br><wbr>{<br><wbr><wbr>//读取文件通用格式:<br><wbr><wbr>//建立字符流读取对象,于指定文件关联<br><wbr><wbr>FileReader fr = null;<br><wbr><wbr>try<br><wbr><wbr>{<br><wbr><wbr><wbr>fr = new FileReader("iodemo.txt");<br><wbr><wbr><wbr>char[] arr = new char[1024];<wbr> //将读出的数据存入到数组中.(缓存)<br><wbr><wbr><wbr>int num = 0;<br><wbr><wbr><wbr>while ((num = fr.read(arr)) != -1) //没有读到数据后,返回-1,循环结束;read方法可以自动向后读取剩下的数据,<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>System.out.println(new String(arr, 0, num));<br><wbr><wbr><wbr>}<br><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


<wbr><wbr>catch (Exception e)<br><wbr><wbr>{<br><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr>}<br><wbr><wbr>finally<br><wbr><wbr>{<br><wbr><wbr><wbr>try<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>fr.close();<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>catch (Exception e)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>public static void bufferdWrite()<br><wbr>{<br><wbr><wbr>//创建流对象<wbr><wbr><br><wbr><wbr>FileWriter fw = null;<br><wbr><wbr>//创建缓冲区跟流对象关联。<br><wbr><wbr>BufferedWriter bfdw = null;<br><wbr><wbr>try<br><wbr><wbr>{<br><wbr><wbr><wbr>fw = new FileWriter("iodemo.txt");<br><wbr><wbr><wbr>bfdw = new BufferedWriter(fw);<br><wbr><wbr><wbr>int i = 0;<br><wbr><wbr><wbr>while (i &lt; 5)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>bfdw.write("aaa" + i);<br><wbr><wbr><wbr><wbr>bfdw.newLine();<wbr><br><wbr><wbr><wbr><wbr>bfdw.flush();<br><wbr><wbr><wbr><wbr>i++;<br><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr>bfdw.write("xxx"); //不调用flush或close,这一行数据不能写入.<br><wbr><wbr><wbr>bfdw.newLine();<br><wbr><wbr>}<br><wbr><wbr>catch (Exception e)<br><wbr><wbr>{<br><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr>}<br><wbr><wbr>finally<br><wbr><wbr>{<br><wbr><wbr><wbr>try<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>bfdw.close();<wbr> //缓冲区的关闭是调用传入流的关闭方法.<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>catch (Exception e)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>public static void bufferdRead()<br><wbr>{<br><wbr><wbr>//创建流对象<br><wbr><wbr>FileReader fr = null;<br><wbr><wbr>//创建缓冲区跟流对象关联。<br><wbr><wbr>BufferedReader bfdr = null;<br><wbr><wbr>try<br><wbr><wbr>{<br><wbr><wbr><wbr>fr = new FileReader("iodemo.txt");<wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>bfdr = new BufferedReader(fr);<br><wbr><wbr><wbr>String s;<br><wbr><wbr><wbr>while ( (s = bfdr.readLine()) != null)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>System.out.println(s);<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr><wbr>catch (Exception e)<br><wbr><wbr>{<br><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr>}<br><wbr><wbr>finally<br><wbr><wbr>{<br><wbr><wbr><wbr>try<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>bfdr.close();<wbr> //缓冲区的关闭是调用传入流的关闭方法.<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>catch (Exception e)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>public static void copyJavaFile()<br><wbr>{<br><wbr><wbr>//创建流对象<br><wbr><wbr>FileReader fr = null;<br><wbr><wbr>FileWriter fw = null;<br><wbr><wbr>//创建缓冲区跟流对象关联。<br><wbr><wbr>BufferedReader bfdr = null;<br><wbr><wbr>BufferedWriter bfdw = null;<br><wbr><wbr>try<br><wbr><wbr>{<br><wbr><wbr><wbr>fr = new FileReader("iodemo.java");<br><wbr><wbr><wbr>fw = new FileWriter("iodemo复件.txt");<br><wbr><wbr><wbr>bfdr = new BufferedReader(fr);<br><wbr><wbr><wbr>bfdw = new BufferedWriter(fw);<br><wbr><wbr><wbr>String line;<br><wbr><wbr><wbr>while ( (line = bfdr.readLine()) != null)<wbr> //readLine没有读取回车符,只读取了回车符前的数据,因此写入文本时需要执行newLine方法.<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>bfdw.write(line);<br><wbr><wbr><wbr><wbr>bfdw.newLine();<br><wbr><wbr><wbr><wbr>bfdw.flush();<br><wbr><wbr><wbr><wbr>//System.out.println(line);<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr><wbr>catch (IOException e)<br><wbr><wbr>{<br><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr>}<br><wbr><wbr>finally<br><wbr><wbr>{<br><wbr><wbr><wbr>//对两个缓冲区分别进行关闭<br><wbr><wbr><wbr>if (bfdw != null)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>try<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>bfdw.close();<wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr>catch (Exception e)<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr><br><wbr><wbr><wbr>if (bfdr != null)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>try<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>bfdr.close();<wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr>catch (Exception e)<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr>}<br> }<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

分享到:
评论

相关推荐

    完整版Java全套入门培训课件 Java基础 07-IO(共29页).pptx

    Java IO(InputOutput)是Java平台中用于处理设备间数据传输的重要部分,它提供了一种在程序和外部数据源之间传输数据的机制。Java中的所有IO操作都是通过流(Stream)来实现的,这些流的对象主要集中在java.io包中...

    buffer-io:Easy Buffer读写工具。 https

    @ imed.ch / buffer-io 在github上的。 缓冲区写入和读取实用程序。 BufferIO在旧版添加了以下功能: BufferIO读取器或写入器为您跟踪偏移量。 您可以在实例化缓冲区时指定默认字节序(但仍可以使用其他字节序...

    io的总结笔记

    根据给定的信息,我们可以整理出一系列关于Java IO流的关键知识点,包括基本概念、不同类型的流以及它们的应用场景等。 ### Java IO流概述 在Java中,输入/输出(Input/Output,简称IO)流主要用于处理数据的读写...

    commons-io-2.4.rar

    还有用于读取和写入字符流的`Reader`和`Writer`的包装类,如`BufferedReader`和`BufferedWriter`。 3. **字符集转换**:`Charsets`类提供了对各种字符编码的支持,如UTF-8、ISO-8859-1等,使得在处理不同编码的文件...

    深入分析javaIO的工作机制.doc

    由于在大多数情况下,程序处理的数据是以字符形式存在的,因此Java提供了专门针对字符操作的IO接口——`Writer`和`Reader`。这两个接口及其子类主要用于解决字符数据的读写问题,同时也涉及到了字符编码的处理。 `...

    java IO章节的总结

    ### Java IO系统总结 Java IO(输入/输出)系统是Java平台的重要组成部分,它提供了用于文件和数据流处理的基础框架。根据数据传输的基本单位不同,Java IO系统主要分为两大类:字节流和字符流。这两类流又各自包含...

    Java IO字符流和字节流

    Java中的`Reader`和`Writer`是所有字符流的基类。 - **`Reader`**:用于从源设备读取字符数据。 - **`Writer`**:用于向目标设备写入字符数据。 ##### 4. 字符流的基本API - **`public int read()`**:从输入流...

    Java 中的 IO 流是如何实现的?

    Java 中的 IO 流是Java平台提供的一种处理输入输出数据的方式。IO,即Input(输入)和Output(输出),在Java中表现为数据流的形式,用于读取或写入各种数据源,如文件、网络连接等。Java的IO流体系结构复杂而强大,...

    Java 中IO流字符流详解及实例

    字符流(Reader、Writer)是操作文本、文本文件的接口。它是一次处理一个字符,字符的底层仍然是基本的字节序列。字符流的基本实现包括: * InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset ...

    Java常用字节流和字符流实例汇总

    Java中的IO流主要分为两大类:字节流和字符流,它们主要用于数据的输入和输出。字节流处理的是单个字节的数据,而字符流处理的是Unicode编码的字符。 1. 字节流: - 字节输入流:`InputStream` 是所有字节输入流的...

    Android平台的txt文本文件读写操作

    此外,`FileReader`还支持`java.io.InputStreamReader`和`java.io.Reader`接口提供的所有方法,例如`void mark(int readLimit)`用于设置标记,`boolean markSupported()`用于判断是否支持标记等。 #### 三、注意...

    Io学习总结

    【Java IO 学习总结】 Java IO 是 Java 平台中用于处理输入和输出的核心库,它提供了丰富的类和接口,使得程序能够与各种设备、文件系统以及网络进行交互。IO 操作通常涉及数据的传输,Java 中所有的数据传输都是...

    字节流实现从文件以字节读取信息

    对于文本文件,我们通常使用字符流(如`Reader`和`Writer`);而对于二进制文件(如图片、音频等),则需要使用字节流(如`InputStream`和`OutputStream`)。本文将详细介绍如何使用Java中的字节流技术来实现从文件...

    Go-Go中的线程安全循环缓冲区环形缓冲区实现了io.ReaderWriter接口

    本篇文章将深入探讨如何构建一个实现了`io.Reader`和`io.Writer`接口的线程安全环形缓冲区。 首先,理解`io.Reader`和`io.Writer`接口至关重要。这两个接口是Go语言标准库`io`包中定义的基本I/O接口,它们分别代表...

    详解java IO流之缓冲流的使用

    Java IO流中的缓冲流是提高I/O操作性能的重要工具,它们并不直接进行读写操作,而是作为其他流的包装,通过内存缓冲区来优化数据传输。缓冲流分为字节缓冲流和字符缓冲流,每种又有输入和输出两种类型。 1. 字节...

    Java编程中字节流与字符流IO操作示例

    字符流的抽象基类是`Reader`和`Writer`,它们同样代表输入流和输出流。例如,`FileReader`和`FileWriter`是处理文本文件的字符流,能够方便地进行字符级别的读写。当使用`FileWriter`创建对象时,必须指定文件路径,...

    nio:Go #golang中的并发缓冲IO

    ReadWriter} nio的Copy方法同时从io.Reader复制到提供的nio.Buffer,然后从nio.Buffer复制到io.Writer。 这样,阻止写入不会降低io.Reader的速度。 import ( "github....

    Go-pipe用于读了管道数据的小型Go包

    这两个接口提供了与标准库`io.Reader`和`io.Writer`相似的方法,如`Read()`和`Write()`,但它们针对的是管道操作,而非普通的文件或网络流。 3. **pipe.New()** 包提供的`pipe.New()`函数可以创建一个新的管道,并...

Global site tag (gtag.js) - Google Analytics