I/O输入输出流

变量、数组和对象中存储数据都是栈内存中,程序结束后会立即消失,为了永久保存创建的数据,将其保存至磁盘文件。Java中的I/O技术可以将数据保存到文本文件、二进制文件、zip文件,达到永久保存。

1. 流概述

流是一组有序的数据序列,数据在两设备之间的传输称为流,流的本质就是数据传输

程序从指向源的输入流中读取源中的数据,输出流的指向是数据要到达的目的
在这里插入图片描述

2. 输入流

  • InputStream:字节输入流的抽象类

遇到错误的时候引发 IOException异常

  • Reader:字符输入流的抽象类

3. 输出流

  • OutputStream:字节输出流的抽象类

所有方法均返回void,遇到错误的时候引发 IOException异常

  • Writer:字符输出流的抽象类

4. File类

File类是io包中唯一代表磁盘文件本身的对象,通过调用File类中的方法,实现创建、删除、重命名文件操作

4.1 文件的创建删除
  1. File file = new File("PATHNAME"):路径名
  2. new file(String parent,String child):父/子路径字符串
  3. new file(File f, String child):父对象,子路径字符串
// 只提供了对字节或字节数组的读取方法
public class FileTest {
	public static void main(String[] args) {
		File file = new File("word.txt");
		
//		if(file.exists()) {
			file.delete();
//			
//		}else {
		try {
//				file.createNewFile();
//				FileOutputStream类对象,向文件file中 写入信息
			FileOutputStream out = new FileOutputStream(file);
			byte hello[] = "要努力成为更好的自己呀".getBytes();
			out.write(hello);
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		try {
			// FileInputStream 读取file信息
			FileInputStream input = new FileInputStream(file);
			
			byte hell[] =new byte[1024];
			//  从文件中读取信息
			int len = input.read(hell);
			
//			System.out.println(new String(hell,0,len));
			System.out.println(len);
			
			input.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

	
}
4.2 获取文件信息
方法功能
file.exists()判断文件是否存在
file.createNewFile()创建文件
file.getName()获取文件名
file.getAbsolutePath()绝对路径
file.getPath()相对路径
file.getParent()父路径
file.canRead()是否可读
file.length()文件长度
file.isDirectory()是否为一个目录
file.isFile()是否是一个文件

博客1

5. 带缓存的输入/输出流

可参考博客

5.1 BufferedInputStream和BufferedOutputStream
  • BufferedInputStream(InputStream in) :32字节的缓存流
  • BufferedInputStream(InputStream in,int size):指定大小
5.2 BufferedReader和BufferedWriter

方法

int read() //读取单个字符。
int read(char[] cbuf, int off, int len) //将字符读入数组的某一部分。
String readLine() //读取一个文本行。
boolean ready() //判断此流是否已准备好被读取。
void reset() //将流重置到最新的标记。
long skip(long n) //跳过字符。
void close() //关闭该流并释放与之关联的所有资源。
void mark(int readAheadLimit) //标记流中的当前位置。
boolean markSupported() //判断此流是否支持 mark() 操作(它一定支持)。

  • BufferedReader(Reader in)

  • BufferedWriter(Reader in, int sz):指定大小

  • BufferedWriter(Writer out, int sz) //创建一个使用给定大小输出缓冲区的新缓冲字符输出流

  • BufferedWriter(Writer out) //建一个使用默认大小输出缓冲区的缓冲字符输出流。

代码

public class BufferTest {
	public static void main(String[] args){
//		定义字符串数组
		String content[] = {"hello","hi","nihao"};
//		创建文件对象
		File file = new File("eee.txt");
		
	
		try {
//			创建FileWriter对象
			FileWriter fw = new FileWriter(file);
			// 将数据存入缓存区
			BufferedWriter bw = new BufferedWriter(fw);
			// 遍历字符串数组
			for(int i=0;i<content.length;i++) {
				// 将数组元素写入磁盘文件中
				bw.write(content[i]);
//				将数组中元素单行显示
				bw.newLine();
			}
			bw.close();
			fw.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		try {
		// 创建一个FileReader类对象,读取文件内容
			FileReader fr = new FileReader(file);
		// 将文件内容读取进缓存区
			BufferedReader br  =new BufferedReader(fr);
			
			String str = null;
			int k =0;
			while((str = br.readLine())!=null) {
				k++;
				// 输出文件数据
				System.out.println("第"+k+"行"+str);
				
			}
			br.close();
			fr.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值