Date类|File类|IO

Date类

导包: java.util.Date 类 Date 表示特定的瞬间,精确到毫秒。

public class DateDemo01 {
	public static void main(String[] args) {
		Date date=new Date();
		System.out.println(date);
		//获取毫秒数
		System.out.println(date.getTime());  
		
		Date date2=new Date(1568859681301L);
		System.out.println(date2);
		
		/*
		 *   boolean after(Date when) 
			          测试此日期是否在指定日期之后。 
			 boolean before(Date when) 
			          测试此日期是否在指定日期之前。 
		 */
		System.out.println(date.after(date2));
		System.out.println(date.before(date2));
		System.out.println(date.toString());
	}
}

日期格式转换类 Date String

  • SimpleDateFormat 转换器

    可以指定转换格式 || 默认格式
    转换:
    format(date) 日期对象转为字符串
    parse(str) 字符串转为日期对象

占位:
y 年
M 月
d 日

H 24小时制
h 12小时制
m 分钟
s 秒
S 毫秒

public class FormartDemo02 {
	public static void main(String[] args) throws ParseException {
		//默认转换格式
		/*SimpleDateFormat simple=new SimpleDateFormat();
		//日期对象抓换字符串:使用默认格式
		System.out.println(simple.format(new Date()));  //19-9-19 下午07:29
		System.out.println(simple.parse("19-9-29 下午07:20"));;*/
		
		//指定转换格式
		SimpleDateFormat simple=new SimpleDateFormat("yyyy/MM/dd  hh:mm:ss SSS");
		//日期对象抓换字符串:使用默认格式
		System.out.println(simple.format(new Date()));  //19-9-19 下午07:29
		System.out.println(simple.parse("2019/9/29  07:20:30 111"));;
	}
}

File 类

文件或者路径的一个抽象表示形式
File类常用方法:

public class FileDemo01 {
		public static void main(String[] args) {
				/*File file=new File("D:/haha.txt");
				File file2=new File("D:/iphone11.txt");
				File file3=new File("D:/haha");
				File file4=new File("heihei.txt");
				File file5=new File("D:/");
				File file6=new File("D:/hehe.txt");
				File file7=new File("D:/haha/","hehe/h.txt");
				/*
				 boolean canExecute() 
				          测试应用程序是否可以执行此抽象路径名表示的文件。 
				 boolean canRead() 
				          测试应用程序是否可以读取此抽象路径名表示的文件。 
				 boolean canWrite() 
				 boolean setReadOnly()  
				 */
				
				file.setReadOnly();
				System.out.println(file.canWrite());
				//createNewFile() 创建文件
				System.out.println(file2.createNewFile());
				
				//boolean delete() 删除此抽象路径名表示的文件或目录。 
				System.out.println(file.delete());
				
				// boolean exists()  测试此抽象路径名表示的文件或目录是否存在。 
				System.out.println(file.exists());  //false
				System.out.println(file2.exists()); //true
				System.out.println(file3.exists()); //true
				
				/*
				 * File getAbsoluteFile() 
				          返回此抽象路径名的绝对路径名形式。 
				   String getAbsolutePath() 
				          返回此抽象路径名的绝对路径名字符串。 
				 */
				System.out.println(file2.getAbsoluteFile());
				System.out.println(file2.getAbsolutePath());
				System.out.println(file4);
				System.out.println(file4.getAbsoluteFile().createNewFile());
				
				// long getFreeSpace()  返回此抽象路径名指定的分区中未分配的字节数。 
				System.out.println(file5.getFreeSpace());
				
				//String getName()  返回由此抽象路径名表示的文件或目录的名称。 
				System.out.println(file2.getName()); //不包括路径,包括文件和文件夹的名字及后缀
				
				//String getParent()  返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。 
				//File getParentFile() 
				System.out.println(file2.getParent());
				System.out.println(file2.getParentFile());
				
				/*
				 *  boolean isDirectory() 
				          测试此抽象路径名表示的文件是否是一个目录。 
				 	boolean isFile() 
				          测试此抽象路径名表示的文件是否是一个标准文件。 
				 */
				System.out.println(file3.isDirectory());  //true
				System.out.println(file3.isFile()); //false
				
				/*
				 *  long lastModified() 最后以此被修改的时间,返回毫秒数
		 
				 */
				System.out.println(new SimpleDateFormat().format(new Date(file6.lastModified())));
				/*
				 *  String[] list()  
				 *  File[] listFiles()  
				 */
				String[] arr=file3.list();
				File[] arr2=file3.listFiles();
				System.out.println(Arrays.toString(arr+"-----"));
				System.out.println(Arrays.toString(arr2));
				
				/*
				 [] listRoots() 
				          列出可用的文件系统根。 
				 boolean mkdir() 
				          创建此抽象路径名指定的目录。 
				 boolean mkdirs() 
				          创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。 
				 */
				System.out.println(file7);
				if(!file7.exists()){
					
					System.out.println(file7.getParent());
					System.out.println(file7.getParentFile().mkdirs());
				}
				System.out.println(file7);
		}
	}

IO流

目的: 操作文件内部的内容 读文件内部的数据 往文件中写入内容
途径: 写入写出,上传下载

流: 一连串流动的数据,以先进先出的方式流动–管道
数据源 —>目的地 以程序为中心

流的分类:

  • 流向:

     输入流 
     输出流
    
  • 操作单元:

     字节流   ***** : 万能流
     字符流 :纯文本
    
  • 功能:

     节点流 : 从数据源直接到目的地,实现基本传输功能的
     功能流 :提高增强节点流的功能|性能
    

字节流
字节输入流
字节输出流

  • 字节流输入:
    InputStream 抽象父类 FileInputStream 具体子类可以实例化
    一个字节一个字节读取 返回读入的内容 如果没有了返回-1
    实现字节流的读入数据,重复的读取,非每次读取都需要手动
    每次只传输一个字节的数据效率相对较低,每次传输一卡车的字节数据,车->字节数组

  • 字节流输出:
    字节输出流 OutputStream 此抽象类是表示输出字节流的所有类的超类。
    write()… + 刷出flush() + close()
    默认会覆盖目的地源文件中的内容,如果想要追加

文件拷贝样例
1.选择流
文件字节输入流
文件字节输出流
2. 读入 写出
3. 刷出
4. 关闭 (后打开的先关闭)

	public class CopyFile05 {
		public static void main(String[] args) {
			 //1.选择流
			InputStream is=null;
			OutputStream os=null;
			 try {
				is=new FileInputStream("D:/test.txt");//文件字节输入流  
				os=new FileOutputStream("D:/lalala/test2.txt",true); //文件字节输出流 //true文件追加
				 //2. 读入  写出
				byte[] car=new byte[1024];
				int len=-1; //存储每次读入到字节数组中数据的个数
				while(-1!=(len=is.read(car))){
					os.write(car, 0, len);
				}
				 //3. 刷出
				os.flush();
				 
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally{
				//4. 关闭  (后打开的先关闭)
				try {
					if(null!=os){
						os.close();
					}
					if(null!=is){
						is.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			 
		}
	}

字符流
操作纯文本的内容…
字符输入流 Reader 用于读取字符流的抽象类
FileReader read() … + close()
字符输出流 Writer
FileWriter write() … + append() + flush() + close()

public class CharIODemo01 {
	public static void main(String[] args) throws IOException {
		//字符文件输入流
		Reader read=new FileReader("D:/first.txt");
		//字符文件输出流
		Writer write=new FileWriter("D:/houhou.txt",true);
		
		//读入
		/*int num=read.read();
		System.out.println((char)num);
		System.out.println((char)read.read());
		System.out.println((char)read.read());*/
		//读一个字符数组的数据
		char[] car=new char[1024];
		/*int len=read.read(ch);
		System.out.println(len);
		System.out.println(Arrays.toString(ch));
		System.out.println(new String(ch,0,len));*/
		
		//写出
		//write.write(98);
		//write.write("哈哈哈!!");
		
		int len=-1;
		while((len=read.read(car))!=-1){
			write.write(car, 0, len);
		}
		
		//刷出
		write.flush();
		//关闭
		write.close();
		read.close();
	}
}

功能流(节点流)

  • 字节缓冲流

缓冲流:提供节点流的功能|性能 (加快读写效率)
如果用到节点流,就可以选择性是的使用缓冲流包裹

BufferedInputStream BufferedOutputStream
无新增方法可以多态调用
BufferedReader BufferedWriter

public class BufferedDemo01 {
	public static void main(String[] args) throws IOException {
		//选择流
		//BufferedInputStream(InputStream in) 
		InputStream is=new BufferedInputStream(new FileInputStream("D:/first.txt"));
		OutputStream os=new BufferedOutputStream(new FileOutputStream("D:/dabai.txt"));
		//操作
		byte[] car=new byte[1024];
		int len=-1;
		while((len=is.read(car))!=-1){
			os.write(car, 0, len);
		}
		//刷出
		os.flush();
		//关闭
		os.close();
		is.close();
		
	}
}
  • 字符缓冲流
    BufferedReader BufferedWriter 字符流缓冲流
    新增方法:
    String readLine() 读取一个文本行。
    void newLine() 写入一个行分隔符。
    不能使用多态调用

     public class BufferedDemo02 {
     	public static void main(String[] args) {
     		//1.选择流
     		BufferedReader br=null;
     		BufferedWriter bw=null;
     		
     		try {
     			br=new BufferedReader(new FileReader("D:/first.txt"));
     			bw=new BufferedWriter(new FileWriter("D:/haha.txt"));
     			//读写
     			String msg=null;
     			while((msg=br.readLine())!=null){
     				bw.write(msg);
     				bw.newLine(); //换行
     			}
     			//刷出
     			bw.flush();
     		} catch (FileNotFoundException e) {
     			// TODO Auto-generated catch block
     			e.printStackTrace();
     		} catch (IOException e) {
     			// TODO Auto-generated catch block
     			e.printStackTrace();
     		} finally{
     			try {
     				if(bw!=null){
     					bw.close();
     				}
     				if(br!=null){
     					br.close();
     				}
     			} catch (IOException e) {
     				// TODO Auto-generated catch block
     				e.printStackTrace();
     			}
     		}
     	}
     }
    
  • 基本数据类型流 Data流

    是字节流的功能流
    基本数据类型+String
    DataInputStream 新增方法: readXxx()
    DataOutputStream 新增方法: writeXxx()
    异常:
    EOFException:文件存在,内容读取不到–>操作必须是源文件

        public class DataDemo01 {
     	public static void main(String[] args) throws IOException {
     		//write("D:/hehe.txt");
     		read("D:/hehe2.txt");
     	}
     	
     	//读入
     	public static void read(String path) throws IOException{
     		DataInputStream is=new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
     		//读写顺序保持一致
     		int i=is.readInt();
     		boolean flag=is.readBoolean();
     		String s=is.readUTF();
     		System.out.println(i+"->"+flag+"->"+s);
     		is.close();
     	}
     	//写出
     	public static void write(String path) throws IOException{
     		DataOutputStream out=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
     		out.writeInt(121);
     		out.writeBoolean(false);
     		out.writeUTF("haha");
     		out.flush();
     		out.close();
     	}
     
     }
    
  • 对象流
    对象流 : 数据+类型
    序列化: 就把对象的状态转为可存储或者可传输的状态的过程
    序列化输出流 ObjectOutputStream()
    反序列化输入流 ObjectInputStream()
    新增方法–> readObject() writeObject() 不能多态使用

    先序列化->后反序列化
    注意:
    不是所有的类都能够序列化 实现java.io.Serializable接口
    读写的顺序要保持一致
    不是所有的属性都需要序列化 transient关键字修饰
    静态的内容不能序列化
    如果父类有实现了Serializable,子类可以所有成员进行序列化
    如果子类实现了父类没有实现,子类只能序列化自己独有的内容

     public class ObjectDemo01 {
     	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
     		out("D:/bighammer.txt");
     		
     		in("D:/bighammer.txt");
     	}
     	
     	//反序列化输入流
     	public static void in(String path) throws FileNotFoundException, IOException, ClassNotFoundException{
     		//选择流
     		ObjectInputStream is=new ObjectInputStream(new BufferedInputStream(new FileInputStream(path)));
     		//读取
     		Object obj=is.readObject();
     		int[] arr=(int[]) is.readObject();
     		System.out.println(obj);
     		System.out.println(Arrays.toString(arr));
     		//关闭
     		is.close();
     		
     	}
     
     	//序列化输出流
     	public static void out(String path) throws FileNotFoundException, IOException{
     		//选择流
     		ObjectOutputStream os=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
     		//写出
     		Person p=new Person(01,"赵六",18);
     		os.writeObject(p);
     		os.writeObject(new int[]{1,2,3,4});
     		
     		//刷出
     		os.flush();
     		//关闭
     		os.close();
     		
     		p.setId(10000);
     		
     	}
     }
     
     class Person implements Serializable{
     	private static int id;
     	private transient String name;
     	private int age;
     	
     	public Person() {
     		// TODO Auto-generated constructor stub
     	}
     
     	public Person(int id, String name, int age) {
     		super();
     		this.id = id;
     		this.name = name;
     		this.age = age;
     	}
     
     	public int getId() {
     		return id;
     	}
     
     	public void setId(int id) {
     		this.id = id;
     	}
     
     	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;
     	}
     
     	@Override
     	public String toString() {
     		return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
     	}
     	
     }
    

总结:

  • File : 在java中表示存在或不存在的文件|目录

    方法->都是操作文件外部的内容,如果想要操作文件内部的内容,需要通过流

  • IO -> 流 :上传下载 读入写出

    管道–>传出资源数据

分类:

  1. 字节流: ***

    InputStream| OutputStream 字节流的超类 抽象类 具体使用子类
    节点流: FileInputStream 文件字节输入流 ByteArrayInputStream 字节数组输入流
    FileOutputStream 文件字节输出流 ByteArrayOutputStream 字节数组输出流

    缓冲流: BufferedInputStream BufferedOutputStream
    基本数据类型流: Data流 数据+基本数据类型|String
    对象流 : Object流 数据+数据类型

  2. 字符流:

    Reader | Writer 字符流的父类
    节点流: FileReader FileWriter
    缓冲流: BufferedReader BufferedWriter
    转换流: InputStreamReader InputStreamWriter 字节转为字符流

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值