package net.csdn.blog.wdd9527.week04.day05.test05;
import java.io.File;
/*
* 递归获取目录下的所有文件,包括子目录下的文件
*/
public class Main03 {
public static void main(String[] args) {
File file = new File("d:" + File.separator + "360安全浏览器下载");
display(file);
}
public static void display(File file) {
// 判断文件是否存在
if (!file.exists()) {
return;
}
// 判断是否为目录
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
if(f.isFile()){
System.out.println(f.getAbsolutePath());
}else{
display(f);
}
}
}else{
System.out.println(file.getAbsolutePath());
}
}
}
package net.csdn.blog.wdd9527.week05.day01.test02;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
//序列化和反序列化
public class Main {
public static void main(String[] args) {
Student s1 = new Student("张三", 19, '女', true);
Student s2 = new Student("李四", 21, '男', false);
Student s3 = new Student("赵五", 22, '女', true);
ArrayList<Student> list = new ArrayList<>();
ArrayList<Student> list1 = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(new File("bbb.dat")));
ois = new ObjectInputStream(new FileInputStream(new File("bbb.dat")));
for (Student student : list) {
oos.writeObject(student); //为什么是乱码?
}
for (int i = 0; i < list.size(); i++) {
Object readObject = ois.readObject();
if (readObject instanceof Student) {
Student s = (Student) readObject;
list1.add(s);
}
}
System.out.println(list1.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
oos.flush();
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package net.csdn.blog.wdd9527.week05.day01.test02;
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = -2433290310809040457L;
private String name;
private Integer age;
private char sex;
private boolean isXxx;
public Student() {
super();
}
public Student(String name, Integer age, char sex, boolean isXxx) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.isXxx = isXxx;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public boolean isXxx() {
return isXxx;
}
public void setXxx(boolean isXxx) {
this.isXxx = isXxx;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", isXxx=" + isXxx + "]";
}
}
package net.csdn.blog.wdd9527.week05.day01.test05;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.TreeSet;
/**
* 已知项目根目录下student_info.txt文件中有如下数据:(姓名-年龄-总分)
* lucy-28-98
* lily-23-97
* robt-25-100
* wili-15-100
* klin-29-93
* 运用IO技术获取将该文件中的数据分别封装成5个Student
* (姓名为String类型,年龄为int类型,总分为int类型 )对象存入TreeSet集合中(需要自己定义Student类)
* 要求:
* 根据学生的总分进行排序(降序),如果分数相同则比较年龄,年龄较大的排在前面。
* 按照排序完的顺序将所有信息打印到控制台上: 打印格式如下:
* robt-25-100
* wili-15-100
* lucy-28-98
* lily-23-97
* klin-29-93
*
* @author WDD
*
*/
public class Main {
public static void main(String[] args) {
BufferedReader reader = null;
TreeSet<Student> set = new TreeSet<>();
try {
reader = new BufferedReader(new FileReader(new File("student_info.txt")));
String s1 = null;
while ((s1 = reader.readLine()) != null) {
String[] split = s1.split("-");
Student s = new Student();
s.setName(split[0]);
s.setAge(Integer.parseInt(split[1]));
s.setGrade(Integer.parseInt(split[2]));
set.add(s);
}
for (Student student : set) {
System.out.println(student);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package net.csdn.blog.wdd9527.week05.day01.test05;
public class Student implements Comparable<Student>{
private String name;
private Integer age;
private Integer grade;
public Student() {
super();
}
public Student(String name, Integer age, Integer grade) {
super();
this.name = name;
this.age = age;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getGrade() {
return grade;
}
public void setGrade(Integer grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", grade=" + grade + "]";
}
@Override
public int compareTo(Student o) {
if (this.grade == o.getGrade()) {
if (this.age > o.getAge()) {
return -1;
} else {
return 1;
}
} else if (this.grade > o.getGrade()) {
return -1;
} else {
return 1;
}
}
}
package net.csdn.blog.wdd9527.week04.day05.test06;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
/**
* 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩)
* 计算出平均成绩,把原有的数据和计算出的平均分数存放在磁盘文件"stud "中。
*
* @author WDD
*
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//HashMap<Student, Double> map = new HashMap<>(); //键值对的值不能是double double是基本类型。。
ArrayList<Student> list = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
System.out.println("请输入第" + i + "个学生的学号:");
String number = sc.next();
System.out.println("请输入第" + i + "个学生的姓名:");
String name = sc.next();
System.out.println("请输入第" + i + "个学生的语文成绩:");
double chinaGrade = sc.nextDouble();
System.out.println("请输入第" + i + "个学生的数学成绩:");
double mathGrade = sc.nextDouble();
System.out.println("请输入第" + i + "个学生的英语成绩:");
double englishGrade = sc.nextDouble();
double ave = (chinaGrade + mathGrade + englishGrade) / 3;
System.out.println("第" + i + "个学生的平均成绩为:" + ave);
Student s = new Student(number, name, chinaGrade, mathGrade, englishGrade,ave);
list.add(s);
}
/*Set<Entry<Student,Double>> entrySet = map.entrySet();
Iterator<Entry<Student, Double>> it = entrySet.iterator();
while (it.hasNext()) {
Entry<Student, Double> next = it.next();
System.out.println(next);
}*/
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
Student student = (Student) it.next();
System.out.println(student.toString());
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(new File("stud.txt")));
/*Iterator<Student> it1 = list.iterator();
while (it1.hasNext()) {
Student student = (Student) it1.next();
bw.write(student.toString());
bw.newLine();*/
for (Student student : list) {
bw.write(student.toString());
bw.newLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bw.flush();
bw.close();
sc.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package net.csdn.blog.wdd9527.week04.day05.test06;
public class Student {
private String stuNumber;//学号
private String name;
private double chinaGrade;
private double mathGrade;
private double englishGrade;
private double ave;
public Student() {
super();
}
public Student(String stuNumber, String name, double chinaGrade, double mathGrade, double englishGrade,double ave) {
super();
this.stuNumber = stuNumber;
this.name = name;
this.chinaGrade = chinaGrade;
this.mathGrade = mathGrade;
this.englishGrade = englishGrade;
this.ave = ave;
}
public String getStuNumber() {
return stuNumber;
}
public void setStuNumber(String stuNumber) {
this.stuNumber = stuNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getChinaGrade() {
return chinaGrade;
}
public void setChinaGrade(double chinaGrade) {
this.chinaGrade = chinaGrade;
}
public double getMathGrade() {
return mathGrade;
}
public void setMathGrade(double mathGrade) {
this.mathGrade = mathGrade;
}
public double getEnglishGrade() {
return englishGrade;
}
public void setEnglishGrade(double englishGrade) {
this.englishGrade = englishGrade;
}
public double getAve() {
return ave;
}
public void setAve(double ave) {
this.ave = ave;
}
@Override
public String toString() {
return "Student [stuNumber=" + stuNumber + ", name=" + name + ", chinaGrade=" + chinaGrade + ", mathGrade="
+ mathGrade + ", englishGrade=" + englishGrade + ", ave=" + ave + "]";
}
}
package net.csdn.blog.wdd9527.week04.day05.test03;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
//图片的拷贝(改下名字可以实现别的拷贝)
//改变byte数组的大小,速度会更快,但是拷贝之后的内容占用空间更大
public class Main {
public static void main(String[] args) {
File f = new File("d:" + File.separator + "Program Files" + File.separator + "视频原件.mp4");
File f2 = new File("d:" + File.separator + "视频副本.mp4");
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(f);
out = new FileOutputStream(f2);
byte[] s = new byte[1024];
while (in.read(s) != -1) {
out.write(s);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
out.flush();
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}