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.txt
BufferedWriter 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;
}
}