18 IO流

IO流

File类

构造方法:
File(String pathname):将一个路径下的文件/文件夹构造成一个file对象

方法:createNewFile() 
          当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
          
案例:
案例:
package cn.wzx.Demo;

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

public class Demo1 {
	public static void main(String[] args) throws IOException {
		//创建一个文件的对象  ctrl+f
		File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
		//创建
		/*boolean createNewFile = file.createNewFile();
		System.out.println(createNewFile);*/
		//删除
		/*boolean delete = file.delete();
		System.out.println(delete);*/
		//是否存在
		/*boolean exists = file.exists();
		System.out.println(exists);*/
		
		//绝对路径
		/*String absolutePath = file.getAbsolutePath();
		System.out.println(absolutePath);*/
		//获得文件名和后缀名
		/*String name = file.getName();
		System.out.println(name);*/
		//获取父路径
		/*String parent = file.getParent();
		System.out.println(parent);*/
		
		//判断是否是绝对路径
		/*boolean absolute = file.isAbsolute();
		System.out.println(absolute);
		
		//是否为文件夹
		boolean directory = file.isDirectory();
		System.out.println(directory);
		
		//是否是文件
		boolean file2 = file.isFile();
		System.out.println(file2);
		
		//
		long length = file.length();
		System.out.println(length);*/
		
		//创建文件夹
		/*boolean mkdir = file.mkdir();
		System.out.println(mkdir);*/
		
		/*boolean setReadOnly = file.setReadOnly();
		System.out.println(setReadOnly);*/
	}
}

IO流概述

IO: input输入 / output输出

IO流:是连接内存和永久存储设备的一条管道
  		输出流
    ------------>
内存              永久存储设备(硬盘 U盘  光盘  磁带)
	<-------------
	    输入流

IO流的分类

按照流的方向
输入流:将永久存储设备的数据读到程序中(内存)
输出流:将内存中程序的数据存储到永久存储设备(硬盘)
按照流的传输数据单位分
字节流:每次传输的数据最小单位是字节  字节流可以处理任何文件 音乐视频 等
字符流:一个字符等于两个字节          可以处理的文件是 记事本能打开且正确的文件  .txt  .java 
按照流的功能分
节点流:负责传输主要数据的流
过滤流:增强流功能的流
所有流的父类输出流输入流
字节流OutputStreamInputStream
字符流WriterReader

字节流

字节输出流代表(文件输出流FileOutputStream)
方法:
write(int b)
write(byte[] b)
write(byte[] b,int off,int len)
close():关闭流

案例:
package cn.wzx;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo2 {
	public static void main(String[] args) throws Exception {
		//指定硬盘中的一个路径
		File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
		
		//创建一个文件输出流
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		
		//将数据写到文件中
		/*fileOutputStream.write(65);
		fileOutputStream.write(66);
		fileOutputStream.write(67);结果:ABC*/
		
		//字节数组
		/*byte[] b = "sdgahladfgha".getBytes();
		fileOutputStream.write(b);*/
		
		//字节数组 开始  int len
		byte[] b = "abcdefg".getBytes();
		fileOutputStream.write(b, 1, 2); //结果:bc
		
		//关流
		fileOutputStream.close();
		System.out.println("写完了");
	}
}


案例:追加输出数据  
当构造方法的第二参数为true时追加
FileOutputStream fileOutputStream = new FileOutputStream(file,true);
案例:输出换行
不同的系统有不同的写法
windows  \r\n     r return  回车   n newLine 新的一行
unix/linux  \n
macOs   \r
代码:
package cn.wzx;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo2 {
	public static void main(String[] args) throws Exception {
		//指定硬盘中的一个路径
		File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
		
		//创建一个文件输出流
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		
		//将数据写到文件中
		/*fileOutputStream.write(65);
		fileOutputStream.write(66);*/
		
		//字节数组
		/*byte[] b = "sdgahladfgha".getBytes();
		fileOutputStream.write(b);*/
		
		//字节数组 开始  int len
		
		byte[] b = "kashjfgkahjdfg".getBytes();
		for (int i = 0; i < b.length; i++) {
			fileOutputStream.write(b[i]);
			fileOutputStream.write("\r\n".getBytes());
		}
		
		//关流
		fileOutputStream.close();
		System.out.println("写完了");
	}
}

字节输入流(文件字节输入流FileInputStream)
构造方法:
FileInputStream(File file)
FileInputStream(String name) 

方法:
read():从此输入流中读取一个数据字节。
read(byte[] b):每次读一个字节数组,将读到的数据存到数组中,返回值类型是int(读到的数据的长度,就是读到了几个数据)
read(byte[] b,int off,int len)

package cn.wzx;
import java.io.File;
import java.io.FileInputStream;

public class Demo3 {
	public static void main(String[] args) throws Exception{
		//指定硬盘中的一个路径
		File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
		
		//创建输入流对象
		FileInputStream fileInputStream =  new FileInputStream(file);
		int read1 = fileInputStream.read();
		System.out.println((char)read1);//打印流 (输出流)
		int read4 = fileInputStream.read();
		System.out.println(read4);//打印流 (输出流)13  \r
		int read5 = fileInputStream.read();
		System.out.println(read5);//打印流 (输出流)10   \n
		int read6 = fileInputStream.read();
		System.out.println(read6);//打印流 (输出流)-1   
		fileInputStream.close();
	}
}


案例:改造之后的代码

package cn.wzx;

import java.io.File;
import java.io.FileInputStream;

public class Demo3 {
	public static void main(String[] args) throws Exception{
		//指定硬盘中的一个路径
		File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
		
		//创建输入流对象
		FileInputStream fileInputStream =  new FileInputStream(file);
		/*int read1 = fileInputStream.read();
		System.out.println((char)read1);//打印流 (输出流)
		int read4 = fileInputStream.read();
		System.out.println(read4);//打印流 (输出流)
		int read5 = fileInputStream.read();
		System.out.println(read5);//打印流 (输出流)
		int read6 = fileInputStream.read();
		System.out.println(read6);//打印流 (输出流)
		fileInputStream.close();*/
		
		//改造
		int read;
		
		//死循环
		while (true) {
			//判断是否为-1
			read = fileInputStream.read();
			if (read==-1) {
				break;
			}else {
				System.out.print((char)read);
			}
		}
		
		fileInputStream.close();
	}
}

案例:有逼格的代码
		int read;
		while ((read=fileInputStream.read())!=-1) {
			System.out.println((char)read);
		}

案列:
package com.baizhi.entity;

import java.io.File;
import java.io.FileInputStream;

public class TestIOFile2 {
    public static void main(String[] args) throws Exception{
        //指定硬盘的中的一个路径
        File file = new File("E:\\ideaworkspace\\project01\\maven01\\src\\main\\resources\\File\\a.txt");
        //创建节点流
        FileInputStream fis = new FileInputStream(file);
       int r;
        byte[] b = new byte[3];
        while (true){
            r = fis.read(b);
            if(r==-1){
                break;
            }else{
                System.out.println(new String(b,0,r));
            }
        }
        fis.close();
   /*    int r;
        byte[] b = new byte[3];
        while((r=fis.read(b))!=-1){
            System.out.println(new String(b,0,r));
        }
        fis.close();*/
    }
}

文件的复制

1B = 8bit

1024B = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
1024TB = 1PB
1024PB = 1EB
1024EB = 1ZB

案例:
package cn.wzx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Demo4 {
	public static void main(String[] args) throws Exception{
		//源文件的位置
		File yuanFile = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/IO上.wmv");
		//目标位置
		File mubiaoFile = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file1/赵帅私密视频.wmv");
		
		
		//输入流  输出流
		FileInputStream fInputStream = new FileInputStream(yuanFile);
		FileOutputStream fileOutputStream = new FileOutputStream(mubiaoFile);
		
		
		//一边读 一边写
		//第一种    读一个字节  写一个字节
		/*int b;
		
		while (true) {
			b = fInputStream.read();
			if (b==-1) {
				break;
			}else {
				fileOutputStream.write(b);
			}
			
		}*/
		
		//第二种
		int len;
		byte[] b = new byte[1024*1024];
		//long start = System.nanoTime();
		while ((len = fInputStream.read(b))!=-1) {
			fileOutputStream.write(b,0,len);
		}
		l//ong end = System.nanoTime();
		//System.out.println(end-start);
		//关流
		fileOutputStream.close();
		fInputStream.close();
		
		System.out.println("复制完毕");
	}
}

过滤流

1.缓冲流BufferedOutputStream/BufferedInputStream
package cn.wzx;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class Demo5 {
	public static void main(String[] args) throws Exception{
		File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
		
		//创建节点流
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		
		//创建缓冲流 过滤流   
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
		
		bufferedOutputStream.write(65);
		
		//刷新流
		bufferedOutputStream.flush();
		
		bufferedOutputStream.close();//在关流之前会先进行一步刷新流的操作
		
	}
}

2.数据过滤流:可以传输基本的数据类型的数据  8中基本数据类型DataOutputStream DataInputStream
案例:
package cn.wzx;

import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Demo5 {
	public static void main(String[] args) throws Exception{
		File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/b.dat");
		
		//创建节点流
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		
		DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);
		
		dataOutputStream.writeInt(300);
		
		dataOutputStream.flush();
		dataOutputStream.close();
		
		FileInputStream fileInputStream = new FileInputStream(file);
		
		//过滤流
		DataInputStream dataInputStream = new DataInputStream(fileInputStream);
		
		int readInt = dataInputStream.readInt();
		System.out.println(readInt);
	}
}
3.ObjectOutputStream/ObjectInputStream 对象输出/输入流 需实现序列化Serializable接口
序列化:将一个对象写入一个持久存储设备(硬盘)的文件中
反序列化:将一个文件中的对象数据读入内存程序中
transient:修饰的属性不参与序列化,当序列化的时候,这个属性时默认值(即使初始化也是默认值)
案列:
package com.wzx;

import java.io.*;

public class TestIOFile3 {
    public static void main(String[] args) throws Exception {
        Student4 student4 = new Student4("王仨",18,new Address("北京","46131",1101011452L));
        File file = new File("E:\\ideaworkspace\\project01\\maven01\\src\\main\\resources\\File\\a.txt");
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(student4);
        oos.flush();
        oos.close();

        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object o = ois.readObject();
        if(o instanceof Student4){
            Student4 student = (Student4)o;
            System.out.println(student);
        }
        ois.close();
    }
}
class Student4 implements Serializable {
    private String name;
    private int age;
    private Address address;

    public Student4() {
    }

    public Student4(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student4{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }
}
class Address implements Serializable{
    private String city;
    private transient String email;
    private Long telephone;

    public Address() {
    }

    public Address(String city, String email, Long telephone) {
        this.city = city;
        this.email = email;
        this.telephone = telephone;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Long getTelephone() {
        return telephone;
    }

    public void setTelephone(Long telephone) {
        this.telephone = telephone;
    }

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", email='" + email + '\'' +
                ", telephone=" + telephone +
                '}';
    }
}

字符流

编码
编码的由来:
	世界上第一台计算机诞生在美国 发明人是冯诺依曼 现在所使用的电脑都是冯诺依曼结构 埃尼拉克(第一台计算机名字)
	现在在研究 量子计算机
	
ASCII码表  最小的码表
其他的国家也要用
	
	中国
	大陆(简体)
	GB2312(国标) 建国初期天朝政府 的某个部门 指定了码表
	刘-----1234------2345
	GBK(国标扩充)
	两岸 台湾  香港   澳门
	繁体  
	BIG5   
	1234----龍
	
	编码   解码
	
	iso8859-1   欧洲国际的码表
    
    Unicode编码  万国码  UTF-8
    因为 Linux系统采用的UTF-8
    windows  GBK 
	
字符输出流(FileWrite)
write(int a)
write(char[] c)
write(String s)
案例:
package cn.wzx;

import java.io.File;
import java.io.FileWriter;



public class Demo7 {
	public static void main(String[] args) throws Exception{
		File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
		
		//创建节点流
		FileWriter fileWriter = new FileWriter(file,true);
		
		/*fileWriter.write('刘');
		fileWriter.write('张');
		fileWriter.write('赵');
		fileWriter.write('李');*/
		
		/*String string = "今天我又来到你的窗外";
		char[] charArray = string.toCharArray();
		fileWriter.write(charArray);*/
		
		
		String string = "最美的不是下雨天,而是和你一起躲过屋檐";
		fileWriter.write(string);
		
		
		
		
		fileWriter.flush();
		//关流
		fileWriter.close();
		System.out.println("写入完毕");
	}
}

字符输入流(FileReader)
read():每次读一个字符
read(char[] c):每次读字符数组长度,将读到的数据存储到数组中 返回值是每次读到几个字符
案例:
package cn.wzx;

import java.io.File;
import java.io.FileReader;

public class Demo7 {
	public static void main(String[] args) throws Exception{
		File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
		
		//创建节点流
		FileReader fileReader = new FileReader(file);
		
		//方法
		/*int b;
		while (true) {
			b = fileReader.read();
			if (b==-1) {
				break;
			}else {
				System.out.print((char)b);
			}
		}*/
		/*int b;
		while ((b=fileReader.read())!=-1) {
			System.out.println((char)b);
		}*/
		char[] c = new char[5];
		int len;
		while ((len=fileReader.read(c))!=-1) {
			System.out.println(new String(c,0,len));
		}
		
		fileReader.close();
	}
}
字符过滤流
1.BufferedWriter/BufferedReader 具有缓冲的字符过滤流
案例:
package cn.wzx;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class Demo7 {
	public static void main(String[] args) throws Exception{
		File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
		
		//创建节点流
		FileWriter fileWriter = new FileWriter(file);
		
		//过滤流 
		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
		
		//bufferedWriter.write('流');
		
		
		/*char[] c = "真情像梅花开过".toCharArray();
		bufferedWriter.write(c);*/
		
		String string = "世界上头发最多的男人";
		//bufferedWriter.write(string);
		
		bufferedWriter.write(string, 0, 3);
		
		
		bufferedWriter.close();
	}
}

案例:readLine():每次可以读一行
package cn.wzx;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;



public class Demo7 {
	public static void main(String[] args) throws Exception{
		File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
		
		//节点流
		FileReader fileReader = new FileReader(file);
		//过滤流
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		
		
		
		String string = "";
		while (true) {
			string = bufferedReader.readLine();
			if (string==null) {
				break;
			} else {
				System.out.println(string);
			}
		}	
	}
}

2.PrintWriter :可以直接输出一行字符串,不用创建节点流,也具有基本数据类型的功能(将基本数据类型转换为char类型进行输出)
    
package cn.wzx;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;



public class Demo7 {
	public static void main(String[] args) throws Exception{
		File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
		
		PrintWriter printWriter =new PrintWriter(file);
		
		printWriter.println("大将南征胆气豪");
		printWriter.println("腰横秋水雁翎刀");
		printWriter.println("风吹鼍鼓山河动");
        printWriter.println("电闪旌旗日月高");                
		printWriter.close();
		
		FileReader fileReader  = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		String string ;
		while (true) {
			string = bufferedReader.readLine();
			if (string==null) {
				break;
			} else {
				System.out.println(string);
			}
		}
	}
}


3.桥转换流OutputStreamWriter/InputStreamReader
作用:可以将一个字节流转换为一个字符流
package cn.wzx;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintWriter;



public class Demo7 {
	public static void main(String[] args) throws Exception{
		File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
		PrintWriter printWriter = new PrintWriter(file);
		printWriter.print(23);
		printWriter.flush();
		//字节流的节点流
		FileInputStream fileInputStream = new FileInputStream(file);
		InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
		
		int read = inputStreamReader.read();
		System.out.println(read);
		int read1 = inputStreamReader.read();
		System.out.println(read1);
		
		printWriter.close();
		inputStreamReader.close();
	}
}
关于IO流异常的处理
package cn.wzx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;


public class Demo7 {
	public static void main(String[] args){
		File file1 = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
		File file2 = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/b.txt");
		FileOutputStream fileOutputStream  = null;
		FileInputStream fileInputStream = null;
		try {
			fileOutputStream = new FileOutputStream(file2);//节点流
			fileInputStream = new FileInputStream(file1);
			//复制
			int len;
			byte[] b =  new byte[1024*1024];
			
			while ((len = fileInputStream.read(b))!=-1) {
				fileOutputStream.write(b, 0, len);
			}
			System.out.println("复制完毕");
		} catch (Exception e) {
			System.out.println("异常解决了");
			e.printStackTrace();
		}finally{
			try {
				if (fileInputStream!=null&&fileOutputStream!=null) {//避免空指针异常
					fileInputStream.close();
					fileOutputStream.close();
				}
				
			} catch (Exception e2) {
				System.out.println("空指针异常");
			}
		}	
	}
}



基于SSM框架的智能家政保洁预约系统,是一个旨在提高家政保洁服务预约效率和管理水平的平台。该系统通过集成现代信息技术,为家政公司、家政服务人员和消费者提供了一个便捷的在线预约和管理系统。 系统的主要功能包括: 1. **用户管理**:允许消费者注册、登录,并管理他们的个人资料和预约历史。 2. **家政人员管理**:家政服务人员可以注册并更新自己的个人信息、服务类别和服务时间。 3. **服务预约**:消费者可以浏览不同的家政服务选项,选择合适的服务人员,并在线预约服务。 4. **订单管理**:系统支持订单的创建、跟踪和管理,包括订单的确认、完成和评价。 5. **评价系统**:消费者可以在家政服务完成后对服务进行评价,帮助提高服务质量和透明度。 6. **后台管理**:管理员可以管理用户、家政人员信息、服务类别、预约订单以及处理用户反馈。 系统采用Java语言开发,使用MySQL数据库进行数据存储,通过B/S架构实现用户与服务的在线交互。系统设计考虑了不同用户角色的需求,包括管理员、家政服务人员和普通用户,每个角色都有相应的权限和功能。此外,系统还采用了软件组件化、精化体系结构、分离逻辑和数据等方法,以便于未来的系统升级和维护。 智能家政保洁预约系统通过提供一个集中的平台,不仅方便了消费者的预约和管理,也为家政服务人员提供了一个展示和推广自己服务的机会。同时,系统的后台管理功能为家政公司提供了强大的数据支持和决策辅助,有助于提高服务质量和管理效率。该系统的设计与实现,标志着家政保洁服务向现代化和网络化的转型,为管理决策和控制提供保障,是行业发展中的重要里程碑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值