第十二章 文本I/O

一,file类

File 类意图提供了一种抽象,这种抽象是指以不依赖机器的方式来处理很多依赖于机器的文件和路径名的复杂性。F i l e 类包含许多获取文件属性的方法:

		File file = new File("F:\\桌面\\1.txt"); //创建一个file对象
		file.getName(); //返回由此抽象路径名表示的文件或目录的名称。
		file.getParent(); //返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。
		file.getPath(); //将此抽象路径名转换为一个路径名字符串并输出。
		file.isAbsolute(); //测试此抽象路径名是否为绝对路径名。
		file.getAbsoluteFile(); //返回此抽象路径名的绝对路径名字符串。 
		file.canRead(); //测试应用程序是否可以读取此抽象路径名表示的文件
		file.exists(); //测试此抽象路径名表示的文件或目录是否存在。
		file.isDirectory(); //测试此抽象路径名表示的文件是否是一个目录。 
		file.isFile(); //测试此抽象路径名表示的文件是否是一个标准文件。
		file.lastModified(); //返回此抽象路径名表示的文件最后一次被修改的时间。 
		file.length(); //返回由此抽象路径名表示的文件的长度
		file.createNewFile(); //当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件
		file.delete(); //删除此抽象路径名表示的文件或目录。
		file.list(); //返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。 
		file.listFiles(); //返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。 
		file.mkdirs(); //创建此抽象路径名指定的目录,包括所有必需但不存在的父目录
		file.renameTo(file); //重新命名此抽象路径名表示的文件。 
		file.toString(); //返回此抽象路径名的路径名字符串。
		file.compareTo(file); //按字母顺序比较两个抽象路径名。
		file.equals(file); //测试此抽象路径名与给定对象是否相等。
		file.hashCode(); //计算此抽象路径名的哈希码

二,字节流inputstream(输入流)与outputstream(输出流)

InputStream 是用来从 CDR 编组流中读取 IDL 类型的 Java API。这些方法供 ORB 用来解组 IDL 类型以及从 Any 中析取 IDL。_array 形式的方法可以直接用来读取 IDL 类型的序列和数组。
OuputStream 是用来将 IDL 类型写入 CDR 编组流的 Java API。这些方法供 ORB 用来编组 IDL 类型以及将 IDL 插入 Any。这些 _array 形式的方法可以直接用于写入 IDL 类型的序列和数组。

//inputstream的应用
		File file = new File("1.txt");
		InputStream is = new FileInputStream(file);
		//一个字节一个字节读取文件
		int b;
		while((b = is.read()) != -1) {
			System.out.println(b);
		}
		//计算文件大小
		int size = 0;
		while(is.read() != -1) {
			size++;
		}
		System.out.println("文件大小为:" + size);
		//用数组读取文件
		byte[] buf = new byte[2];
		int len = 0;
		while((len = is.read(buf)) != -1) {
			System.out.println(len + " " + Arrays.toString(buf));
		}

//outputstream的应用
		File file = new File("1.txt");
		FileOutputStream fos = new FileOutputStream(file);
		//一个一个写入
		fos.write(48);
		System.out.println("写入成功");
		//以数组写入
		byte[] buf = new byte[] {97,98};
		fos.write(buf);
		System.out.println("写入成功");

//复制
public static void main(String[] args) throws IOException {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("1.txt");
			fos = new FileOutputStream("2.txt");
			int content;
			while((content = fis.read()) != -1) {
				fos.write(content);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

三,字符流bufferredinputstream与bufferredoutputstream

BufferedInputStream 为另一个输入流添加一些功能,即缓冲输入以及支持 mark 和 reset 方法的能力。在创建 BufferedInputStream 时,会创建一个内部缓冲区数组。在读取或跳过流中的字节时,可根据需要从包含的输入流再次填充该内部缓冲区,一次填充多个字节。mark 操作记录输入流中的某个点,reset 操作使得在从包含的输入流中获取新字节之前,再次读取自最后一次 mark 操作后读取的所有字节。

//用字符流进行复制
public static void main(String[] args) throws IOException {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		BufferedInputStream bis = null; 
		BufferedOutputStream bos = null;
		try {
			fis = new FileInputStream("1.txt");
			bis = new BufferedInputStream(fis);
			fos = new FileOutputStream("2.txt");
			bos = new BufferedOutputStream(fos);
			byte[] buf = new byte[1024];
			int len;
			while((len = bis.read(buf)) != -1) {
				bos.write(buf,0,len);
			}
			System.out.println("复制成功");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值