案例 集合到文件 数据排序改进版
//在学生类里添加方法
public int getSum(){
return this.chinese + this.math+this.english;
}
//比较器排序 匿名内部类
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2){
//从高到低 2-1
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.getName().compareTo(s2.getName()):num3;
renturn num4;
}
});
//键盘录入学生数据
for(int i = 0;i<5;i++){
Scanner sc = new Scanner(System.in);
sout("请录入第“+(i+1)+"个学生信息");
sout("姓名:");
String name = sc.nextLine();
sout("语文成绩:");
int chinese = sc.nextLine();
sout("数学成绩:");
int math = sc.nextLine();
sout("英语成绩:");
int enligsh = sc.nextLine();
//创建学生对象,把键盘录入的数据赋值给对应的学生对象
Student s = new Student();
s.setName(name);
s.setChinese(chinese);
s.setMath(math);
s.setEnligsh(enligsh);
//把学生对象添加到集合种
ts.add(s);
}
//目的地创建字符缓冲输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName:"myCharStream\\ts.txt"));
//遍历集合,得到每一个学生对象
for(Student s ;ts){
//把学生对象的数据拼接成指定格式的字符串
StringBuilder sb = new StringBuilder();
sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnligsh()).append(",").append(s.getSum());
//调用字符缓冲输出流对象的方法写数据
bw.write(sb.toString());//不包含换行符 所有数据都在同一行
bw.newwLine();
bw.flush();
}
//释放资源
bw.close();
案例 复制单级文件夹
单级文件夹就是该文件夹内部只有文件没有文件夹
需求:把"E:\itcast"这个文件夹复制到模块目录下
//创建数据源目录File对象,路径是"E:\\itcast"
File srcFolder = mew File(pathname:"E:\\itcast");
//获取数据源目录File对象名称
String srcFolderName = srcFolder.getName();
//创建目的地目录File对象,路径是模块名+itcast组成(myCharStream\\itcast)
File destFolder = mew File(parent:"myCharStream",srcFolderName);
//判断目的地目录对应的FIle是否存在,如果不存在就创建
if(!destFolder.exists()){
destFolder.mkdir();
}
//获取数据源所有目录下的File数组
File[] listFiles = srcFolder.listFiles();
//遍历File数组,得到每一个File对象,该File对象,其实就是数据源文件
for(File srcFile : listFiles){
//获取数据源文件File对象名称(mn.jpg)
String srcFileName = srcFile.getName();
//创建目的地File对象,路径名是个目的地目录加上文件名
File destFile = mew File(destFolder,srcFileName);
//复制文件
copyFile(srcFile,destFile);//方法
}
//字节缓冲流复制文件
private static void copyFile(File srcFile,File destFile) throws IOException{
//封装好数据源文件和目的地文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bys = new byte[1024];
int len;
while((len=bis.read(bys))!=-1){
bos.write(bys.off:0,len);
}
bos.close();
bis.close();
}
案例 复制多级文件夹
多级用递归方法
//创建数据源File对象,路径是"E:\\itcast"
File src = mew File(pathname:"E:\\itcast");
//创建目的地File对象,路径是F:\\
File destFile = new File((pathname:"F:\\");
//写方法实现文件夹的复制,参数为数据源File对象和目的地File对象
copyFolder(srcFile,destFile);//方法
//写方法实现文件夹的复制
private static void copyFolder(File srcFile,File destFile) throws IOException{
//判断数据源File是否是目录 如果是目录执行下面
if(srcFile.isDirectory()){
//首先再目的地目录下创建和数据源File名称一样的目录
//获取数据源文件File对象名称(mn.jpg)
String srcFileName = srcFile.getName();
File newFolder = new File(destFile,srcFileName);//创建目的地目录 拼接起来F:\\itcast
//路径封装好就创建
if(!newFolder.exists()){
newFolder.mkdir();
}
//获取数据源File下所有文件或者目录的File数组
File[] listArray = srcFolder.listFiles();
//遍历File数组,得到每一个File对象,
for(File file : listArray){
//这个File可能是文件也可能是目录 把该File作为数据源File对象,递归调用复制文件夹的方法
copyFolder(file,newFolder);
}
}else{
//不是目录说明是文件直接写入字节流 直接复制
File newFile = new File(destFile,srcFile.getName());//是文件要放在对应的目的地目录下并后面加上文件名字 封装一下目的地的目的地文件 目的地目录加数据源文件名称
copyFile(srcFile,newFile); //调用复制文件方法
}
}
//字节缓冲流复制文件
private static void copyFile(File srcFile,File destFile) throws IOException{
//封装好数据源文件和目的地文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bys = new byte[1024];
int len;
while((len=bis.read(bys))!=-1){
bos.write(bys.off:0,len);
}
bos.close();
bis.close();
}
复制文件的异常处理
在方法内部进行异常处理
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。