1、目录、文件操作
(1)在C盘下建立一个目录dir1
(2)在目录dir1下建立文本文件1.txt,并在里面输入内容。
(3)输出1.txt文件的大小及最后修改日期。
(4)将1.txt重命名为2.txt。
(5)将目录dir1删除。
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception {
File dir1 = new File("C:/dir1");
if (!dir1.exists()) {
dir1.mkdir(); // 创建单一目录
}
if (dir1.exists()) {
File f1 = new File("C:/dir1/1.txt");
f1.createNewFile();
FileWriter fw = new FileWriter("C:/dir1/1.txt");
fw.write("风吹落最后一片叶");
fw.close(); // 关闭
System.out.print("1.txt文件大小:" + f1.length());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // 格式化日期格式
Date date1 = new Date(f1.lastModified()); // 将文件的最后修改日期转为日期格式
String lastmodified1 = sdf.format(date1); // 日期格式化
System.out.println(",最后修改日期:" + lastmodified1);
File f2 = new File("C:/dir1/2.txt"); // 创建要被重命名的目标文件,以便重命名
f1.renameTo(f2); // 重命名指向目标文件
System.out.print("2.txt文件大小:" + f2.length());
Date date2 = new Date(f2.lastModified());
String lastmodified2 = sdf.format(date2);
System.out.println(",最后修改日期:" + lastmodified2);
f2.delete();
dir1.delete(); //删除dir1目录
}
}
}
2、单词计数
统计文件中单词出现次数,单词间以空格,tab或回车间隔。
import java.io.File;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) throws Exception {
File dr = new File("C:/wordcount");
if (!dr.exists()) {
dr.mkdir();
}
File f1 = new File("C:/wordcount/1.txt");
if (!f1.exists()) {
f1.createNewFile();
}
Scanner sc = new Scanner(new File("C:/wordcount/1.txt")); // 从文件输入,需要文件中已有内容
Map<String, Integer> map = new TreeMap<String, Integer>();
while (sc.hasNext()) {
String word = sc.next();
Integer num = map.get(word);
if (num == null) {
map.put(word, 1);
} else {
map.put(word, num + 1);
}
}
for (Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
}
3、图片还原
文件1.jpg经过如下操作后变为2.jpg,请还原图片1.jpg。
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Test {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("c:/1.jpg");
FileOutputStream fos = new FileOutputStream("c:/2.jpg");
while (true) {
int a = fis.read();
if (a == -1) {
break;
} else {
fos.write(a ^ 100);
/*(按位异或运算符(^)是一个二元运算符,要化为二进制进行计算,在运算的两个元中,两个相同位相同,则结果为0,否则为1)*/
}
}
fos.close();
fis.close();
}
}
还原代码如下:
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Main {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("C:/2.jpg");
FileOutputStream fos = new FileOutputStream("C:/1.jpg");
while (true) {
int a = fis.read();
if (a == -1) {
break;
} else {
fos.write(a ^ 100);
/*(按位异或运算符(^)是一个二元运算符,要化为二进制进行计算,在运算的两个元中,两个相同位相同,则结果为0,否则为1)*/
}
}
fos.close();
fis.close();
}
}
4、学生记录排序
文件1.txt :1.txt
1.txt中保存了若干同学的成绩信息,按如下方式对记录排序再存回到1.txt中。
按班号升序排列,班号相同按成绩降序排列,参考效果:
/*
这里读写文件会导致中文因编码格式不一样出现乱码,我提前在Eclipse菜单Edit - setencoding将格式改为UTF-8,或者写入记事本格式时的最后换行部分改为\n\r
*/
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(new File("C:/1.txt"));
sc.nextLine(); //跳过标题行
List<Student> ss = new ArrayList<Student>();
while (sc.hasNext()) {
Student student = new Student(sc.next(), sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt());
ss.add(student);
}
sc.close();
Collections.sort(ss, new Comparator1());
FileWriter fw = new FileWriter("C:/2.txt");
fw.write("学号\t姓名\t班号\t数学\t语文\t英语\t总分\n");
for (Student s : ss) {
fw.write(s.toString());
}
fw.close();
new File("C:/1.txt").delete();
new File("C:/2.txt").renameTo(new File("C:/1.txt"));
}
}
class Student {
String sno, sname;
int class_number, math, chinese, english, sum;
public Student(String sno, String sname, int class_number, int math, int chinese, int english) {
super();
this.sno = sno;
this.sname = sname;
this.class_number = class_number;
this.math = math;
this.chinese = chinese;
this.english = english;
this.sum = this.math + this.chinese + this.english;
}
@Override
public String toString() {
return sno + "\t" + sname + "\t" + class_number + "\t" + math + "\t" + chinese + "\t" + english + "\t" + sum + "\n";
}
}
class Comparator1 implements Comparator<Student> {
public int compare(Student o1, Student o2) {
if (o1.class_number == o2.class_number) { // 若班号相同
return o2.sum - o1.sum; // 总成绩降序排列
}
return o1.class_number - o2.class_number; // 按班号升序排列
}
}
5、出题
随机出100道100内的整数加减乘除题,保存到两个文本文件中,一个不带答案,一个带答案。
减法结果大于等于0,除数不能为0,且要能除尽。