输入输出流-零基础学习

  1. Java中流的概念:
    1. 在Java程序中,对于数据的输入/输出操作以“流” (stream) 方式进行;J2SDK提供了各种各样的“流”类,用以获取不同种类的数据;程序中通过标准的方法输入或输出数据。
  2. 可以从不同的角度对其进行分类:
    1.  按数据流的方向不同可以分为输入流和输出流。(以程序的角度来考虑)
    2.  按处理数据单位不同可以分为字节流和字符流。
    3.  按照功能不同可以分为节点流和处理流
  3. 字节流:
    1. 输入流:抽象类InputStream
    2. 输出流:抽象类OutputStream
  4. 字符流
    1. 输入流:抽象类Reader
    2. 输出流:抽象类Writer
  5. 节点流
    1. 节点流为可以从一个特定的数据源(节点)读写数据(如:文件,内存)
  6. 处理流
    1. 处理流是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。
  7. 文件类File
    1. 文件和目录路径名的抽象表示形式。
  8. 常用方法:
    1. listRoots()//列出可用的文件系统根。
    2. getTotalSpace()//返回此抽象路径名指定的分区大小。
    3. getFreeSpace()//可用空间。
    4. 文件是否存在:file.exists()
    5. 是否是文件:file.isFile()
    6. 是否可读:file.canRead()
    7. 是否可写:file.canWrite()
    8. 是否隐藏:file.isHidden()
    9. 可执行:file.canExecute()
    10. 绝对路径:file.getAbsolutePath()
    11. 上层路径:file.getParent()
    12. 文件的长度:file.length()
    13. 文件最后修改时间:file.lastModified()
    14. 是否是文件夹:isDirectory()
    15. 得到当前文件夹下的所有文件或文件夹:listFiles()  返回值:File[]
    16. 得当当前文件的路径: getPath()  
  9. 案例:如何得到一个磁盘下的所有文件?在控制台中输出文件名称
    1. 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);
      				}
      			}
      		}
      	}
  10. InputStream
    1. 继承自InputSteam的流都是用于向程序中输入数据,且数据的单位为字节(8 bit)
    2. 常用方法:
    3. int read() throws IOException
      • 如果返回-1已到输入流的末尾。
    4. int read(byte[] buffer) throws IOException
      • 返回实际读取的字节数,如果读取前已到输入流的末尾返回-1
    5. void close() throws IOException
      • 关闭流释放内存资源
  11. InputStream的常用子类:
    1. FileInputStream
  12. OutputStream
    1. 继承自OutputSteam的流是用于程序中输出数据,且数据的单位为字节(8 bit)
    2. 常用方法:
    3. void write(int b) throws IOException
      • 向输出流中写入一个字节数据,该字节数据为参数b的低8位
    4. void write(byte[] b) throws IOException
      • 将一个字节类型的数组中的数据写入输出流
    5. void write(byte[] b, int off, int len)  throws IOException
      • 将一个字节类型的数组中的从指定位置(off)开始的len个字节写入到输出流
    6. void close() throws IOException
      • 关闭流释放内存资源
    7. void flush() throws IOException
      • 将输出流中缓冲的数据全部写出到目的地
  13. OutputStream的常用子类
    1. FileOutputStream
  14. Reader
    1. 继承自Reader的流都是用于向程序中输入数据,且数据的单位为字符(16 bit)
    2. 常用子类:
      • 节点流:InputStreamReader
        1. FileReader
      • 处理流:BufferReader
  15. Reader的基本方法
    1. int read() throws IOException
      • 如果返回-1已到输入流的末尾。
    2. int read(char[] cbuf) throws IOException
      • 返回实际读取的字符数,如果读取前已到输入流的末尾返回-1
    3. void close() throws IOException
      • 关闭流释放内存资源
  16. Writer
    1. 继承自Writer的流都是用于程序中输入数据,且数据的单位为字符(16 bit);
    2. 常用子类:
      • 节点流:OutStreamReader
        1. FileWriter
      • 处理流:BufferWriter
  17. Writer的基本用法
    1. void write(int c) throws IOException
      • 向输出流中写入一个字符数据,该字节数据为参数b的低16位
    2. void write(char[] cbuf) throws IOException
      • 将一个字符类型的数组中的数据写入输出流,
    3. void write(char[] cbuf, int offset, int length)throws IOException
      • 将一个字符类型的数组中的从指定位置(offset)开始的length个字符写入到输出流
    4. void write(String string) throws IOException
      • 将一个字符串中的字符写入到输出流
    5. void close() throws IOException
      • 关闭流释放内存资源
    6. void flush() throws IOException
      • 将输出流中缓冲的数据全部写出到目的地
  18. 代码案例:“将一个C盘的文件a.txt复制到D盘 a.txt”
    1. 	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();
      			}
      		}
      	}

       

  19. BufferedReader
    1. 从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
    2. 示例代码:
      • BufferedReader in = new BufferedReader(new FileReader("foo.in"));
    3. 方法示例:
      • readLine()
        1. 读取一个文本行。
  20. BufferedWriter
    1. 将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
    2. 示例代码:
      • new BufferedWriter(new FileWriter("foo.out"));
  21. 字符(Character):
    1. 是文字与符号的总称,包括文字、图形符号、数学符号等。
  22. 字符集(Charset):
    1. 就是一组抽象字符的集合。
    2. 字符集常常和一种具体的语言文字对应起来,该文字中的所有字符或者大部分常用字符就构成了该文字的字符集,比如英文字符集。
    3. 一组有共同特征的字符也可以组成字符集,比如繁体汉字字符集、日文汉字字符集。
  23. 编码(Encoding):
    1. 制定编码首先要确定字符集,并将字符集内的字符排序,然后和二进制数字对应起来。根据字符集内字符的多少,会确定用几个字节来编码。
  24. UTF-8:
    1. 8bit变长编码,对于大多数常用字符集(ASCII中0~127字符)它只使用单字节,而对其它常用字符(特别是朝鲜和汉语会意文字),它使用3字节。
  25. UTF-16:
    1. 16bit编码,大致相当于20位编码,
  26. GB2312字集
    1. 是简体字集,全称为GB2312(80)字集,共包括国标简体汉字6763个。
    2. 通常也称为ANSI字符集。
  27. BIG5字集
    1. 是台湾繁体字集,共包括国标繁体汉字13053个。
  28. GBK字集
    1. 是简繁字集,包括了GB字集、BIG5字集和一些符号,共包括21003个字符。
  29. GB18030字集
    1. 是国家制定的一个强制性大字集标准,全称为GB18030-2000,它的推出使汉字集有了一个“大一统”的标准。
  30. InputStreamReader
    1. 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。
    2. 示例代码:
      1. 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);
        		}
        	}

         

  31. Serializable
    1. 类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。
  32. ObjectOutputStream
    1. 对象输出流
    2. 案例:
    3. 	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();
      	}
  33. ObjectInputStream
    1. 对象输入流
    2. 案例:
    3. 	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();
      	}

       

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值