- Java中流的概念:
- 在Java程序中,对于数据的输入/输出操作以“流” (stream) 方式进行;J2SDK提供了各种各样的“流”类,用以获取不同种类的数据;程序中通过标准的方法输入或输出数据。
- 可以从不同的角度对其进行分类:
- 按数据流的方向不同可以分为输入流和输出流。(以程序的角度来考虑)
- 按处理数据单位不同可以分为字节流和字符流。
- 按照功能不同可以分为节点流和处理流
- 字节流:
- 输入流:抽象类InputStream
- 输出流:抽象类OutputStream
- 字符流
- 输入流:抽象类Reader
- 输出流:抽象类Writer
- 节点流
- 节点流为可以从一个特定的数据源(节点)读写数据(如:文件,内存)
- 处理流
- 处理流是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。
- 文件类File
- 文件和目录路径名的抽象表示形式。
- 常用方法:
- listRoots()//列出可用的文件系统根。
- getTotalSpace()//返回此抽象路径名指定的分区大小。
- getFreeSpace()//可用空间。
- 文件是否存在:file.exists()
- 是否是文件:file.isFile()
- 是否可读:file.canRead()
- 是否可写:file.canWrite()
- 是否隐藏:file.isHidden()
- 可执行:file.canExecute()
- 绝对路径:file.getAbsolutePath()
- 上层路径:file.getParent()
- 文件的长度:file.length()
- 文件最后修改时间:file.lastModified()
- 是否是文件夹:isDirectory()
- 得到当前文件夹下的所有文件或文件夹:listFiles() 返回值:File[]
- 得当当前文件的路径: getPath()
- 案例:如何得到一个磁盘下的所有文件?在控制台中输出文件名称
-
public static void main(String[] args) { File file = new File("E:\\"); openFile(file); } public static void openFile(File file) { if (file.isFile()) { System.out.println(file); } else { File[] files = file.listFiles(); for (File f : files) { if (f.isDirectory()) { openFile(f);// 继续打开当前文件夹 } else { System.out.println(f); } } } }
-
- InputStream
- 继承自InputSteam的流都是用于向程序中输入数据,且数据的单位为字节(8 bit)
- 常用方法:
- int read() throws IOException
- 如果返回-1已到输入流的末尾。
- int read(byte[] buffer) throws IOException
- 返回实际读取的字节数,如果读取前已到输入流的末尾返回-1
- void close() throws IOException
- 关闭流释放内存资源
- InputStream的常用子类:
- FileInputStream
- OutputStream
- 继承自OutputSteam的流是用于程序中输出数据,且数据的单位为字节(8 bit)
- 常用方法:
- void write(int b) throws IOException
- 向输出流中写入一个字节数据,该字节数据为参数b的低8位
- void write(byte[] b) throws IOException
- 将一个字节类型的数组中的数据写入输出流
- void write(byte[] b, int off, int len) throws IOException
- 将一个字节类型的数组中的从指定位置(off)开始的len个字节写入到输出流
- void close() throws IOException
- 关闭流释放内存资源
- void flush() throws IOException
- 将输出流中缓冲的数据全部写出到目的地
- OutputStream的常用子类
- FileOutputStream
- Reader
- 继承自Reader的流都是用于向程序中输入数据,且数据的单位为字符(16 bit)
- 常用子类:
- 节点流:InputStreamReader
- FileReader
- 处理流:BufferReader
- 节点流:InputStreamReader
- Reader的基本方法
- int read() throws IOException
- 如果返回-1已到输入流的末尾。
- int read(char[] cbuf) throws IOException
- 返回实际读取的字符数,如果读取前已到输入流的末尾返回-1
- void close() throws IOException
- 关闭流释放内存资源
- int read() throws IOException
- Writer
- 继承自Writer的流都是用于程序中输入数据,且数据的单位为字符(16 bit);
- 常用子类:
- 节点流:OutStreamReader
- FileWriter
- 处理流:BufferWriter
- 节点流:OutStreamReader
- Writer的基本用法
- void write(int c) throws IOException
- 向输出流中写入一个字符数据,该字节数据为参数b的低16位
- void write(char[] cbuf) throws IOException
- 将一个字符类型的数组中的数据写入输出流,
- void write(char[] cbuf, int offset, int length)throws IOException
- 将一个字符类型的数组中的从指定位置(offset)开始的length个字符写入到输出流
- void write(String string) throws IOException
- 将一个字符串中的字符写入到输出流
- void close() throws IOException
- 关闭流释放内存资源
- void flush() throws IOException
- 将输出流中缓冲的数据全部写出到目的地
- void write(int c) throws IOException
- 代码案例:“将一个C盘的文件a.txt复制到D盘 a.txt”
-
public static void main(String[] args) { File oldFile = new File("c:/a.txt"); File newFile = new File("d:/a.txt"); Reader reader = null; Writer writer = null; try { reader = new FileReader(oldFile); writer = new FileWriter(newFile); while (true) { int i = reader.read(); if (i == -1) break; // 将这个信息输出到目标文件当中 writer.write((char) i); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { writer.flush(); } catch (IOException e) { e.printStackTrace(); } try { writer.close(); } catch (IOException e) { e.printStackTrace(); } try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
-
- BufferedReader
- 从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
- 示例代码:
- BufferedReader in = new BufferedReader(new FileReader("foo.in"));
- 方法示例:
- readLine()
- 读取一个文本行。
- readLine()
- BufferedWriter
- 将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
- 示例代码:
- new BufferedWriter(new FileWriter("foo.out"));
- 字符(Character):
- 是文字与符号的总称,包括文字、图形符号、数学符号等。
- 字符集(Charset):
- 就是一组抽象字符的集合。
- 字符集常常和一种具体的语言文字对应起来,该文字中的所有字符或者大部分常用字符就构成了该文字的字符集,比如英文字符集。
- 一组有共同特征的字符也可以组成字符集,比如繁体汉字字符集、日文汉字字符集。
- 编码(Encoding):
- 制定编码首先要确定字符集,并将字符集内的字符排序,然后和二进制数字对应起来。根据字符集内字符的多少,会确定用几个字节来编码。
- UTF-8:
- 8bit变长编码,对于大多数常用字符集(ASCII中0~127字符)它只使用单字节,而对其它常用字符(特别是朝鲜和汉语会意文字),它使用3字节。
- UTF-16:
- 16bit编码,大致相当于20位编码,
- GB2312字集
- 是简体字集,全称为GB2312(80)字集,共包括国标简体汉字6763个。
- 通常也称为ANSI字符集。
- BIG5字集
- 是台湾繁体字集,共包括国标繁体汉字13053个。
- GBK字集
- 是简繁字集,包括了GB字集、BIG5字集和一些符号,共包括21003个字符。
- GB18030字集
- 是国家制定的一个强制性大字集标准,全称为GB18030-2000,它的推出使汉字集有了一个“大一统”的标准。
- InputStreamReader
- 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。
- 示例代码:
-
public static void main(String[] args) throws Exception { FileInputStream fin = new FileInputStream("d:\\a.txt"); // 如果换成GBK则为乱码 InputStreamReader fileIn = new InputStreamReader(fin, "utf-8"); BufferedReader infm = new BufferedReader(fileIn); String s = ""; while (true) { s = infm.readLine(); if (s == null) break; System.out.println(s); } }
-
- Serializable
- 类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。
- ObjectOutputStream
- 对象输出流
- 案例:
-
private static void method1() throws IOException { // 文件字节输出流 FileOutputStream output = new FileOutputStream("d:/22.temp"); // 创建ObjectOutputStream ObjectOutputStream stream = new ObjectOutputStream(output); // 创建n个对象 int i = 168; String s = "你好2020"; Date d1 = new Date(); Name name = new Name("王", "晨"); // 输出 stream.writeInt(i); stream.writeObject(s); stream.writeObject(d1); stream.writeObject(name); // 关闭 stream.close(); }
- ObjectInputStream
- 对象输入流
- 案例:
-
private static void method2() throws IOException, ClassCastException, ClassNotFoundException { // 文件字节输入流 FileInputStream input = new FileInputStream("d:/22.temp"); // 创建ObjectInputStream ObjectInputStream stream = new ObjectInputStream(input); // 使用ObjectInputStream对象,将文件中的对象读 int i = stream.readInt(); String s = stream.readObject().toString(); Date date = (Date) stream.readObject(); Name name = (Name) stream.readObject(); System.out.println(i); System.out.println(s); System.out.println(date); System.out.println(name.getFirstName()); // 关闭 stream.close(); }