(44)Java学习笔记——IO流 / 分阶段总结和练习

IO流总结



练习01:复制文本文件

使用字符流更方便一些。采用字符缓冲区流的方式。

package cn.itcast_01;

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

/*
 * 采用字符缓冲流复制文本文件
 */
public class CopyBufferedTest_01 {
	public static void main(String[] args) throws IOException {
		//创建字符缓冲流对象
		BufferedReader br = new BufferedReader(new FileReader("a.txt"));
		BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
		
		//输出输入数据
		String line = null;
		while((line = br.readLine())!=null){
			bw.write(line);
			bw.newLine();
			bw.flush();
		}
		
		//释放资源
		br.close();
		bw.close();
		
	}
}
练习02:复制图片

package cn.itcast_01;

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 CopyImageTest_01 {
	public static void main(String[] args) throws IOException {
		//定义字节缓冲流的对象
		BufferedInputStream bis = new BufferedInputStream(
				new FileInputStream("f:\\马士兵JAVA.jpg"));
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream("g:\\mashibing.jpg"));
		
		//读写数据
		byte[] bys = new byte[1024];
		int len=0;
		while((len = bis.read(bys))!= -1){
			bos.write(bys,0,len);
		}
		
		//释放资源
		bis.close();
		bos.close();
	}
}

练习03:把ArrayList集合中的字符串数据储存到文本文件

package cn.itcast_01;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

/*
 * 把ArrayList集合中的字符串数据存储到文本文件
 * 思路:
 * A / 定义一个ArrayList集合,添加字符串数据
 * B / 创建字符缓冲流对象
 * C / 遍历集合写入数据
 * D / 释放资源
 */
public class ArrayListTest_01 {
	public static void main(String[] args) throws IOException {
		//创建ArrayList集合
		ArrayList<String> array = new ArrayList<String>();
		//添加元素
		array.add("hello");
		array.add("world");
		array.add("java");
		
		//创建字符缓冲输出流
		BufferedWriter bw = new BufferedWriter(
				new FileWriter("aa.txt"));
		
		//遍历集合并写入数据
		Iterator<String> it = array.iterator();
		while (it.hasNext()){
			String s = it.next();
			bw.write(s);
			bw.flush();
		}
		
		//释放资源
		bw.close();
		
	}
}

练习04:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合

package cn.itcast_01;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

/*
 * 练习04:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合
 * 
 */
public class ArrayListTest_02 {
	public static void main(String[] args) throws IOException {
		//创建字符缓冲输出流
		BufferedReader bd = new BufferedReader(
				new FileReader("aa.txt"));
		
		//创建集合对象
		ArrayList<String> array = new ArrayList<String>();
		
		//读取数据并添加到集合
		String line = null;
		while((line= bd.readLine())!= null){
			//字符数组转成字符串
			String s = line.toString();
			array.add(s);
		}
		
		//遍历集合
		Iterator<String> it = array.iterator();
		while(it.hasNext()){
			String s = it.next();
			System.out.println(s);
		}
		
		//释放资源
		bd.close();
		
	}
}
练习05:随机获取文本文件中的姓名案例

package cn.itcast_01;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

/*
 * 练习05:随机获取文本文件中的姓名案例
 * 思路:
 * A / 读取文件中的姓名
 * B / 传入集合 (采用ArrayList集合,因为他有get()功能)
 * C / 随机获取集合内的元素
 * 
 */

public class RandomTest_01 {
	public static void main(String[] args) throws IOException {
		//创建字符缓冲读取流对象
		BufferedReader bd =new BufferedReader(
				new FileReader("bb.txt"));
		
		//创建集合用存储文本中的名字
		ArrayList<String> array =new ArrayList<String>();
		
		//读取数据并添加到集合中
		String line = null;
		while ((line = bd.readLine())!= null){
			array.add(line);
		}
		
		//随机获取集合中的元素
		Random r = new Random();
		int result = r.nextInt(array.size());
		String s = array.get(result);
		System.out.println(s);
		
		//释放资源
		bd.close();
	}
}

练习06:复制单级文件夹

package cn.itcast_01;

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

/*
 * 复制单级文件夹
 * 数据源:F:\\demo
 * 目的地:G:\\newDemo
 * 
 * 思路:
 * A / 封装目录
 * B / 获取该目录下的所有文件的File数组
 * C / 遍历File 数组,得到每一个File对象
 * D / 把该File进行复制
 * 
 */
public class FolderTest_01 {
	public static void main(String[] args) throws IOException {
		//封装目录
		File srcFolder = new File("f:\\demo");
		//封装目的地
		File destFolder = new File("g:\\newDemo");
		
		//如果目的地文件夹不存在,就创建
		if (!destFolder.exists()){
			destFolder.mkdirs();
		}

		//获取该目录下的所有文件的File数组
		File[] fileArray = srcFolder.listFiles();
			
		//遍历数组,得到每一个File对象
		for(File file : fileArray){
			//System.out.println(file);
			//f:\demo\a.txt
			String name = file.getName();	//a.txt
			File newFile = new File(destFolder,name);	//g:\\newDemo\\a.txt
			
			copyFile(file,newFile);
			}
		System.out.println("文件复制完成!");	

	}
		
	private static void copyFile(File file, File newFile) throws IOException {
			
		//创建字节缓冲流对象
		BufferedInputStream bis =new BufferedInputStream(
				new FileInputStream(file));
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(newFile));
		
		//读取每一个File对象并输入
		byte[] bys = new byte[1024];
		int len=0;
		while((len = bis.read(bys))!= -1){
			bos.write(bys, 0, len);
		
		}
		bos.flush();
		bos.close();
		bis.close();
	}
}

练习07:复制多级文件夹

package cn.itcast_01;

package cn.itcast_01;

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

/*
 * 练习07:复制多级文件夹
 * 数据源:F:\\demos
 * 目的地:G:\\
 * 
 * 思路:
 * A / 封装目录FILE
 * B / 判断该File是文件还是文件夹
 * 			a / 是文件
 * 					复制(字节流)
 * 			b / 是文件夹
 * 					就在目的地目录下创建该文件夹
 * 					获取该File对象下的所有文件或者文件夹File对象
 * 					遍历得到每一个File对象
 * 					回到B
 */
public class CopyFolderTest_02 {
	public static void main(String[] args) throws IOException {
		//封装目录
		File srcFile = new File("F:\\demos");
		File destFile = new File("g:\\");
		
		//复制文件夹的方法
		copyFolder(srcFile,destFile);

	}

	private static void copyFolder(File srcFile, File destFile) throws IOException {
		if(srcFile.isDirectory()){
			//是文件夹就在目的地目录下创建该文件夹
			File newFolder = new File(destFile,srcFile.getName());
			newFolder.mkdir();
			//获取该File对象下的所有文件或者文件夹File对象
			File[] fileArray =srcFile.listFiles();
			for(File  file : fileArray){
				copyFolder(file, newFolder);	//递归
			}
		}
		else {
			File newFile =new File(destFile,srcFile.getName());
			copyFile(srcFile,newFile);
		}
		
	}

	private static void copyFile(File srcFile, File newFile) throws IOException {
		BufferedInputStream bis = new BufferedInputStream(
				new FileInputStream(srcFile));
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(newFile));
		
		byte[] bys = new byte[1024];
		int len =0;
		while((len = bis.read(bys))!=-1){
			bos.write(bys, 0, len);
			bos.flush();
		}
		bis.close();
		bos.close();
		
	}
}

练习08:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩)按照总分从高到低存入文本文件

package cn.itcast_02;
/*
 * Student
 */
public class Student {
	private String name;
	
	private int chinese;
	
	private int math;
	
	private int english;

	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Student(String name, int chinese, int math, int english) {
		super();
		this.name = name;
		this.chinese = chinese;
		this.math = math;
		this.english = english;
	}

	public String getName() {
		return name;
	}

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

	public int getChinese() {
		return chinese;
	}

	public void setChinese(int chinese) {
		this.chinese = chinese;
	}

	public int getMath() {
		return math;
	}

	public void setMath(int math) {
		this.math = math;
	}

	public int getEnglish() {
		return english;
	}

	public void setEnglish(int english) {
		this.english = english;
	}
package cn.itcast_02;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

/*
 * 练习08:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩)按照总分从高到低存入文本文件
 * 思路:
 * A / 创建学生对象
 * B / 创建集合对象 TreeSet<Student>
 * C / 键盘录入学生信息存储到集合
 * D / 遍历集合,输出到文本
 */
public class IOTest_01 {
	public static void main(String[] args) throws IOException {
		//创建集合对象
		TreeSet<Student> ts = new TreeSet<Student>(
				new Comparator<Student>() {		//比较器比较

					@Override
					public int compare(Student s1, Student s2) {
						int num = s2.getSum() - s1.getSum();	//主要条件
						int num2 = num ==0?s1.getChinese() - s2.getChinese() :num;
						int num3 = num2 ==0?s1.getMath() - s2.getMath() : num2;
						int num4 = num3 ==0?s1.getEnglish() - s2.getEnglish() : num3;
						int num5 = num4 ==0?s1.getName().compareTo(s2.getName()) : num4;
						return num5;
					}
					
				});
		
		//键盘录入学生信息储存到集合
		for(int x =0; x<=5 ;x++){
			Scanner sc =new Scanner(System.in);
			System.out.println("请录入第"+x+"个的学生信息");
			System.out.println("姓名");
			String name = sc.nextLine();
			System.out.println("语文成绩");
			int chinese = sc.nextInt();
			System.out.println("数学成绩");
			int math = sc.nextInt();
			System.out.println("英语成绩");
			int english = sc.nextInt();
			
			//创建学生对象
			Student s = new Student();
			s.setName(name);
			s.setChinese(chinese);
			s.setMath(math);
			s.setEnglish(english);
			
			//把学生信息添加到集合
			ts.add(s);
		}
		
		//遍历集合,把数据写到文本文件
		BufferedWriter bw = new BufferedWriter(
				new FileWriter("Student.txt"));
		bw.write("学生信息如下:");
		bw.newLine();
		bw.flush();
		bw.write("姓名,语文成绩,数学成绩,英语成绩");
		bw.newLine();
		bw.flush();
		//遍历集合
		for (Student s : ts){
			StringBuilder sb = new StringBuilder();
			sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish());
			bw.write(sb.toString());
			bw.newLine();
			bw.flush();
		}
		
		//释放资源
		bw.close();
		System.out.println("学生信息存储完毕!");
		
		
		
	}
}

练习09:把一个文件中的字符串排序后在写入另一个文件案例

package cn.itcast_02;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

/*
 * 已知s.txt文件中有这样一个字符串:"jintiantianqihenhaoxinqingbuhao",读取数据内容,吧数据排序后写入ss.txt
 * 思路:
 * A/ 创建字符缓冲流
 * B/ 读取文件内容,并转换成字符串
 * C/ 字符串转换为字符数组,并对数组排序
 * D/ 将排序后的字符串写入文件
 */
public class StringTest_01 {
	public static void main(String[] args) throws IOException {
		//创建字符缓冲流对象
		BufferedReader br = new BufferedReader(
				new FileReader("s.txt"));
		
		//读取该文件内容,存储到一个字符串中
		String line = br.readLine();
		br.close();
		
		//把字符串转成字符数组
		char[] chs = line.toCharArray();
		
		//对字符数组进行排序
		Arrays.sort(chs);
		
		//把排序后的数组转成字符串
		String s = String.valueOf(chs);
		
		//把排序后的字符串写入ss.txt文件
		BufferedWriter bw = new BufferedWriter(
				new FileWriter("ss.txt"));
		bw.write(s);
		bw.flush();
		bw.close();

		
	}
}




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值