2017.11.25作业

来源:互联网 发布:通信网络与信息技术 编辑:程序博客网 时间:2024/06/05 20:41

1:需求:递归删除带内容的目录

import java.io.File;public class Homework1 {public static void main(String[] args) {File Folder = new File ("RNG6");Delete(Folder);}private static void Delete(File Folder) {File[] fileArray=Folder.listFiles();if (fileArray!=null){for(File file:f6ileArray){if(file.isDirectory()){Delete(file);}else{System.out.println(file.getName()+file.delete());}}}}}

2:需求:请大家把E:\JavaSE目录下所有的java结尾的文件的绝对路径给输出在控制台。

import java.io.File;public class HomeWork2 {public static void main(String[] args) {File file = new File("C:\\KSWJJ\\src");File[]lists= file.listFiles();for(File f: lists){if(f.isFile()){if(f.toString().endsWith(".java")){System.out.println(f);}}}}}

3. 复制文本文件:有5种方式

package qx.work_02;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class HomeWork1 {private static String srcString;public static void main(String[] args) throws IOException {long startTime = System.currentTimeMillis();method1("E:\\abc.mp4","Copy.mp4") ;method2("E:\\abc.mp4","Copy.mp4") ;method3("E:\\abc.mp4","Copy.mp4") ;method4("E:\\abc.mp4","Copy.mp4") ;method5("E:\\abc.mp4","Copy.mp4") ;long endTime = System.currentTimeMillis();System.out.println("共耗时:"+(endTime - startTime)+"毫秒");}//高效字节流一次读取一个字节数组private static void method4(String srcString , String destString) throws IOException{//封装数据源和目的地BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));//一次读取一个字节数组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();}//字节缓冲输入流一次读取一个字节private static void method3(String SrcString, String destString) throws IOException {//创建字节缓冲输入对象BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));//一次读取一个字节int by = 0;while((by = bis.read())!=-1){bos.write(by);bos.flush();}bis.close();bos.close();}//基本字节流一次读取一个字节数组private static void method2(String SrcString, String destString) throws IOException {FileInputStream fis = new FileInputStream(srcString);FileOutputStream fos = new FileOutputStream(destString);//一次读取一个字节数组byte [] bys = new byte[1024];int len = 0;while((len = fis.read())!=-1){fos.write(bys,0,len);}fis.close();fos.close();}//基本字节流一次读取一个字节private static void method1(String StrString, String destString) throws IOException{FileInputStream fis = new FileInputStream(srcString);FileOutputStream fos = new FileOutputStream(destString);//一次读取一个字节//并且一次写一个字节int by = 0;while((by = fis.read())!=-1){fos.write(by);}fis.close();fos.close();}//字符缓冲流一次读取一个字符串private static void method5(String srcString, String destString) throws IOException{BufferedReader br = new BufferedReader(new FileReader(srcString));BufferedWriter bw = new BufferedWriter(new FileWriter(destString));String line = null;while((line = br.readLine())!=null){bw.write(line);bw.newLine();bw.flush();}br.close();bw.close();}}

4. 复制图片:4种方式

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;public class HomeWork4 {public static void main(String[] args) throws IOException {File s1 = new File("a.jpg");File s2 = new File ("b.jpg");method1(s1,s2);method2(s1,s2);method3(s1,s2);method4(s1,s2);}//高效字节流一个读写一个字节数组private static void method4(File s1, File s2) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(s1));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(s2));        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();    }  //高效字节流一次读写一个字节private static void method3(File s1, File s2) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(s1));        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(s2));          int by = 0;    while ((by=bis.read())!=-1){    bos.write(by);    }    bis.close();    bos.close();}//基本字节流一次读写一个字节 数组private static void method2(File s1, File s2) throws IOException {FileInputStream fis = new FileInputStream(s1);FileOutputStream fos = new FileOutputStream(s2);byte [] bys = new byte [1024];int by = 0;while((by=fis.read(bys))!=-1){fos.write(bys,0,by);fos.flush();}fis.close();    fos.close();}//基本字节流一次读写一个字节private static void method1(File s1, File s2)throws IOException  {FileInputStream fis = new FileInputStream(s1);FileOutputStream fos = new FileOutputStream(s2); int by = 0;    while ((by=fis.read())!=-1){    fos.write(by);    }    fis.close();    fos.close();}}


5:已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”

          请编写程序读取数据内容,把数据排序后写入ss.txt中。

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Arrays;public class HomeWork5 {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 = new String(chs);//把字符串再次写入ss.txtBufferedWriter bw = new BufferedWriter(new FileWriter("ss.txt"));bw.write(s);bw.newLine();bw.flush();bw.close();}  }

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

import java.io.BufferedWriter;import java.io.IOException;import java.util.Scanner;import java.util.TreeSet;public class HomeWork6 {    public static void main(String[] args) throws IOException {          // 创建集合对象          TreeSet<Student> ts = new TreeSet<Student> (new Comparator<Student>() {              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 = 1; 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("students.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();     }  }  
public class Student {private String name;private int Chinese;private int English;private int math;public Student() {super();// TODO Auto-generated constructor stub}public Student(String name, int chinese, int english, int math) {super();this.name = name;Chinese = chinese;English = english;this.math = math;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getChinese() {return Chinese;}public void setChinese(int chinese) {Chinese = chinese;}public int getEnglish() {return English;}public void setEnglish(int english) {English = english;}public int getMath() {return math;}public void setMath(int math) {this.math = math;}public String toString() {return "Student [name=" + name + ", Chinese=" + Chinese + ", English="+ English + ", math=" + math + "]";}public int getSum(){return this.Chinese+this.English+this.math;}}



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩八个月了还在软得很怎么办 产后第三天乳房胀痛有硬块怎么办 怀孕七个多月了胎位不正怎么办 怀孕6个月梅毒1:4怎么办 怀孕了胃酸胃涨吐酸水怎么办 怀孕了胃酸胃胀吐酸水怎么办 练瑜伽大腿外侧扭筋了怎么办 膝盖运动时疼痛睡觉不疼怎么办 在农村里床上老是有小蜈蚣该怎么办 瑜伽垫晒太阳后散发的甲醛怎么办 杯子盖上的皮垫子掉了漏水怎么办 20个月宝宝天天晚上不睡觉怎么办 八个月大小孩天天晚上不睡觉怎么办 如果开了光的貔貅不要了要怎么办 刚岀生的婴儿长得太快怎么办 呼市去办牌照时没有牌照怎么办 宾馆发现隐藏的摄像头怎么办报警吗 拍拍贷律师函寄到家里了怎么办 欠了拍拍贷本息一万多了怎么办 把人偷小孩的人贩子打死了怎么办 怀孕3个月没有胎心怎么办 社保局打印关系转移信封之后怎么办 长裙变装外出被发现了怎么办 超变陀螺怎么绳子拉不出来怎么办 梦幻诛仙传说时间得不到东西怎么办 夹在强势母亲和强势老公中间怎么办 工作调动校长总拖着不盖章怎么办 宝贝在妈妈肚子里发育慢怎么办 领导安排你负责一次讲座你怎么办 雷蛇北海巨妖耳机有回音怎么办 手机直播声卡有杂音有回音怎么办 大班见到陌生人入园怎么办安全教案 两首歌合并到一起中间有停顿怎么办 想做主持人但不是播音专业怎么办 动脉造影术2天后穿刺点出血怎么办 战舰世界买了重复金币船怎么办 戒指戴在手上取不下来怎么办 工作中难以和同事上司相处怎么办 老是被上司和同事欺负我该怎么办 桌面太低座位太高写字不舒服怎么办 一个人如果欠下网贷无法偿还怎么办