IO流与集合都已经学过了一遍,接下来看一些案例,混合使用IO流与集合:
案例分析:
1.把集合中的数据存储到文本文件
案例演示: 需求:把ArrayList集合中的字符串数据存储到文本文件
分析:
a: 创建一个ArrayList集合
b: 添加元素
c: 创建一个高效的字符输出流对象
d: 遍历集合,获取每一个元素,把这个元素通过高效的输出流写到文 本文件中
e: 释放资源
public class Demo1 {
public static void main(String[] args) throws IOException {
//定义一个集合
ArrayList<String> arrayList=new ArrayList<>();
arrayList.add("窗前明月光");
arrayList.add("疑是地上霜");
arrayList.add("举头望明月");
arrayList.add("低头思故乡");
BufferedWriter bw=new BufferedWriter(new FileWriter("a.txt"));
//遍历集合元素并写入文本文件
for(String str:arrayList){
bw.write(str);
bw.newLine();
bw.flush();
}
bw.close();
}
}
案例2:把文本文件中的数据存储到集合中
案例演示: 需求:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合
分析:
* a: 创建高效的字符输入流对象
* b: 创建一个集合对象
* c: 读取数据(一次读取一行)
* d: 把读取到的数据添加到集合中
* e: 遍历集合
* f: 释放资源
public class Demo2 {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("a.txt"));
ArrayList<String> arrayList=new ArrayList<>();
//遍历文本文件元素写入集合
String line=null;
while((line=br.readLine())!=null){
arrayList.add(line);
}
//遍历集合输出元素
for(int i=0;i<arrayList.size();i++){
String str=arrayList.get(i);
System.out.println(str);
}
br.close();
案例3:随机获取文本文件中的姓名
案例演示: 需求:我有一个文本文件,每一行是一个学生的名字,请写一个程序,每次允许随机获取一个学生名称
* 分析:
* a: 创建一个高效的字符输入流对象
* b: 创建集合对象
* c: 读取数据,把数据存储到集合中
* d: 产生一个随机数,这个随机数的范围是 0 - 集合的长度 . 作为: 集合的随机索引
* e: 根据索引获取指定的元素
* f: 输出
* g: 释放资源
public class Demo3 {
public static void main(String[] args) throws IOException {
//创建一个存储学生姓名的文本文件
BufferedWriter bw=new BufferedWriter(new FileWriter("b.txt"));
bw.write("张三");
bw.newLine();
bw.write("李四");
bw.newLine();
bw.write("王五");
bw.newLine();
bw.write("赵六");
bw.newLine();
bw.write("明明");
bw.newLine();
bw.write("天天");
bw.flush();
//把文本文件存储到集合中去
ArrayList<String> arrayList=new ArrayList<>();
BufferedReader br=new BufferedReader(new FileReader("b.txt"));
String line=null;
while((line=br.readLine())!=null){
arrayList.add(line);
}
//产生随机索引
int size=arrayList.size();
Random random=new Random();
int i=random.nextInt(size);
System.out.println(i);
String str=arrayList.get(i);
System.out.println(str);
bw.close();
br.close();
}
}
案例4:复制单级文件夹(复制多级文件夹)
案例演示: 需求: 复制D:\course这文件夹到E:\course
- 分析:
a: 封装D:\course为一个File对象
b: 封装E:\course为一个File对象,然后判断是否存在,如果不存在就是创建一个目录
c: 获取a中的File对应的路径下所有的文件对应的File数组
d: 遍历数组,获取每一个元素,进行复制
e: 释放资源
public class Demo4 {
public static void main(String[] args) throws IOException {
File srcFloder= new File("D:\\course");
File aimFolder = new File("E:\\course");
if (!aimFolder.exists()) {
aimFolder.mkdirs();
}
copyFolder(srcFloder,aimFolder);
}
private static void copyFolder(File srcFloder, File aimFolder)throws IOException {
File[] files=srcFloder.listFiles();
for(File f:files){
if(f.isFile()) {
copyfile(f, aimFolder);
} else{
File aimFloder1=new File(aimFolder,f.getName());
if(!aimFloder1.exists()){
aimFloder1.mkdirs();
}
copyFolder(f,aimFloder1);
//复制多层文件夹时,需要加此代码
}
}
}
private static void copyfile(File f, File aimFolder) throws IOException{
FileInputStream fis=new FileInputStream(f);
//新创建一个输出流,注意是根据一个父对象和一个子文件得到的File对象
FileOutputStream fos=new FileOutputStream(new File(aimFolder,f.getName()));
int len=0;
byte[] bytes=new byte[1024*8];
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
fos.flush();
}
fos.close();
fis.close();
}
}
案例5:复制指定目录下指定后缀名的文件并修改名称
案例演示: * 需求: 复制D:\demo目录下所有以.java结尾的文件到E:\demo .并且将其后缀名更改文.jad
- 分析:
a: 复制D:\demo文件夹到E:\
b: 对E:\demo文件夹下的文件进行重命名
public class Demo5 {
public static void main(String[] args) throws IOException {
File file1 = new File("D:\\demo");
File file2 = new File("E:\\demo");
if (!file2.exists()) {
file2.mkdirs();
}
File[] files = file1.listFiles();
for (File file : files) {
if (file.getName().endsWith(".java")) {
copyFile(file, file2);
}
}
}
private static void copyFile(File file, File file2) throws IOException {
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(new File(file2,file.getName()));
int len = 0;
byte[] bytes = new byte[1024];
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
out.flush();
}
//把IO流关掉,然后再进行文件重命名
in.close();
out.close();
File[] files1=file2.listFiles();
for(File f:files1){
String name=f.getName();
int index=name.indexOf('.');
String name1=name.substring(0,index);
String str=name1.concat(".jad");
File file3=new File("E:\\demo",str);
f.renameTo(file3);
}
}
}
案例6:键盘录入学生信息按照总分排序并写入文本文件
案例演示: 需求:键盘录入3个学生信息(姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)), 按照总分从高到低存入文本文件
- 分析:
- a: 创建一个学生类: 姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)
- b: 因为要排序,所以需要选择TreeSet进行存储学生对象
- c: 键盘录入学生信息,把学生信息封装成一个学生对象,在把学生对象添加到集合中
- d: 创建一个高效的字符输出流对象
- e: 遍历集合,把学生的信息写入到指定的文本文件中
- f: 释放资源
public class Demo6 {
public static void main(String[] args)throws IOException {
Scanner sc=new Scanner(System.in);
TreeSet<Student> treeset=new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int sum=s1.sumScore-s2.sumScore;
int sum2=sum==0?s1.name.compareTo(s2.name):sum;
return -sum2;
}
});
for(int i=1;i<=3;i++){
System.out.println("请输入第"+i+"个学生的姓名:");
String name=sc.next();
System.out.println("请输入第"+i+"个学生的语文成绩:");
int chineseScore=sc.nextInt();
System.out.println("请输入第"+i+"个学生的数学成绩:");
int mathScore=sc.nextInt();
System.out.println("请输入第"+i+"个学生的英语成绩:");
int englishScore=sc.nextInt();
int sumScore=scoreSum(chineseScore,mathScore,englishScore);
Student student = new Student(name,chineseScore,mathScore,englishScore,sumScore);
treeset.add(student);
}
BufferedWriter bw=new BufferedWriter(new FileWriter("score.txt"));
bw.write("姓名 语文成绩 数学成绩 英语成绩 总分");
bw.newLine();
for(Student stu:treeset){
String name=stu.name;
int chineseScore1=stu.chineseScore;
int mathScore1=stu.mathScore;
int englishScore1=stu.englishScore;
int sum=stu.sumScore;
bw.write(name+" "+chineseScore1+" "+mathScore1+" "+englishScore1+" "+sum);
bw.newLine();
bw.flush();
}
bw.close();
}
private static int scoreSum(int chineseScore, int mathScore, int englishScore) {
int sum=chineseScore+mathScore+englishScore;
return sum;
}
}
public class Student {
String name;
int sumScore;
int chineseScore;
int mathScore;
int englishScore;
public Student(String name,int chineseScore,int mathScore,int englishScore,int sumScore){
this.name=name;
this.chineseScore=chineseScore;
this.mathScore=mathScore;
this.englishScore=englishScore;
this.sumScore=sumScore;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sumScore=" + sumScore +
", chineseScore=" + chineseScore +
", mathScore=" + mathScore +
", englishScore=" + englishScore +
'}';
}
}