//1.把一个文件复制 10 份
public class MyTest {
public static void main(String[] args) throws IOException {
BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream("树木.mp3"));
BufferedOutputStream out1 =new BufferedOutputStream(new FileOutputStream("树木1.mp3"));
BufferedOutputStream out2 =new BufferedOutputStream(new FileOutputStream("树木2.mp3"));
BufferedOutputStream out3 =new BufferedOutputStream(new FileOutputStream("树木3.mp3"));
BufferedOutputStream out4 =new BufferedOutputStream(new FileOutputStream("树木4.mp3"));
BufferedOutputStream out5 =new BufferedOutputStream(new FileOutputStream("树木5.mp3"));
BufferedOutputStream out6 =new BufferedOutputStream(new FileOutputStream("树木6.mp3"));
BufferedOutputStream out7 =new BufferedOutputStream(new FileOutputStream("树木7.mp3"));
BufferedOutputStream out8 =new BufferedOutputStream(new FileOutputStream("树木8.mp3"));
BufferedOutputStream out9 =new BufferedOutputStream(new FileOutputStream("树木9.mp3"));
int len = 0;
byte[] bytes = new byte[1024 * 8];
while ((len= in.read(bytes)) != -1) {
out.write(bytes,0,len);
}
in.close();
out.close();
}
}
public class MyTest {
public static void main(String[] args) throws IOException {
//把一个文件复制多份
RandomAccessFile in = new RandomAccessFile("C:\\Users\\ShenMouMou\\Desktop\\mv.jpg","rw");
byte[] bytes = new byte[1024 * 8];
int len=0;
for (int i = 1; i <=10; i++) {
FileOutputStream out = new FileOutputStream(new File("C:\\Users\\ShenMouMou\\Desktop\\", i + "mv.jpg"));
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
}
out.close();
in.seek(0);
}
in.close();
}
}
//2.需求:我有一个文本文件,我知道数据是键值对形式的,但是不知道内容是什么。 请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其值为”100”
public class MyTest {
public static void main(String[] args) throws IOException {
TreeSet treeSet = new TreeSet<>(new Comparator() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getTotalScore() - s2.getTotalScore();
int num2=num==0?s1.getName().compareTo(s2.getName()):num;
return -num2;
}
});
for (int i = 0; i < 1; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第"+i+"个学生的姓名");
String name = sc.nextLine();
System.out.println("请输入第"+i+"个学生的语文成绩");
int yw = sc.nextInt();
System.out.println("请输入第"+i+"个学生的数学成绩");
int sx = sc.nextInt();
System.out.println("请输入第"+i+"个学生的英语成绩");
int yy = sc.nextInt();
Student student = new Student(name,yw,sx,yy);
treeSet.add(student);
}
BufferedWriter writer= new BufferedWriter(new FileWriter("score.txt"));
String title = "序号\t姓名\t语文\t数学\t英语\t总分";
System.out.println(title);
// writer.writer();
writer.write(title);
writer.newLine();
writer.flush();
int i =0;
for (Student student : treeSet) {
String name = student.getName();
int chineseCore = student.getChineseScore();
int mathScore = student.getMatchScore();
int englishScore = student.getEnglishScore();
int totalScore = student.getTotalScore();
System.out.println((i++)+"\t"+name+"\t"+chineseCore+"\t"+mathScore+"\t"+totalScore+"\t");
writer.write(i + “\t” + name + “\t” +chineseCore + “\t” + mathScore + “\t” + englishScore + “\t” + totalScore);
writer.newLine();
writer.flush();
}
writer.close();
System.out.println("录入完毕");
//课后作业:把数据导入到excel
//https://www.cnblogs.com/qinda/p/10932277.html
}
}
public class Student {
private String name;
private int chineseScore;
private int matchScore;
private int englishScore;
public Student(String name, int chineseScore, int matchScore, int englishScore) {
this.name = name;
this.chineseScore = chineseScore;
this.matchScore = matchScore;
this.englishScore = englishScore;
}
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChineseScore() {
return chineseScore;
}
public void setChineseScore(int chineseScore) {
this.chineseScore = chineseScore;
}
public int getMatchScore() {
return matchScore;
}
public void setMatchScore(int matchScore) {
this.matchScore = matchScore;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
public int getTotalScore(){
return chineseScore+matchScore+englishScore;
}
}
//3.删除目录 通过File方法
public class MyTest02 {
public static boolean delete1(File file){
/*
判断一下file对象是否是存在和是否是一个目录。
*/
if(file.exists() && file.isDirectory()){
//转为String数组存储
String[] str = file.list();
//遍历
for (String f : str) {
//利用File(File parent, String child)方法找到目录下的子文件
File subFile = new File(file,f);
//然后递归
delete1(subFile);
}
}
return file.delete();
}
public static void main(String[] args) {
File file = new File("E:\\java_test\\pipeline");
System.out.println(delete1(file));
}
}
输出日志:
true
//4.递归删除
public class Work3 {
public static void main(String[] args) {
File file = new File("E:\\java_test\\test001");
// boolean b = file.delete();
// System.out.println(b);
deleteFolder(file);
}
private static void deleteFolder(File file) {
File[] files = file.listFiles();
for (File f:files) {
if (f.isFile()) {
f.delete();
} else{
deleteFolder(f);
}
}
}
}
public class Mytest {
public static void main(String[] args) {
//修改文件的后缀名
File file = new File(“E:\java_test\pipeline2 - 副本 - 副本\sougou\美女”);
//递归修改
updateFiles(file);
}
private static void updateFiles(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
if (f.isFile() && f.getName().endsWith("jpg")) {
//获取父路径
String parent = f.getParent();
//获取文件名
String name = f.getName();
// System.out.println(name);
//修改文件名
name = name.substring(0, name.lastIndexOf(".")) + “.png”;
// System.out.println(name);
//构建目标文件,跟源文件目标保持一致
File targetFile = new File(parent, name);
f.renameTo(targetFile);
// String parent = f.getParent();
// System.out.println(parent);
// File targetFile = new File();
// f.renameTo(targetFile);
}
else {
//递归 传过去的文件夹里面,会有文件夹和文件
updateFiles(f);
}
}
}
}
}