JavaSE - Day21 (IO流及其文件上与下载)

本文详细介绍了Java中的IO流,包括其作用、分类,如字节流和字符流,以及各种流的子类,如FileInputStream、FileOutputStream、BufferedReader和BufferedWriter。还讲解了文件File的相关操作和文件过滤器FileFilter接口的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IO流

作用

        程序与文件传输数据的通道

分类

        按照流向分类:

                输入流

                输出流

        按照最小传输单位分类:

                字节流
                字符流

        按照功能分类

                节点流

                过滤流

字节流

父类:

        InputStream (抽象类)

                read() 

                read(byte[] b)

                read(byte[] b , int off , int len)

                close() 关闭流

        OutputStream(抽象类)

                write()

                write(byte[] b)

                write(byte[] b , int off ,int len)

                flush()

                close()

      

常用子类

        文件流

                作用:读取文件中的内容 或者 给文件中写入内容

                FileInputStream

                FileOutputStream

                

public static void main(String[] args) throws IOException {
		FileInputStream fs = new FileInputStream("D:\\桌面\\Day06.txt");
		FileOutputStream fo = new FileOutputStream("D:\\缓冲流.txt" , true);
		
		BufferedInputStream bi = new BufferedInputStream(fs);
		BufferedOutputStream bo = new BufferedOutputStream(fo);
		
		byte[] b = new byte[1024];
		int len = 0;
		while ((len = bi.read()) != -1) {
			bi.read(b, 0, len);
			bo.write(b);
			bo.flush();
		}
		
		bo.close();
		bi.close();
		System.out.println("缓冲流测试成功,出现乱码问题");
	}

        内存流

                作用: 将数据读取到内存中 或者 从内存中取出数据

                ByteArrayInputStream

                ByteArrayOutputStream

                

	public static void main(String[] args) throws IOException {
		FileInputStream fi = new FileInputStream("D:\\桌面\\Day06.txt");
//		FileOutputStream fo = new FileOutputStream("D:\\内存流.txt");
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		byte[] b = new byte[102]; 
		int len = 0;
		while ((len = fi.read(b)) != -1) {
//			fi.read(buf , 0 , len);
			bo.write(b, 0, len);
			bo.flush();
		}
		bo.close();
		fi.close();
		byte[] array = bo.toByteArray();
		String string = new String(array);
		System.out.println(string);
		System.out.println("内存流测试成功");
	}

        缓冲流

                作用:提高读写效率

                BufferedInputStream

                BufferedOutputStream

public static void main(String[] args) throws IOException {
		FileInputStream fs = new FileInputStream("D:\\桌面\\Day06.txt");
		FileOutputStream fo = new FileOutputStream("D:\\缓冲流.txt" , true);
		
		BufferedInputStream bi = new BufferedInputStream(fs);
		BufferedOutputStream bo = new BufferedOutputStream(fo);
		
		byte[] b = new byte[1024];
		int len = 0;
		while ((len = bi.read()) != -1) {
			bi.read(b, 0, len);
			bo.write(b);
			bo.flush();
		}
		
		bo.close();
		bi.close();
		System.out.println("缓冲流测试成功,出现乱码问题");
	}

        对象流:

                作用:将对象存入文件中 或者 将文件中的对象读取到内存中

                ObjectInputStream

                ObjectOutputStream

                注意:

                        static 修饰的属性不能被序列化

                        版本号除了让程序员之间沟通没任何作用

                        存储对象中所有属性所属的数据类型必须全部实现Serializable接口

                        transient修饰的属性为瞬时属性,不参与序列化

public static void main(String[] args) throws IOException {
		Person person = new Person("张三", "黄种人", 23);
		FileOutputStream fos = new FileOutputStream("D:\\对象流.txt");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		
		
		oos.writeObject(person);
		oos.close();
		System.out.println("对象流测试成功");
	}
}

public class Dog{
	private String name;

	public Dog(String name) {
		super();
		this.name = name;
	}

	public Dog() {
		super();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Dog [name=" + name + "]";
	}
	
	
}
public class Person2 implements Serializable{
	private String name;
	private String sex;
	private transient int age;
	private static Dog dog;
	public Person2(String name, String sex, int age,Dog dog) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.dog = dog;
	}
	
	public Person2() {
		super();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", sex=" + sex + ", dog=" + dog + "]";
	}
}

字符流

        特点: 传入的最小单位是char 字符

        父类:

                Reader(抽象类)

                        read()

                        read(byte[] b)

                        read(byte[] b , int off , int len)

                        close()

                Writer(抽象类)

                        writer()

                        writer(byte[] b)

                        write(byte[] b , int off ,int len)

                        flush()

                        close()

常用子类

        文件流

                FileRead

                FileWiter

public static void main(String[] args) throws IOException {
		FileWriter fw = new FileWriter("D:\\FileWriter.txt");
		fw.write("你好,世界.djhfkh5634");
		fw.flush();
		fw.close();
		System.out.println("废物合计");
		r();
	}
	
	public static void r() throws IOException {
		FileReader fr = new FileReader("D:\\FileWriter.txt");
		FileWriter fw = new FileWriter("D:\\FileWriter2.txt");
		char[] c = new char[100];
		int l  = 0;
		while ((l = fr.read(c)) != -1) {
			fr.read(c , 0, l);
			fw.write(c);fw.flush();
		}
		fw.close();
		fr.close();
		System.out.println("飞洒地方和数据库");
	}

        缓冲流

                BufferedReader

                        特有方法: readLine()

                BufferedWriter

                        特有方法: newLine()

public static void main(String[] args) throws IOException {
//		FileReader reader = new FileReader("D:\\writer.txt");
		FileWriter writer = new FileWriter("D:\\writer.txt");
//		BufferedReader bufferedReader = new BufferedReader(reader);
		BufferedWriter bufferedWriter = new BufferedWriter(writer);
		
		bufferedWriter.write("提高效率的你好,世界,很高兴");
		bufferedWriter.flush();
		bufferedWriter.close();
		System.out.println("成功输出了");
		r();
	}
	
	public static void r() throws IOException {
		FileReader reader = new FileReader("D:\\writer.txt");
		FileWriter writer = new FileWriter("D:\\writer2.txt");
		BufferedReader bufferedReader = new BufferedReader(reader);
		BufferedWriter bufferedWriter = new BufferedWriter(writer);
		char[] b = new char[1024];
		int len = 0;
		while ((len = bufferedReader.read(b)) != -1) {
			bufferedReader.read();
			bufferedWriter.write(b);
			bufferedWriter.append('n');
			bufferedWriter.flush();
		}
		bufferedWriter.close();
		bufferedReader.close();
		System.out.println("chenggong");
	}

        转换流:

                InputStreamReader

                OutputStreamWriter

文件File

/*
 * file 类
 * 	1.createNewFile() // 创建一个新的文件
 * 	2.MKdir() 				//创建一个新目录 MKdirs() 多级文件夹
 * 	3.Delete() 				//删除一个文件 或者 空目录
 * 	4.Exists() 				//判断file对象所代表的对象是否存在
 * 	5.getAbsolutePath() 	//获取文件的据对路径
 * 	6.getName() 			//获取文件名字
 * 	7.getParent()	 		//获取文件或者目录所在的目录
 * 	8.isDerectory() 		//是否是目录
 * 	9.isFile() 				//是否是文件
 * 	10.length() 			//获取长度
 * 	11.listFiles() 			//列出目录中所有内容
 * 	12.renameTo() 			//修改文件名为
 */
public class Demon04 {
	public static void main(String[] args) {
		File file = new File("F:\\JavaEE2205class\\资料\\html\\Day05\\视频");
		File[] listFiles = file.listFiles();
		for (File file2 : listFiles) {
			System.out.println(file2);
			
		}
		String name = file.getName();
		System.out.println(name + "555");
		
//		boolean mkdir = file.mkdir();
		long length = file.length();
		System.out.println(length);
		
		
	}
	
	public void bl() {
		File file = new File("D:\\");
		file.listFiles(new FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				if (pathname.getName().endsWith("txt")) {
					return true;
				}
				return false;
			}
		});
		
	}
}

文件过滤器

FileFilter (接口)

public void bl() {
		File file = new File("D:\\");
		file.listFiles(new FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				if (pathname.getName().endsWith("txt")) {
					return true;
				}
				return false;
			}
		});
		
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值