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;}}
阅读全文
0 0
- 2017.11.25作业
- 【Java作业】2017.11.25 / 11.26
- 25作业
- [2017.11.01]作业06
- 2017.11.2作业
- [2017.11.04]作业08
- 2017.11.3作业
- 2017.11.6作业
- 2017.11.7作业
- 2017.11.10作业
- 2017.11.13作业
- 2017.11.14作业
- 2017.11.14作业
- 2017.11.15作业
- [2017.11.16]作业10
- [2017.11.18]作业12
- 2017.11.21大作业
- [2017.11.26]作业14
- [优质论文]伪造共同访问对推荐系统进行攻击
- 带你逐步深入了解SSH框架——hibernate概念和api使用
- 理解深度卷积网络
- 【二分+最大流Dinic】BZOJ1305(CQOI2009)[dance跳舞]题解
- java学习 java -- 函数
- 2017.11.25作业
- MySQL主从同步配置
- POJ3009题解
- JavaScript 面向对象编写方式
- 利用反射分析类的能力,使用反射分析对象
- Qt写文件的缓存buffer大小与编码格式
- 关于在Spring容器外调用bean
- ObjectBox[七] 支持LiveData(Android体系结构组件)
- 记录好文章