什么是字节流?
- 字节流的类通常以stream结尾
字节流--传输过程中,传输数据的最基本单位是字节的流。
什么是字符流?
- 字符流的类通常以reader和writer结尾
字符流--传输过程中,传输数据的最基本单位是字符的流。
字节输入流:
常用的字节输入流主要有:
- InputStream
- FileInputStream
- BufferedInputStream 【BufferedInputStream不是InputStream的直接实现子类,是FilterInputStream的子类】
他们的区别与用途:
- InputStream是字节输入流的抽象基类 ,InputStream作为基类,给它的基类定义了几个通用的函数:
-
- read(byte[] b):从流中读取b的长度个字节的数据存储到b中,返回结果是读取的字节个数(当再次读时,如果返回-1说明到了结尾,没有了数据)
read(byte[] b, int off, int len):从流中从off的位置开始读取len个字节的数据存储到b中,返回结果是实际读取到的字节个数(当再次读时,如果返回-1说明到了结尾,没有了数据)- close():关闭流,释放资源。
-
- FileInputStream主要用来操作文件输入流,它除了可以使用基类定义的函数外,它还实现了基类的read()函数(无参的):
-
- read():从流中读取1个字节的数据,返回结果是一个int,(如果编码是以一个字节一个字符的,可以尝试转成char,用来查看数据)。
-
- BufferedInputStream带有缓冲的意思,普通的读是从硬盘里面读,而带有缓冲区之后,BufferedInputStream已经提前将数据封装到内存中,内存中操作数据要快,所以它的效率要要非缓冲的要高。它除了可以使用基类定义的函数外,它还实现了基类的read()函数(无参的):
-
- read():从流中读取1个字节的数据,返回结果是一个int,(如果编码是以一个字节一个字符的,可以尝试转成char,用来查看数据)。
-
- InputStream是字节输入流的抽象基类 ,InputStream作为基类,给它的基类定义了几个通用的函数:
字节输出流:
常用的字节输出流主要有:
- OutputStream
- FileOutputStream
- BufferedOutputStream 【BufferedOutputStream不是OutputStream的直接实现子类,是FilterOutputStream的子类】
他们的区别与用途:
- OutputStream是字节输出流的基类, OutputStream作为基类,给它的基类定义了几个通用的函数:
-
- write(byte[] b):将b的长度个字节数据写到输出流中。
- write(byte[] b,int off,int len):从b的off位置开始,获取len个字节数据,写到输出流中。
- flush():刷新输出流,把数据马上写到输出流中。
- close():关闭流,释放系统资源。
-
- FileOutputStream是用于写文件的输出流,它除了可以使用基类定义的函数外,还实现了OutputStream的抽象函数write(int b):
-
- write(int b):将b转成一个字节数据,写到输出流中。
-
- BufferedOutputStream像上面那个BufferedInputStream一样,都可以提高效率。它除了可以使用基类定义的函数外,它还实现了OutputStream的抽象函数write(int b):
-
- write(int b):将b转成一个字节数据,写到输出流中。
-
字符输入流:
常见的字符输入流有:
- Reader
- InputStreamReader
- FileReader
- BufferedReader
他们的区别与用途:
- Reader是字符输入流的抽象基类 ,它定义了以下几个函数:
-
- read() :读取单个字符,返回结果是一个int,需要转成char;到达流的末尾时,返回-1
- read(char[] cbuf):读取cbuf的长度个字符到cbuf这种,返回结果是读取的字符数,到达流的末尾时,返回-1
- close() :关闭流,释放占用的系统资源。
-
- InputStreamReader 可以把InputStream中的字节数据流根据字符编码方式转成字符数据流。它除了可以使用基类定义的函数,它自己还实现了以下函数:
- read(char[] cbuf, int offset, int length) :从offset位置开始,读取length个字符到cbuf中,返回结果是实际读取的字符数,到达流的末尾时,返回-1
- FileReader 可以把FileInputStream中的字节数据转成根据字符编码方式转成字符数据流。
- BufferedReader可以把字符输入流进行封装,将数据进行缓冲,提高读取效率。它除了可以使用基类定义的函数,它自己还实现了以下函数:
- read(char[] cbuf, int offset, int length) :从offset位置开始,读取length个字符到cbuf中,返回结果是实际读取的字符数,到达流的末尾时,返回-1
- readLine() :读取一个文本行,以行结束符作为末尾,返回结果是读取的字符串。如果已到达流末尾,则返回 null
字符输出流:
常见的字符输出流有:
- Writer
- OutputStreamWriter
- FileWriter
- BufferedWriter
他们的区别与用途:
- Writer是字符输出流的抽象基类, ,它定义了以下几个函数
- write(char[] cbuf) :往输出流写入一个字符数组。
- write(int c) :往输出流写入一个字符。
- write(String str) :往输出流写入一串字符串。
- write(String str, int off, int len) :往输出流写入字符串的一部分。
- close() :关闭流,释放资源。 【这个还是抽象的,写出来是说明有这个关闭功能】
flush():刷新输出流,把数据马上写到输出流中。 【这个还是抽象的,写出来是说明有这个关闭功能】
- OutputStreamWriter可以使我们直接往流中写字符串数据,它里面会帮我们根据字符编码方式来把字符数据转成字节数据再写给输出流,它相当于一个中介\桥梁。
- FileWriter与OutputStreamWriter功能类似,我们可以直接往流中写字符串数据,FileWriter内部会根据字符编码方式来把字符数据转成字节数据再写给输出流。
- BufferedWriter比FileWriter还高级一点,它利用了缓冲区来提高写的效率。它还多出了一个函数:
-
- newLine() :写入一个换行符。
附代码:
package fileTest; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.Writer; import org.apache.http.impl.conn.Wire; import sun.nio.cs.UnicodeEncoder; //读取文件内容 public class TestFile { public static void main(String[] args) { try { readFromFileByByte(); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("找不到文件"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("读取不成功"); } try { readFromFileByteTwo(); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("找不到文件"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("读取不成功"); } // try { // readFromFileChar(); // } // catch (FileNotFoundException e) { // e.printStackTrace(); // System.out.println("找不到文件"); // } // catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // System.out.println("读取不成功"); // } // try { // writeFromFileChar(); // System.out.println("写入成功"); // } // catch (FileNotFoundException e) { // e.printStackTrace(); // System.out.println("找不到文件"); // } // catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // System.out.println("读取不成功"); // } } //读取字节流InputStream :方法一 public static void readFromFileByByte() throws IOException{ File file = new File("d:"+File.separator+"abc.txt"); if(!file.exists()){ file.createNewFile(); } InputStream inputStream = new FileInputStream(file); byte[] bs = new byte[1024]; int len =inputStream.read(bs); inputStream.close(); System.out.print("字节流一:"); System.out.println(new String(bs)); } //读取字节流 InputStream :方法二 public static void readFromFileByteTwo() throws IOException{ File file = new File("d:"+File.separator+"abc.txt"); if(!file.exists()){ file.createNewFile(); } InputStream inputStream = new FileInputStream(file); byte[] bs = new byte[(int)file.length()]; inputStream.read(bs); System.out.print("字节流二:"); inputStream.close(); System.out.println(new String(bs)); } //读取字符流Reader InputStreamReader public static void readFromFileChar() throws IOException{ File file = new File("d:"+File.separator+"abc.txt"); if(!file.exists()){ file.createNewFile(); } Reader reader = new FileReader(file); BufferedReader bre = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8")); String str=""; while((str=bre.readLine())!=null){ System.out.println(str); } bre.close(); reader.close(); } //写字符流write ,bufferedWriter,FileWriter public static void writeFromFileChar() throws IOException{ FileWriter fileWriter = new FileWriter("d:"+File.separator+"abc.txt",true);//第二个参数表示是追加的 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write("我是追加的字符串"); bufferedWriter.close(); fileWriter.close(); } }
讯享网


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/45111.html