黑马程序员-----Java基础-----IO流(三)

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

                         IO流(三)

Ⅻ 复制单级文件夹

 

<pre name="code" class="java">public class StreamTest4 {
	
	public static void main(String[] args) throws IOException {
		
		// 封装C:\\course文件夹为一个File对象
		File srcFile = new File("C:\\course");
		
		// 封装E:\\course文件夹为一个File对象,判断其是否存在,如果不存在就创建一个文件夹
		File destFile = new File("E:\\course") ;
		
		// 判断其是否存在
		if(!destFile.exists()){
			destFile.mkdir() ;
		}
		
		copyFile(srcFile , destFile) ;
		
	}
	private static void copyFile(File srcFile, File destFile) throws IOException {
		
		// 获取C:\\course对应的File的所有的文件的一个File数组
		File[] files = srcFile.listFiles() ;
		
		// 遍历数组
		for(File file : files){
			
			// 获取当前遍历的文件对应的名称
			String srcFileName = file.getName() ;
			
			// 构建一个新的File对象
			File destResultFile = new File(destFile , srcFileName) ;
			
			// 复制文件
			copy(file , destResultFile) ;
			
		}
		
	}
	private static void copy(File file, File destResultFile) throws IOException {
		
		// 创建高效字节输入流和字节输出流对象
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)) ;
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destResultFile)) ;
	
		// 一次读取一个字节数组复制文件
		byte[] bytes = new byte[1024] ;
		int len = 0 ;
		while((len = bis.read(bytes)) != -1){
			bos.write(bytes, 0, len) ;
		}
		
		// 释放资源
		bos.close() ;
		bis.close() ;
	}
}


 

XIII 复制指定目录下指定后缀名的文件并修改名称

public class StreamTest5 {
	
	public static void main(String[] args) throws IOException {
		
		// 封装C:\\demo路径为一个File对象
		File srcFile = new File("C:\\demo") ;
		
		// 封装E:\\demo路劲为一个File对象,
		File destFile = new File("E:\\demo") ;
		
		// 然后判断是否存在,如果不存在就创建一个文件夹
		if(!destFile.exists()){
			destFile.mkdir() ;
		}
		
		// 获取a中的所有的以.java结尾的文件数组
		File[] files = srcFile.listFiles(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return new File(dir , name).isFile() && name.endsWith(".java");
			}
		}) ;
		
		// 遍历数组进行复制文件
		for(File file : files) {
			
			// 获取当前遍历的文件对应的名称
			String srcFileName = file.getName() ;
			
			// 构建一个目标文件
			File destResultFile = new File(destFile , srcFileName) ;
			
			// 复制
			copy(file , destResultFile) ;
		}
		// 重命名
		// 获取b中对应的文件的File数组
		File[] resultFiles = destFile.listFiles() ;
		
		// 遍历
		for(File file : resultFiles){
			
			// 获取当前遍历的文件对应的名称
			String currentFileName = file.getName() ;
			currentFileName = currentFileName.replace("java", "jad") ;
			
			// 构建目标文件
			File resultFile = new File(destFile , currentFileName) ;
			
			// 重命名
			file.renameTo(resultFile) ;
		}
		
	}


XIV 键盘录入3个学生信息(姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)),按照总分从高到低存入文本文件

 

public class StreamTest5 {
	
	public static void main(String[] args) throws IOException {
		
		// 封装C:\\demo路径为一个File对象
		File srcFile = new File("C:\\demo") ;
		
		// 封装E:\\demo路劲为一个File对象,
		File destFile = new File("E:\\demo") ;
		
		// 然后判断是否存在,如果不存在就创建一个文件夹
		if(!destFile.exists()){
			destFile.mkdir() ;
		}
		
		// 获取a中的所有的以.java结尾的文件数组
		File[] files = srcFile.listFiles(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return new File(dir , name).isFile() && name.endsWith(".java");
			}
		}) ;
		
		// 遍历数组进行复制文件
		for(File file : files) {
			
			// 获取当前遍历的文件对应的名称
			String srcFileName = file.getName() ;
			
			// 构建一个目标文件
			File destResultFile = new File(destFile , srcFileName) ;
			
			// 复制
			copy(file , destResultFile) ;
		}
		// 重命名
		// 获取b中对应的文件的File数组
		File[] resultFiles = destFile.listFiles() ;
		
		// 遍历
		for(File file : resultFiles){
			
			// 获取当前遍历的文件对应的名称
			String currentFileName = file.getName() ;
			currentFileName = currentFileName.replace("java", "jad") ;
			
			// 构建目标文件
			File resultFile = new File(destFile , currentFileName) ;
			
			// 重命名
			file.renameTo(resultFile) ;
		}
		
	}

	private static void copy(File file, File destResultFile) throws IOException {
		
		// 创建高效字节输入流和字节输出流对象
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)) ;
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destResultFile)) ;
		
		// 频繁的读写操作
		// 一次读取一个字节数组复制文件
		byte[] bytes = new byte[1024] ;
		int len = 0 ;
		while((len = bis.read(bytes)) != -1){
			bos.write(bytes, 0, len) ;
		}
		// 释放资源
		bos.close() ;
		bis.close() ;
	
	}
}


XV 使用Reader自定义类,模拟BufferedReader的readLine()功能并测试

LineNumberReader的概述和使用

(不作为重点)

 

9.数据输入输出流流

数据输入输出流的概述和使用

  

public class DataStreamDemo {
	
	public static void main(String[] args) throws IOException {
		
		read() ;
		
	}
	
	private static void read() throws IOException {

		DataInputStream dis = new DataInputStream(new FileInputStream("dataStream.txt")) ;
		
		// 读取数据
		int a = dis.readInt() ;
		float f = dis.readFloat() ;
		String s = dis.readUTF() ;
		
		// 释放资源
		dis.close() ;
		
		// 输出
		System.out.println(a);
		System.out.println(f);
		System.out.println(s);
		
	}

	private static void write() throws IOException {
		
		DataOutputStream dos = new DataOutputStream(new FileOutputStream("dataStream.txt")) ;
		
		// 写数据
		dos.writeInt(100) ;
		dos.writeFloat(23.45f) ;
		dos.writeUTF("中国") ;
		
		// 释放资源
		dos.close() ;
	}

}

10.打印流

 

Ⅰ 字节流打印流(PrintStream)
  字符打印流(PrintWriter)

  

打印流的特点
    只能操作目的地,不能操作数据源。
    可以操作任意类型的数据。
    如果启动了自动刷新,能够自动刷新。
    可以操作文件的流

 

ⅡPrintWriter实现自动刷新和换行

public class PrintWriterDemo2 {
	
	public static void main(String[] args) throws IOException {
		
		// 创建对象
        //PrintWriter pw = new PrintWriter("autoFlush.txt") ;
		
		// 启用自动刷新
		PrintWriter pw = new PrintWriter(new FileWriter("autoFlush.txt"), true) ;
		
		pw.println(true) ;
		pw.println(100) ;
		pw.println("中国") ;
 		
		// 释放资源
		pw.close() ;
		
	}
}


Ⅲ打印流复制文本文件

 

11标准输入输出流

 

Ⅰ概述

 

System类中的字段:in,out。
它们各代表了系统标准的输入和输出设备。
默认输入设备是键盘,输出设备是显示器。
System.in的类型是InputStream.
System.out的类型是PrintStream是OutputStream的孙子类FilterOutputStream 的子类.

Ⅱ三种方式实现键盘录入

 

 a: Scanner 

   Scanner sc = new Scanner(System.in) ;

 b: main方法

   java HelloWorld hello world java

 c: 使用BufferedReader通过转换流对System.in这个字节流进行转换

 

12 序列化流和反序列化流的概述和使用

 

Ⅰ序列化流的概述


 所谓的序列化就是把对象通过流的方式存储到文件中.
 反序列化就是把文件中存储的对象以流的方式还原成对象
 ObjectInputStream和ObjectOutputStream

 

 我们使用序列化流在写对象时候,要求这个元素必须实现Serializable

 如果以后看到某一个接口没有方法,那么这样的接口就被称之为标记接口 


public class DataStreamDemo {
	
	public static void main(String[] args) throws IOException {
		
		read() ;
	}
	private static void read() throws IOException {

		DataInputStream dis = new DataInputStream(new FileInputStream("dataStream.txt")) ;
		
		// 读取数据
		int a = dis.readInt() ;
		float f = dis.readFloat() ;
		String s = dis.readUTF() ;
		
		// 释放资源
		dis.close() ;
		
		// 输出
		System.out.println(a);
		System.out.println(f);
		System.out.println(s);


 

Ⅱ解决序列化时候的黄色警告线问题 使用transient关键字声明不需要序列化的成员变量

 

13 Properties集合

 

Ⅰ Properties 类表示了一个持久的属性集。
  Properties 可保存在流中或从流中加载。
  属性列表中每个键及其对应值都是一个字符串。

 

public class PropertiesDemo {
	
	public static void main(String[] args) {
		
		// 创建对象
		Properties prop = new Properties() ;
		
		// 使用put方法添加元素
		prop.put("张三", "20") ;
		prop.put("李四", "23") ;
		
		// 遍历
		Set<Object> keySet = prop.keySet() ;
		
		// 遍历
		for(Object obj : keySet){
			
			// 通过键获取值
			Object value = prop.get(obj) ;
		
			// 输出
			System.out.println(obj + "----" + value);
			
		}
	}
}

Ⅱ Properties的特殊功能
   

   public Object setProperty(String key,String value)
   public String getProperty(String key)
   public Set<String> stringPropertyNames()

public class PropertiesDemo2 {
	
	public static void main(String[] args) {
		
		// 创建对象
		Properties prop = new Properties() ;
		
		// 添加元素
		prop.setProperty("乔峰", "阿珠") ;
		prop.setProperty("许仙", "白娘子") ;
		

		Set<String> names = prop.stringPropertyNames() ;
		for(String name : names){
			
			// 获取值
			String value = prop.getProperty(name) ;
			
			// 输出
			System.out.println(name + "----" + value);
		}
	}
}

Ⅲ Properties的load()和store()功能
   public void load(Reader reader)
   public void store(Writer writer,String comments)
public class PropertiesDemo3 {
	
	public static void main(String[] args) throws IOException {
		
//		store();
		load() ;
		
	}

	private static void load() throws FileNotFoundException, IOException {
		
		// 创建Properties对象
		Properties prop = new Properties() ;
		
		// 调方法加载数据
		prop.load(new FileReader("names.properties")) ;
		
		// 遍历
		Set<String> names = prop.stringPropertyNames() ;
		
		for(String name : names){
			
			// 获取值
			String value = prop.getProperty(name) ;
			
			// 输出
			System.out.println(name + "===" + value);
		}
	}

	public static void store() throws IOException {
		
		// 创建Properties对象
		Properties prop = new Properties() ;
		
		// 添加数据
		prop.setProperty("邓超", "孙俪") ;
		prop.setProperty("黄晓明", "杨颖") ;
		prop.setProperty("李晨", "范冰冰") ;

		prop.store(new FileWriter("names.properties"), "NAMES") ;
	}

}

 

Ⅳ 判断文件中是否有指定的键如果有就修改值的

 

我有一个文本文件,我知道数据是键值对形式的,但是不知道内容是什么。
请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其值为”100”

 

public class PropertiesTest {
	
	public static void main(String[] args) throws IOException {
		
		// 创建一个Properties对象
		Properties prop = new Properties() ;
		
		// 通过load方法加载数据到Properties集合
		prop.load(new FileReader("prop.properties")) ;
		
		// 获取所有键的Set集合
		Set<String> names = prop.stringPropertyNames() ;
		
		// 判断这个集合中是否存在"lisi"这个键
		if(names.contains("lisi")){
			prop.setProperty("lisi", "100") ;
		}
		
		// 调用store方法把数据存储到文件中
		prop.store(new FileWriter("prop.properties"), null) ;
		
	}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值