题一:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低打印到控制台。
Student.java
public class Student {
// 姓名
private String name;
// 语文成绩
private int chinese;
// 数学成绩
private int math;
// 英语成绩
private int english;
public Student() {
super();
}
public Student(String name, int chinese, int math, int english) {
super();
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getSum() {
return this.chinese + this.math + this.english;
}
}
StendentDemo.java
java技术学习qun:59,789,1510,每天分享一点java知识干货!/*
* 键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
*
* 分析:
* A:创建学生类
* B:创建集合对象
* TreeSet
* C:键盘录入学生信息存储到集合
* D:遍历集合,把数据写到文本文件
*/
public class StendentDemo {
public static void main(String args[]) throws IOException {
TreeSet students = new TreeSet<>(new Comparator() {
@Override
public int compare(Student s1, Student s2) {
//s1-s2升序,s2-s1降序
int num = s2.getSum() - s1.getSum();
int num1 = num == 0 ?s2.getChinese()-s1.getChinese():num;
int num2 = num1 ==0 ?s2.getMath()-s1.getMath():num1;
int num3 = num2 ==0 ?s2.getEnglish()-s1.getEnglish():num2;
int num4 = num3 == 0?s1.getName().compareTo(s2.getName()):num3;//按字母顺序
return num4;
}
});
for (int i = 0; i <5 ; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("录入学生成绩:");
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 student = new Student(name,chinese,math,english);
students.add(student);
}
BufferedWriter bw = new BufferedWriter(new FileWriter("grade.txt"));
bw.write("学生信息如下:");
bw.newLine();
bw.flush();
bw.write("姓名,语文成绩,数学成绩,英语成绩");
bw.newLine();
bw.flush();
for (Student student : students) {
StringBuilder sb = new StringBuilder();
sb.append(student.getName()).append(student.getChinese()).append(student.getMath()).append(student.getEnglish());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
}
题二:复制单级文件夹
java技术学习qun:59,789,1510,每天分享一点java知识干货!/**
* 封装
* 新建文件夹
* 获得源文件夹下文件列表
* 复制文件到新文件夹
*/
public class copyFolder {
public static void main(String args[]) throws IOException {
File file1 = new File("F:\汤包\IT时代\java基础\day21\code\demo");
File file2 = new File("e:\demo");
//判断文件夹是否存在
if (!file2.exists()){
file2.mkdir();
}
//获取源文件夹下文件列表
File[] files = file1.listFiles();
for (File file : files) {
File newfile = new File(file2,file.getName());
copyFun(file,newfile);
}
}
private static void copyFun(File file1,File file2) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file1));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes,0,len);
}
bis.close();
bos.close();
}
}
题三:复制多极文件夹
/*
* 需求:复制多极文件夹
*
* 数据源:F:汤包IT时代java基础day21codedemos
* 目的地:E:\
*
* 分析:
* A:封装数据源File
* B:封装目的地File
* C:判断该File是文件夹还是文件
* a:是文件夹
* 就在目的地目录下创建该文件夹
* 获取该File对象下的所有文件或者文件夹File对象
* 遍历得到每一个File对象
* 回到C
* b:是文件
* 就复制(字节流)
*/
public class copyFolder2 {
public static void main(String args[]) throws IOException {
File file1 = new File("F:\汤包\IT时代\java基础\day21\code\demos");
File file2 = new File("e:\");
copyFolder(file1,file2);
}
private static void copyFolder(File srcFile, File destFile) throws IOException {
if (srcFile.isDirectory()){
File newFolder = new File(destFile, srcFile.getName());
newFolder.mkdir();
File[] files = srcFile.listFiles();
for (File file1 : files) {
copyFolder(file1,newFolder);
}
}else {
File newFile = new File(destFile,srcFile.getName());
copyFile(srcFile,newFile);
}
}
private static void copyFile(File file1,File file2) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file1));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes,0,len);
}
bis.close();
bos.close();
}
}
复制多级文件夹,构思用到了递归,可知递归真的很重要,之后也会总结一下递归的知识。
java技术学习qun:59,789,1510,每天分享一点java知识干货!题四:已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”,请编写程序读取数据内容,把数据排序后写入ss.txt中。
java技术学习qun:59,789,1510,每天分享一点java知识干货!/*
* 已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”
* 请编写程序读取数据内容,把数据排序后写入ss.txt中。
*
* 分析:
* A:把s.txt这个文件给做出来
* B:读取该文件的内容,存储到一个字符串中
* C:把字符串转换为字符数组
* D:对字符数组进行排序
* E:把排序后的字符数组转换为字符串
* F:把字符串再次写入ss.txt中
*/
public class StringDemo {
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();
}
}
题五:用Reader模拟BufferedReader的readLine()功能
/*
* 用Reader模拟BufferedReader的readLine()功能
*
* readLine():一次读取一行,根据换行符判断是否结束,只返回内容,不返回换行符
*/
public class MyBufferedReader {
private Reader reader;
public MyBufferedReader(Reader reader){
this.reader=reader;
}
public String readLine() throws IOException {
StringBuilder sb = new StringBuilder();
int ch = 0;
while((ch=reader.read())!=-1) {
if (ch==''){
continue;
}
if (ch==''){
return sb.toString();
}else {
sb.append((char)ch);
}
}
if (sb.length()>0){
return sb.toString();
}
return null;
}
public void close() throws IOException {
this.reader.close();
}
}
测试:
MyBufferedReader mbr = new MyBufferedReader(new FileReader("a.txt"));
String line = null;
while ((line = mbr.readLine()) != null) {
System.out.println((line));
}
mbr.close();
通过自己实现readLine()方法,并且查看源码可知,字符缓冲流的readLine()也用到了StringBuilder,并且也要判断 和 ,最后关闭的流就是Reader。
网友评论