1:需求:递归删除带内容的目录 假设删除当前项目下的目录:demo,demo中可以有文件夹自己给出
package pFile;
import java.io.File;
import java.io.IOException;
/**
* 1:需求:递归删除带内容的目录 假设删除当前项目下的目录:demo,demo中可以有文件夹自己给出
* @author 花花
*
*/
public class deleteFile {
static int times=0;
public static void main(String[] args) throws IOException {
File f=new File("demo\\dem\\de");
f.mkdirs();
f=new File("demo\\dem\\de\\d.txt");
f.createNewFile();
deleteDirs("demo\\dem\\de\\d.txt");
}
public static Boolean deleteDirs(String dirs) {
String regex="\\\\+";
String[] str=dirs.split(regex);
StringBuffer sb=new StringBuffer();
for (int i = 0; i <str.length-times; i++) {
if(i!=str.length-times-1) {
sb.append(str[i]+"\\\\");
}
else {
sb.append(str[i]);
}
}
dirs=sb.toString();
System.out.println("times"+times+dirs);
File f=new File(dirs);
boolean flag=f.delete();
times++;
if(flag) {
flag=deleteDirs(dirs);
}
return flag;
}
}
2:需求:请大家把E:\JavaSE目录下所有的java结尾的文件的绝对路径给输出在控制台。
package pFile;
import java.io.File;
import java.util.Iterator;
/**
* 2:需求:请大家把E:\JavaSE目录下所有的java结尾的文件的绝对路径给输出在控制台
* @author 花花
*
*/
public class choiceFile {
public static void main(String[] args) {
File f=new File("K:\\Java");
File[] files=f.listFiles();
for (File fi : files) {
if(fi.isFile()) {
if(fi.getName().endsWith("java")) {
System.out.println(fi.getAbsolutePath());
}
}
}
}
}
3:复制文本文件:有5种方式
package pFile;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 3:复制文本文件:有5种方式
* 文本文件用记事本打开就可以读懂,因此推荐用字符流,用字节流也可以
* @author 花花
*
*/
public class CopyText {
public static void main(String[] args) {
}
//使用缓冲字符流一次读取一行,推荐使用
static void method1(String sourceStr,String destStr) throws IOException {
BufferedReader br=new BufferedReader(new FileReader(sourceStr));
BufferedWriter bw=new BufferedWriter(new FileWriter(destStr));
String line="";
while((line=br.readLine())!=null) {
bw.write(line);//将内容缓存到字符输出流中
bw.newLine();//写入一个换行符
bw.flush(); //刷新后将流中缓存的内容写入到文件中
}
bw.close();
br.close();
}
//使用字符缓冲流一次读写一个字符数组
static void method2(String sourceStr,String destStr) throws IOException {
BufferedReader br=new BufferedReader(new FileReader(sourceStr));
BufferedWriter bw=new BufferedWriter(new FileWriter(destStr));
char[] chs=new char[1024];
int len=0;
while((len=br.read(chs))!=-1) {
bw.write(chs,0,len);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
//字符缓冲流一次读写一个字符
static void method3(String sourceStr,String destStr) throws IOException {
BufferedReader br=new BufferedReader(new FileReader(sourceStr));
BufferedWriter bw=new BufferedWriter(new FileWriter(destStr));
int ch=0;
while((ch=br.read())!=-1) {
bw.write(ch);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
//基本字符流一次读写一个字符数组
static void method4(String sourceStr,String destStr) throws IOException {
FileReader fr=new FileReader(sourceStr);
FileWriter fw=new FileWriter(destStr);
char[] chs=new char[1024];
int len=0;
while((fr.read(chs))!=-1) {
fw.write(chs,0,len);
fw.flush();
}
fw.close();
fr.close();
}
//基本字符流一次读写一个字符
static void method5(String sourceStr,String destStr) throws IOException {
FileReader fr=new FileReader(sourceStr);
FileWriter fw=new FileWriter(destStr);
int ch=0;
while((ch=fr.read())!=-1) {
fw.write(ch);
fw.flush();
}
fw.close();
fr.close();
}
}
4:复制图片:4种方式
package pFile;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 4:复制图片:4种方式
*
* 图片文件应使用字节流
* @author 花花
*
*/
public class CopyImage {
public static void main(String[] args) throws IOException {
method1("D:\\images\\16276.jpg","娃娃.jpg");
method2("D:\\images\\16276.jpg","D:\\果果.jpg");
}
//使用字节缓冲流一次读取一个字节数组
public static void method1(String sourceStr,String destStr) throws IOException {
BufferedInputStream bi=new BufferedInputStream(new FileInputStream(sourceStr));
BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream(destStr));
byte[] bys=new byte[1024];
int len;//实际读取的字节数
while((len=bi.read(bys))!=-1) {
bo.write(bys,0,len);
}
bo.close();
bi.close();
}
//使用字节缓冲流一读取一个字节
public static void method2(String sourceStr,String destStr) throws IOException {
BufferedInputStream bi=new BufferedInputStream(new FileInputStream(sourceStr));
BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream(destStr));
byte[] bys=new byte[1024];
int len;//实际读取的字节数
while((len=bi.read(bys))!=-1) {
bo.write(bys,0,len);
}
bo.close();
bi.close();
}
//使用基本流一次读写一个字节数组
public static void method3(String sourceStr,String destStr) throws IOException {
FileInputStream fis=new FileInputStream(sourceStr);
FileOutputStream fos=new FileOutputStream(destStr);
byte[] bys=new byte[1024];
int len;
while((len=fis.read(bys))!=-1) {
fos.write(bys,0,len);
}
fos.close();
fis.close();
}
//使用基本流一次读写一个字节
public static void method4(String sourceStr,String destStr) throws IOException {
FileInputStream fis=new FileInputStream(sourceStr);
FileOutputStream fos=new FileOutputStream(destStr);
int by=0;
while((by=fis.read())!=-1) {
fos.write(by);
}
fos.close();
fis.close();
}
}
5:已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”
请编写程序读取数据内容,把数据排序后写入ss.txt中。
package pFile;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.TreeSet;
/**
* 5:已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”
请编写程序读取数据内容,把数据排序后写入ss.txt中。
* @author 花花
*
*/
public class readFileSort {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("s.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("ss.txt"));
TreeSet<Integer> ts=new TreeSet<Integer>();//可实现元素的自然排序
int ch=0;
while((ch=br.read())!=-1) {
ts.add(ch);
}
for(int i:ts) {
bw.write(i);
bw.flush();
}
br.close();
bw.close();
}
}
6:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
package pFile;
import java.text.NumberFormat;
/**
* 6:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
* @author 花花
*
*/
public class Student {
private String name;
float chinese;
float math;
float english;
String average;
public Student() {
super();
}
public Student(String name, float chinese, float math, float english) {
super();
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
this.setAverage();
}
/**
* @return name
*/
public String getName() {
return name;
}
/**
* @param name 要设置的 name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return chinese
*/
public float getChinese() {
return chinese;
}
/**
* @param chinese 要设置的 chinese
*/
public void setChinese(float chinese) {
this.chinese = chinese;
}
/**
* @return math
*/
public float getMath() {
return math;
}
/**
* @param math 要设置的 math
*/
public void setMath(float math) {
this.math = math;
}
/**
* @return english
*/
public float getEnglish() {
return english;
}
/**
* @param english 要设置的 english
*/
public void setEnglish(float english) {
this.english = english;
}
/**
* @return average
*/
public String getAverage() {
return average;
}
/**
* @param average 要设置的 average
*/
public void setAverage() {
NumberFormat nf=NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);//保留两位小数
float ave = (this.chinese+this.math+this.english)/3;
this.average=nf.format(ave);
//this.average=ave;
}
@Override
public String toString() {
StringBuffer sb=new StringBuffer();
sb.append("姓名:"+name+" 语文:"+chinese+" 数学:"+math+" 英语:"+english+" 总分:"+average);
return sb.toString();
}
}
package pFile;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
/**
* 6:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
* @author 花花
*
*/
public class SetSort {
public static void main(String[] args) throws IOException {
System.out.println("请输入5个学生信息(姓名,语文成绩,数学成绩,英语成绩):");
Scanner sc=new Scanner(System.in);
BufferedWriter bw=new BufferedWriter(new FileWriter("score.txt"));
TreeSet<Student> tsStudent=new TreeSet<Student>(new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
//int grade=(int)(o2.getAverage()-o1.getAverage());
int grade=(int)(Float.valueOf(o2.getAverage())-Float.valueOf(o1.getAverage()));
//总分相同比较姓名,姓名不同说明不是同一个人,加入集合
int nameFlag=1;//姓名不同
if(o1.getName().equals(o2.getName())) {
nameFlag=0;//姓名相同
}
int name=grade==0 ? nameFlag:grade;
return name;
}
});
for(int i=0;i<5;i++) {
Student s=new Student(sc.next(),Float.parseFloat(sc.next()),
Float.parseFloat(sc.next()),Float.parseFloat(sc.next()));
tsStudent.add(s);
}
for(Student s:tsStudent) {
String str=s.toString();
//System.out.println(str);
bw.write(str);
bw.newLine();
bw.flush();
}
bw.close();
}
}
score.txt