zoukankan      html  css  js  c++  java
  • 复习java基础第六天(IO)

    一:File 类

    • 输入:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。 
    • 输出:将程序(内存)数据输出到磁盘、光盘等存储设备中
    • Java 的 IO 流主要包括输入、输出两种 IO 流,每种输入、输出流有可分为字节流和字符流两大类:
    – 字节流以字节为单位来处理输入、输出操作
    – 字符流以字符为单位来处理输入、输出操作
    注意:输入、输出是以程序为参照。
    • File 类代表与平台无关的文件和目录。
    • File  能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。
      如果需要访问文件内容本身,则需要使用输入/输出流。
    部分操作File的方法:
    练习代码:
    package com.shellway.io;
    import java.io.File;
    import java.io.IOException;
    import org.junit.Test;
    public class IOtest {
    	@Test
    	public void test() throws IOException{
    		File file = new File("helloo.txt");
    		//获取文件的名称
    		String fileName = file.getName();
    		System.out.println(fileName);
    		//获取文件的绝对路径
    		String filePath = file.getAbsolutePath();
    		System.out.println(filePath);
    		//为文件重命名,不仅文件名变了且原来的文件路径跟着下面的路径变
    	//	file.renameTo(new File("D:\test\day03\helloo.txt"));
    		String path = file.getPath();
    		System.out.println(path);
    		//文件检测相关方法
    		System.out.println(file.exists());
    		File dir = new File("shellway");
    		System.out.println(dir.isDirectory());
    		System.out.println(dir.isFile());
    		//获取文件的常规信息
    		System.out.println(file.length());//单位为字节,一个汉字为两个字节,换行会加两个字节
            //文件的相关操作
    		File file2 = new File("adc.txt");
    		file2.createNewFile();//创建一个空的文件
    		File file3 = new File("adc");
    		file3.mkdir();//创建一个空的目录
    		file2.delete();//删除一个文件
    	}
    }

    1、IO流的分类

    • 按流向分:
    – 输入流
    – 输出流
    • 按处理的单位:
    – 字节流(8 位的字节)
    – 字符流(16 位的字节)
    • 按流的角色
    – 节点流:可以从一个特定的 IO 设备读/写数据的流
    – 处理流:对一个已存在的流进行连接和封装,通过封装后的流来实现数据读/写操作
     
    2、IO流体系:
     
    二、InputStream & Reader
    • InputStream 和 Reader 是所有输入流的基类。
    • InputStream(典型实现:FileInputStream):
    – int read()
    – int read(byte[] b)
    – int read(byte[] b, int off, int len)
    • Reader(典型实现:FileReader):
    – int read()
    – int read(char [] c)
    – int read(char [] c, int off, int len)
    • 程序中打开的文件 IO 资源不属于内存里的资源,垃圾回收机制无法回收该资源,所以应该显式关闭文件 IO 资源。
    练习代码:
     1 package com.shellway.io;
     2 import java.io.File;
     3 import java.io.FileInputStream;
     4 import java.io.FileNotFoundException;
     5 import java.io.FileReader;
     6 import java.io.IOException;
     7 import java.io.InputStream;
     8 import java.io.Reader;
     9 import org.junit.Test;
    10 public class IOtest {
    11     /**
    12      * 测试字符输入流
    13      * @throws IOException 
    14      */
    15     @Test
    16     public void testReader() throws IOException {
    17          Reader reader = new FileReader("helloo.txt");
    18          char []buffer = new char[10];
    19          int len = 0;
    20          while((len = reader.read(buffer)) != -1){
    21              for(int i = 0;i < len;i++){
    22                  System.out.print(buffer[i]);
    23              }
    24          }
    25          reader.close();
    26     }
    27     /**
    28      * 测试字节输入流
    29      * @throws IOException
    30      */
    31     @Test
    32     public void testInputStream() throws IOException {
    33         //1、创建一个字符输入流
    34         InputStream in = new FileInputStream("helloo.txt");
    35         //2、读取文件内容
    36         //2、1一次读取一个字符,效率很低,不建议这样读,-1表示读到文件的结尾
    37         int result = in.read();
    38         while(result!=-1){
    39         System.out.print((char)result);
    40          result = in.read();
    41         }
    42         //2、2一次读取一组字符,
    43         byte []buffer = new byte[10];
    44         int length = 0;
    45         while((length = in.read(buffer))!=-1){
    46             for(int i = 0;i<length;i++){   //注意不能加“=”号
    47                 System.out.print((char)buffer[i]);
    48             }
    49         //2、3把内容读取到字节数组的部分连续的元素中
    50         byte  []result1 = new byte[1024*10];
    51         //10为字节数组的开始部分,第三个为实际长度。
    52         in.read(result1, 10, in.available());
    53         }
    54         //3、关闭字符流
    55         in.close();
    56     }
    57 }
    测试字符、字节输入流
     
     
  • 相关阅读:
    NYOJ 10 skiing DFS+DP
    51nod 1270 数组的最大代价
    HDU 4635 Strongly connected
    HDU 4612 Warm up
    POJ 3177 Redundant Paths
    HDU 1629 迷宫城堡
    uva 796
    uva 315
    POJ 3180 The Cow Prom
    POJ 1236 Network of Schools
  • 原文地址:https://www.cnblogs.com/shellway/p/3792080.html
Copyright © 2011-2022 走看看