IO概述

来源:互联网 发布:mac推出磁盘快捷键 编辑:程序博客网 时间:2024/06/15 16:05
1、IO流用来处理设备之间的数据传输

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

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

流按操作数据分为两种:字节流字符流,字符流是基于字节流

字节:字节英语:Byte。一个字节代表八个比特。它通常用作计算机信息计量单位,不分数据类型。它也是程序设计语言里不可缺少的基本数据类型。在处理图片、音视频文件时就只能使用字节流。

字符:电脑电信领域中,字符Character)是一个信息单位,一个字符是一个单位的字形、类字形单位或符号的基本信息

字符是指计算机中使用的字母、数字符号。

电脑和通信设备会在表示字符时,会使用字符编码。是指将一个字符对应为某个东西。传统上,是代表整数比特序列,如此,则可通过网络传输,同时亦便于存储。两个常用的例子是ASCII和用于Unicode编码UTF-8。根据Google的统计,UTF-8是目前最常用于网页的编码方式。相较于大部分的字符编码把字符对应到数字或比特串

IO流常用基类:

字节流的抽象基类:InputStream ,OutputStream

字符流的抽象基类:Reader ,Writer

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

如:InputStream的子类FileInputStream。
如:Reader的子类FileReader。


编程:

  • java.lang.Object
    • java.io.Writer
      • java.io.OutputStreamWriter
        • java.io.FileWriter
          • Constructors:
FileWriter(String fileName)
Constructs a FileWriter object given a file name. the 0riginal file will be override if once gain run.
public static void main(String[] args) {
FileWriter file = null;
try {
file = new FileWriter("F:\\fileIo.txt"); //Constructs a FileWriter object given a file name.
file.write("use FileWriter write string.");  //Writes a string.
} catch (IOException e) {
e.printStackTrace();
}finally{
if(file != null){
try {
file.close(); //Closes the stream, flushing it first.
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Constructors:
FileWriter(String fileName, boolean append)
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append(
  • append - boolean if true, then data will be written to the end of the file rather than the beginning.
) the data written.
public static void main(String[] args) {
FileWriter file = null;
try {
file = new FileWriter("F:\\file02.txt", true);
file.write("File Continue to write..");
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
  • Methods inherited from class java.io.Writer

    append, append, append, write, write
voidwrite(String str)
Writes a string.

  • Methods inherited from class java.io.OutputStreamWriter

    close, flush, getEncoding, write, write, write

voidflush()
Flushes the stream.
voidclose()
Closes the stream, flushing it first.
voidwrite(int c)
Writes a single character.

1 0