Io流

Io流

1 File文件
1.1 概念:File对象是将平时所用到的关于文件相关的操作进行的封装,方便使用
递归操作:自己调用自己,要求必须要有结束的条件
package cn.yunhe.file;

import java.io.File;
import java.io.IOException;

public class FileDemo {

	public static void main(String[] args) throws IOException {
		test("D:\\Java课堂资料\\资料");
		test("D:\\Java课堂资料\\资料\\笔记");
	}
	
	public static void test(String path) {
		File file = new File(path);
	}
	
	/**
	 * 递归(方法自身调用自身)
	 * 递归一定要有出口
	 * @param path
	 */
	public static void getFileListMethod(String path) {
		File file = new File(path);
		File[] files = file.listFiles();
		//因为可能存在某些文件夹是空的,此时获取到的文件列表对象就是null对象
		if(files != null) {
			for(int i=0;i<files.length;i++) {
				//判断当前的文件对象是否是文件
				if(files[i].isFile()) {
					System.out.println(files[i].getName());
				}else {//该文件是目录
					String filePath = files[i].getPath();
					getFileListMethod(filePath);
				}
			}
		}
	}
	
	/**
	 * 获取指定目录下的第一级文件名列表
	 */
	public static void getFileList() {
		File file = new File("D:/Java课堂资料/资料");
		//获取指定目录下的所有文件的抽象路径名
		String[] fileList = file.list();
		for(int i=0;i<fileList.length;i++) {
			System.out.println(fileList[i]);
		}
	}
	
	/**
	 * 判断文件类型及权限
	 */
	public static void powerMethod() {
		File file = new File("E:/createFile.txt");
		System.out.println("判断文件是否可读:"+file.canRead());
		System.out.println("判断文件是否可写:"+file.canWrite());
		System.out.println("判断是否是目录:"+file.isDirectory());
		System.out.println("判断是否是文件:"+file.isFile());
	}
	
	/**
	 * 创建目录
	 */
	public static void createFileMethod() {
		//创建目录
		File file = new File("E:/file/test");
		//创建单级目录
		//file.mkdir();
		//创建多级目录
		file.mkdirs();
	}
	
	/**
	 * 创建文件
	 * @throws IOException
	 */
	public static void createMethod() throws IOException {
		//创建文件对象,需要指定文件所在的路径
		File file = new File("E:/createFile.txt");
		//判断文件是否已存在
		if(file.exists()) {
			//删除文件
			file.delete();
		}
		//创建文件
		file.createNewFile();
	}
}
2 IO流
2.1 IO流分字节流和字符流,字符流是由字节流组成的,最小单位是b(字节)
2.2常用流:

InputStream OutputStream FileInputStream FileOutputStream

Reader Writer FileReader FileWriter

2.3
  • 不要重复造轮子
  • 框架的出现就是为了能让功能实现起来更简单方便
  • 字符流中的两个超类
  • Reader Writer
  • 跟文件相关的两个子类
  • FileReader FileWriter
  • 字节流底层使用的是字节
  • 字符流使用的字符
2.4 字符流写功能,字符流在写的时候一定要进行刷新才会被保存
package cn.yunhe.io;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;


public class ReadDemo {
	
	public static void main(String[] args) throws IOException {
		readMethod2();
	}
	
	/**
	 * 使用缓冲池读取
	 * 注意:使用字符数组作为缓冲池进行读取时,会出现显示多余或不完整的情况
	 * @throws IOException
	 */
	public static void readMethod2() throws IOException {
		Reader reader = new FileReader("E:/writer.txt");
		char[] cbuf = new char[3];
		while(reader.read(cbuf)!=-1) {
			System.out.println(new String(cbuf));
		}
		reader.close();
	}
	
	/**
	 * 单个字符读取
	 * @throws IOException
	 */
	public static void readMethod() throws IOException {
		Reader reader = new FileReader("E:/writer.txt");
		//System.out.println((char)reader.read());
		//System.out.println((char)reader.read());
		int temp = 0;
		while((temp=reader.read())!=-1) {
			System.out.println((char)temp);
		}
		reader.close();
	}

	/**
	 * 字符流写功能,字符流在写的时候一定要进行刷新才会被保存
	 * 默认,在每次写的时候都是从头开始写
	 * @throws IOException
	 */
	public static void writeMethod() throws IOException {
		//指定要写的位置,包装成字符流
		Writer writer = new FileWriter("E:/writer.txt",true);
		writer.write(97);
		writer.write("张三");
		//writer.flush();
		writer.write(98);
		writer.write("大张飞");
		//writer.flush();
		writer.close();
	}
}


package cn.yunhe.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

/**
 * 字符流缓冲对象 BufferedReader
 */
public class BufferedReaderDemo {
	
	public static void main(String[] args) throws IOException {
		bufferedWriterMethod();
	}

	/**
	 * 缓冲流写
	 * @throws IOException
	 */
	public static void bufferedWriterMethod() throws IOException {
		Writer writer = new FileWriter("E:/writerCopy.txt",true);
		BufferedWriter bw = new BufferedWriter(writer);
		bw.write("hahahahha呵呵呵呵");
		bw.close();
		writer.close();
	}
	
	/**
	 * 字符缓冲对象读取
	 * @throws IOException
	 */
	public static void bufferedReaderMethod() throws IOException {
		//创建字符流读取对象-需要文件路径
		Reader reader = new FileReader("E:/writer.txt");
		//创建字符流缓冲对象-需要一个字符流读对象
		BufferedReader br = new BufferedReader(reader);
		String result = null;
		//按行读取
		while((result = br.readLine()) != null) {
			System.out.println(result);
		}
		//关闭流对象--倒着关
		br.close();
		reader.close();
	}
}

package cn.yunhe.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {

	public static void main(String[] args) throws IOException {
		inputStreamMethod2();
	}
	
	/**
	 * 需求:将指定文件的内容读取出来在控制台打印
	 * 
	 * FileInputStream()
	 * 	1、以字符串的形式直接给地址
	 * 	2、通过File对象给地址
	 * 两种方案的区别:各自的作用及特点
	 * 	如果是使用的File对象,那可以在转为流之前对该文件做相关的处理(增删改查)
	 * 
	 * 一个字母或一个数字占一个字节,一个中文占两个字节
	 * @throws IOException 
	 */
	public static void inputStreamMethod() throws IOException {
		//将地址包装成流对象
		InputStream is = new FileInputStream("E:/stream.txt");
		int temp = 0;
		//读取内容
		while((temp = is.read()) != -1) {
			System.out.println((char)temp);
		}
		//关闭流
		is.close();
	}
	
	/**
	 * 使用缓冲区读
	 * 1024B=1KB
	 * 1024KB = 1M
	 * @throws IOException
	 */
	public static void inputStreamMethod2() throws IOException {
		//将地址包装成流对象
		InputStream is = new FileInputStream("E:/stream.txt");
		//先定义一个缓冲区,流是先将内容读取到了缓冲区中
		byte[] bcuf = new byte[1024];
		//available()拿到整个文档的大小--这种方式不合适
		//byte[] bcuf = new byte[is.available()];
		//读内容
		while(is.read(bcuf) != -1) {
			System.out.println(new String(bcuf));
		}
		//关闭流
		is.close();
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值