- 1.用代码实现以下需求
(1)定义学生类,包含姓名(String name),性别(String gender),年龄(int age)三个属性,生成空参有参构造,set和get方法,toString方法
(2)键盘录入6个学员信息(录入格式:张三,男,25),要求有两个相同的信息,将6个学员信息存入到ArrayList集合中
(3)将存有6个学员信息的ArrayList集合对象写入到D:\\StudentInfo.txt文件中
(4)读取D:\\StudentInfo.txt文件中的ArrayList对象
(5)对ArrayList集合中的6个学生对象进行去重并按照年龄从小到大的顺序排序
(6)将ArrayList集合中排序后的结果利用PrintWriter流写入到E:\\StudentInfo.txt文件中(写入格式:张三-男-25)
这里排序用的是Student 类实现Comparable<Student>接口,然后用TreeSet 自然排序。
package exrcise1;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.TreeSet;
public class Test {
/*
* 1.用代码实现以下需求
(1)定义学生类,包含姓名(String name),性别(String gender),年龄(int age)三个属性,生成空参有参构造,set和get方法,toString方法
(2)键盘录入6个学员信息(录入格式:张三,男,25),要求有两个相同的信息,将6个学员信息存入到ArrayList集合中
(3)将存有6个学员信息的ArrayList集合对象写入到D:\\StudentInfo.txt文件中
(4)读取D:\\StudentInfo.txt文件中的ArrayList对象
(5)对ArrayList集合中的6个学生对象进行去重并按照年龄从小到大的顺序排序
(6)将ArrayList集合中排序后的结果利用PrintWriter流写入到E:\\StudentInfo.txt文件中(写入格式:张三-男-25)
* */
public static void main(String[] args) throws IOException, ClassNotFoundException {
//将list写入文件
ArrayList<Student> list = new ArrayList<>();
list.add(new Student("alex", "男", 22));
list.add(new Student("alex", "男", 22));
list.add(new Student("alex2", "男", 25));
list.add(new Student("alex1", "女", 24));
list.add(new Student("alex3", "女", 26));
list.add(new Student("alex4", "男", 27));
// writeToFile(list, "/home/alex/test/Student.object");
//读取文件中list
ArrayList<Student> list1 = readFromFile("/home/alex/test/Student.object");
//去重
HashSet<Student> set = new HashSet<>();
for (Student student : list1) {
set.add(student);
}
//输出结果
for (Student student : set) {
System.out.println(student.toString());
}
System.out.println("---------------------------------------------");
//用TreeSet排序 需要Student类实现Comparable<Student>接口
TreeSet<Student> tset = new TreeSet<>();
for (Student s : set) {
tset.add(s);
}
//输出结果
for (Student student : tset) {
System.out.println(student.toString());
}
String dest = "/home/alex/test/StudentInfo.txt";
printToFile(tset, dest);
}
private static void printToFile(TreeSet<Student> tset, String dest) throws IOException{
//注意PrintWriter需要传入流才能自动刷新
PrintWriter pw = new PrintWriter(new FileOutputStream(dest),true);
for (Student student : tset) {
String s = student.getName() + "-" + student.getGender() + "-" + student.getAge();
pw.println(s);
}
pw.close();
}
private static ArrayList<Student> readFromFile(String string) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(string));
ArrayList<Student> list = null;
list = (ArrayList<Student>) ois.readObject();
ois.close();
return list;
}
public static void writeToFile(ArrayList<Student> list, String dest) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(dest));
oos.writeObject(list);
oos.close();
}
}
package exrcise1;
import java.io.Serializable;
public class Student implements Serializable ,Comparable<Student>{
/*
* (1)定义学生类,包含姓名(String name),性别(String gender),年龄(int age)三个属性,生成空参有参构造,set和get方法,toString方法
* */
private String name;
private String gender;
private int age;
public Student() {
super();
}
public Student(String name, String gender, int age) {
super();
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", gender=" + gender + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Student o) {
if(this.age > o.age) {
return 1;
}else if(this.age == o.age) {
return 0;
}else {
return -1;
}
}
}
- 2.用代码实现以下需求:
(1)已知配置文件config.properties中有三个键值对
name=zhangsan
score=80
address=beijing
(2)使用IO字节流对象和Properties类结合使用,将配置文件中的score键的值修改为100
package exrcise2;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Set;
public class Test1 {
/*
*
* 2.用代码实现以下需求: (1)已知配置文件config.properties中有三个键值对
* name=zhangsan
* score=80
* address=beijing
* (2)使用IO字节流对象和Properties类结合使用,将配置文件中的score键的值修改为100
*
*/
public static void main(String[] args) {
// update_properties("score", "100");
update_properties1("score", "100");
}
private static void update_properties1(String key, String val) {
File file = new File("/home/alex/test/config.properties");
Properties prop = new Properties();
BufferedReader in = null;
BufferedWriter out = null;
try {
in = new BufferedReader(new FileReader(file));
prop.load(in);
//load 一定要在 new FileOutputStream(file)之前,否则将会新建一个空文件读取
out = new BufferedWriter(new FileWriter(file));
prop.setProperty(key, val);
prop.store(out, "");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private static void update_properties(String key, String val) {
File file = new File("/home/alex/test/config.properties");
Properties prop = new Properties();
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(file);
prop.load(in);
//load 一定要在 new FileOutputStream(file)之前,否则将会新建一个空文件读取
out = new FileOutputStream(file);
prop.setProperty(key, val);
prop.store(out, "");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
- 3.获取指定目录及子目录下所有txt文件的个数,并将这些txt文件复制到D盘下任意目录
package exrcise3;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test3 {
/*
* 2.获取指定目录及子目录下所有txt文件的个数,并将这些txt文件复制到D盘下任意目录
* */
public static void main(String[] args) {
function("/home/alex/test", "/home/alex/texts");
}
public static void function(String src, String dest) {
File path = new File(src);
File[] fl = path.listFiles(new TxtFilter());
BufferedWriter out = null;
BufferedReader in = null;
for (File f : fl) {
if(f.isDirectory()) {
function(f.getAbsolutePath(), dest);
}else if(f.isFile()){
File newFile = new File(dest+"/"+f.getName());
// System.out.println(newFile.getAbsolutePath());
try {
in = new BufferedReader(new FileReader(f));
out = new BufferedWriter(new FileWriter(newFile));
String line = null;
while((line = in.readLine())!=null) {
out.write(line);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
class TxtFilter implements FileFilter{
@Override
public boolean accept(File pathname) {
if(pathname.isDirectory()) {
return true;
}else if(pathname.isFile()&&pathname.getName().endsWith("txt")) {
return true;
}else {
return false;
}
}
}