IO流

IO流的概述

操作文件时一定要关闭文件,file.close() ,写入文件最好对文件进行刷新 file.flush();采用自动关闭流的方法较好
IO流的主要作用是用来处理设备之间的数据传输,例如可以使用IO流将一台电脑硬盘里面的照片传输到另一台电脑里面,即将照片转换为字节,然后将字节传到另一台电脑上面,另一台电脑接收后,可以将这些字节还原为照片。

IO的分类

按照流向分类:
				        输入流(InPutStream和Reader):从硬盘或者别的地方读到内存里面
				        输出流(OutPutStream和Writer):从内存向硬盘或者别的地方输出
按照操作类型分类:
					    字节流(InPutStream和OutPutStream):字节流可以操作任何数据类型,因为在计算机中的数据都是以字节的形式存储的。
					    字符流(Reader和Writer):字符流只能操作纯字符数据,防止乱码

InputStream,OutPutStream,Reader和Writer都是抽象类!

在这里插入图片描述

使用FileInputStream和FileOutputStream读取和写入文件

操作结束之后注意关闭流释放资源,这里相当于在内存和硬盘之间放一个梯子,在将硬盘中的文件全部读取到内存之后,需要拿掉梯子,否则会浪费资源

FileInputStream:
								构造方法: FileInputStream(String name)
								对象方法:
												close()  :关闭资源
												
												read() :返回int,当读完返回-1,Reads a byte of data from this input stream.
												
												read(byte[] b): 返回in当读完返回-1t,Reads up to b.length bytes of data  from this input stream into an array of bytes;  
												b:the buffer into which the data is read
												

											   read(byte[] b,int off,int len):返回int,Reads up to len bytes of data from this input stream into an array of bytes; 
											   b:the buffer into which the data is read,off-
												off:the start offset in the destination array b
												len:the maximum number of bytes read




FileOutputStream:
									构造方法:
														FileOutputStream(String name):Creates a file OutPut stream to write to the file with the specifiedname.
													  FileOutputStream(String name,boolean append) :Creats a file output stream to write to the file,append 为true则不覆盖原文内容。

								对象方法:
								·					write(int b):
													write(byte []b):  write b.lengtj bytes from the specified bytes array to this file output stream.
													write(byte[] b ,int off, int len)
package com.chapter8;

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

public class Chapter8 {

	public static void main(String[] args) {
		
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			 fis = new FileInputStream("helloword");
			 //如果文件不存在的话,就会自动创建
			 //如果想要在文件中继续写的话,第二个参数传入true
			 fos = new FileOutputStream("123.txt");
			 
			 
			 //read()方法: 效率低
			 int temp =0;
//			 while((temp=fis.read())!=-1){
//				 System.out.println((char)temp);
//			 }
			 
			 //read(byte[] b)方法
			 //定义byte数组告诉系统一次读取几个字节,减少硬盘盒内存之间通信提高效率
			 byte [] b = new byte[5];
			 //有参的read方法返回的int值是读取了几个字节
			 while((temp=fis.read(b))!=-1) {
				 System.out.println(new String(b,0,temp));
			 }
			 
			 
			 //write方法
			 // write(int)
			 fos.write(98);
			  
			 //write(byte[] b) 方法
			 String msg = "monkey1024";
			 //将byte数组中所有的数据全部写入
			 byte[] bytes =msg.getBytes();
			 //将byte数组中的所有数据全部写入
			 fos.write(bytes);
			 //刷新一下确保数全部写入硬盘
			 fos.flush();
			 
			 
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}

文件copy

package com.chapter8;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
  将helloword中内容考到 hellowordcopy中,先以流的方式将helloword文件读取到内存中,然后再以流的方式将内存中的内容写出到硬盘里面
 */
public class copyTest {

	public static void main(String[] args) {
		
		try(FileInputStream fis = new FileInputStream("helloword");
			FileOutputStream fos = new FileOutputStream("hellowordcopy.txt");	
			) {
			
			int temp = 0;
			byte[] arr = new byte[1024]; //注意读取的字节数,1024
			while((temp=fis.read(arr))!=-1) {
				fos.write(arr); //写入文件
			}
			fos.flush();
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

}

使用缓冲流进行文件拷贝

Java中提供了BufferedInputStream和BufferedInputStream缓冲流来读取和写出
BufferedInputStream读取时会创建一个长度为 8192的 byte类型数组,程序一次读取 8192 个字节数据到数组中 使用缓冲流之后 不用再自定义byte类型数组了。

package com.chapter8;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedCopy {

	public static void main(String[] args) {
		
		try(
				//使用缓冲流装饰一下
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream("helloword"));
				BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("hellowordcopy2.txt"));) {
			
			int temp=0;
			while((temp=bis.read())!=-1) {
				bos.write(temp);
			}
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
	}

}

文件的加密

通过输入流将文件读取到内存里面,可以对这些数据做一些处理,之后再将数据写出到硬盘里面从而达到加密的效果。

package com.chapter8;

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

public class CodeWord {

	public static void main(String[] args) {
		
		try(FileInputStream fis = new FileInputStream("helloword");
			FileOutputStream fos = new FileOutputStream("hellowordcode");	
			) {
			
			int temp = 0;
			while((temp=fis.read())!=-1) {
				//数据异或一个数字进行加密
				fos.write((char)temp^88);
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
	}

}

文件解密
上面的加密操作就是在写出数据之前对数据进行异或操作,利用对一个数进行两次异或的结果就是他本身的整个特点,将待解密的word读取到内存里面,然后在进行异或操作即可。u

字符流FileReader

字符流FileReader主要用来读取字符的IO流,使用字符流读取文本 文件可以解决 乱码问题。

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/*
 读取字符: FileReader
 		     Convenience class for reading character files.
 		用法和 FileInputStream 一样
 */

public class FileReaderTest01 {

	public static void main(String[] args) {
		
		try(FileReader fr = new FileReader("小重山");) {
			
			int temp = 0;
			
			//一次读取一个字符
			System.out.println((char)fr.read()); //昨
			while((temp=fr.read())!=-1) {
			   System.out.println((char)temp);}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

}

使用BufferedReader可以一次读取一行的文字:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderTEST {
	/*BufferedReader
	 		Reads text from a character-input stream,buffering characters so as to provide for the efficient reading of the
	 		
	 		characters,arrays,and lines.
	 		
	 	对象方法:
	 			readLine()  //按行读取,达到文件末尾返回null
	 * 
	*/
	public static void main(String[] args) {
		
		try(BufferedReader br = new BufferedReader(new FileReader("小重山"));) {
			//读取一个字节
			System.out.println((char)br.read());//昨
			
			//读取一行
			System.out.println(br.readLine()); //夜寒蛩不住鸣。
			
			//利用按行读取,读取全文内容
			String s;
			while ((s=br.readLine())!=null) {
				System.out.println(s); 
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
	}

}

字符流FileWriter

使用字符流FileWriter可以解决写出文本文件中乱码的问题

import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {

	public static void main(String[] args) {
		try(FileWriter fw = new FileWriter("中文");) {
			fw.write(97);
			
			fw.write("中文");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

使用字符流拷贝文件

使用字符流拷贝文本文件可以避免文件中的内容乱码,需要注意的是 ***字符流不能拷贝非文本文件,***比如照片。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyTest01 {

	public static void main(String[] args) {
		
		
		try(BufferedReader br = new BufferedReader(new FileReader("小重山"));
			BufferedWriter bw = new BufferedWriter(new FileWriter("小重山copy"));	
				) {
			
			String s ;
			while((s=br.readLine())!=null) {
			    bw.write(s);
			    bw.newLine(); //重写 一行
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

}

Java装饰者设计模式

设计模式简介
设计模式是可以重复利用的解决方案,软件开发的先驱者或前辈们将之前开发中遇到的问题进行总结并给出了解决方案,后辈在遇到这些问题之后直接使用这些方案即可解决问题。

装饰者设计模式

使用场景:
给某个对象的功能进行扩展时,可以考虑使用装饰者模式。

在IO流这部分内容中,JDK源码使用了大量的装饰者设计模式。比如BufferedReader可以对FileReader进行装饰。

装饰者设计模式的优点:
不用修改被装饰对象的源码,装饰者与被装饰者耦合度不高。

以去拉面管吃拉面为例,一碗面做好后都会向里面加料装饰,所以先定义一个装饰拉面的接口,提供一个加料的方法:

package decorator;

/*
 * 
 * 装饰拉面
 */
public interface NoodleDecorate {
	
	//向拉面中加料
	void addThings();
}

创建一个拉面(Noodle)类实现NoodleDecorate接口:

package decorator;

public class Noodle implements NoodleDecorate {

	@Override
	//制作一碗普通拉面
	public void addThings() {
		System.out.println("加两块肉末");
		System.out.println("加两块白菜");
		System.out.println("加三根香菜");	
	}
}

普通的拉面做好之后,有人爱吃辣椒,想在拉面中添加辣椒,创建一个辣椒拉面(ChiliNoodle)类:

package decorator;

public class ChiliNoodle implements NoodleDecorate {
	
	//普通拉面
	private Noodle noodle;
	
	//传入要加辣椒的普通拉面对象,先做一碗拉面然后加入带辣椒的材料
	public ChiliNoodle(Noodle noodle) {
		super();
		this.noodle = noodle;
	}


	@Override
	//对普通拉面进行升级,加入辣椒
	public void addThings() {
		noodle.addThings();
		System.out.println("加三少辣椒");

	}

}

测试一下:

package decorator;

public class NoodleTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Noodle noodle = new Noodle();
		noodle.addThings();
		
		
		//装饰者模式
		ChiliNoodle cn = new ChiliNoodle(new Noodle());
		cn.addThings();
	}

}

在实际生活中你去拉面馆点了一碗拉面,做好之后如果 你爱吃辣椒的话可以向里面加点辣椒,而不是 让厨师重新做一碗辣椒面。

File类简介

File类在Java.io包下,看名字应该可以猜到,这个类跟文件夹的操作有关

构造方法:      File(String pathname)  //创造文件名,或得对象
 
 对象方法: 
 						createNewFile()      //创建空的文件夹或文件,假如已经存在则不创建

						delete()     //删除文件或文件夹
						exists()     //Tests whether the file or directory exits
						getAbsoluteFile()   //获取绝对路径
						isDirectory()  //判断是否为文件夹
						isFile()    //判断是否为文件
						lastModified()  //返回最后修改时间;毫秒值
						length()  //Returns the length of the file denoted by this abstract pathname;获取文件大小。字节数
						list()     //获取文件路径下的所有文件名(同一层级目录)
						list(FilenameFilter filter)  //Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
						listFiles()  :获取文件路径下的所有File对象数组
						mkdir()
						mkdirs()
					

文件过滤器

package com.serilizable;
/*
 * 问题:统计根目录下以.txt结尾的文件数量,并将文件名打印出来
 */
import java.io.File;

public class ObjectInputTEST {
	public static void main(String[] args) {
		
		//methond1();
		methond2();

		
	}

	//去除根目录干扰,较好(关键如何去除目录文件夹的影响)
	private static void methond2() {
		File file1 = new File("D:\\java\\code\\Chapter08");
		File[] fi = file1.listFiles();
		for(File f:fi) {
			String s = f.toString();     //将File类型转换为String类型
			if(s.endsWith(".txt")&&f.isFile()) { //判断其是否以.txt结尾,并判断其是否为文件(去除文件夹的影响)
				System.out.println(s);
			}
		}
	}

	//会将根目录下以.txt结尾的文件夹也统计出来,严格说来不正确
	@Deprecated
	private static void methond1() {
		File file1 = new File("D:\\java\\code\\Chapter08");
		String[] st = file1.list();
		for(String s:st) {
			if(s.endsWith(".txt")) {
				System.out.println(s);
			}
		}
	}
	
	
	
	
	
}

使用文件过滤器对文件进行过滤

package Test01;

import java.io.File;
import java.io.FilenameFilter;

public class Filter implements FilenameFilter {

	@Override
	public boolean accept(File dir, String name) {
		//获取根目录下每个文件的File对象
		File file = new File(dir,name);
		//编写筛选条件
		return file.isFile()&&file.getName().endsWith(".txt");
		
		
	}

}



package Test01;

import java.io.File;

/*
	使用文件过滤器对文件过滤
 */
public class fimleFilter01 {

	public static void main(String[] args) {
		
		File file = new File("D:\\java\\code\\Chapter08");
		//测试文件目录下的每个文件
		Filter filter = new Filter();
		String[] str = file.list(filter);
		
		for(String s:str) {
			System.out.println("该文件夹下的以.txt结尾的文件"+s);
		}
	}

}

练习:输出指定目录下的所有文件

package Test01;

import java.io.File;

//获取指定目录下的所有文件名称
public class Exe03 {
	
	//记录缩进的次数
	private static int count = 0;

	public static void main(String[] args) {
		
		File file = new File("G:\\test");
		
		getFileName(file);
		
	}
//每次调用该方法时,说明进入到一个新的文件夹的内部,需要增加一个缩进
	public static void getFileName(File file) {
		//获取File路径对象中的File数组
		File[] fileArray = file.listFiles();
		//遍历数组,取得File对象
		for(File f:fileArray) { 
			//通过遍历count来控制打印几个缩进
			for(int i =0;i<count;i++) {
				System.out.print("\t");
			}
			//打印文件或文件名
			System.out.println(f);
			//如果是一个文件或文件夹的的话,使用递归重复上面的操作
			if(f.isDirectory()) {
				count ++;
				getFileName(f);
				//方法执行结束后要减少缩进
				count --;
			}
			
		}
	}

}

在这里插入图片描述

序列化和反序列化

序列化与反序列化的作用

在工作中可能遇到多台机器远程通信的情况,如果要将机器A中的某个JAVA对象传输到机器B上面,需要将这个Java对象转换为字节序列化然后进行传输。将对象转换为字节序列的过程叫作序列化,反之叫作反序列化。

一个对象如果支持序列化,需要实现Serializable的接口,这个接口中没有任何办法,实现该接口后,JVM会给这个对象做特殊待遇。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牙科就诊管理系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线查看数据。管理员管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等功能。牙科就诊管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 管理员在后台主要管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等。 牙医列表页面,此页面提供给管理员的功能有:查看牙医、新增牙医、修改牙医、删除牙医等。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。药品管理页面,此页面提供给管理员的功能有:新增药品,修改药品,删除药品。药品类型管理页面,此页面提供给管理员的功能有:新增药品类型,修改药品类型,删除药品类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值