Java homework9

来源:互联网 发布:管理决策模拟大赛 知乎 编辑:程序博客网 时间:2024/06/05 02:58
编程题:
1.复制文本文件:有5种方式
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 Copy {
public static void main(String[] args) throws IOException {
// method1("a.txt","b1.txt");
// method2("a.txt","b2.txt");
// method3("a.txt","b3.txt");
// method4("a.txt","b4.txt"); 
method5("a.txt","b5.txt");


}
        //字符缓冲流
private static void method5(String srcString, String destString) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
// 一次读取一个字符数组
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
bw.write(chs,0,len);
}
// 关闭资源
br.close();
bw.close();
}
//   高效字节流(字节缓冲输入流)一次读取一个字节数组
// 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();
// }
//  基本字节流(FileInputStream)一次读取一个字节数组
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(bys)) != -1) {
fos.write(bys, 0, len);
}
fis.close();
fos.close();
}
//   基本字节流(FileInputStream)一次读取一个字节
// private static void method1(String srcString, String destString) throws
// IOException {
// // TODO Auto-generated method stub
// 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() ;
// }
}     
  
2.复制图片:4种方式
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
// method1("a.png","a1.png");
method2("a.png","a2.png");
// method3("a.png","a3.png");
// method4("a.png","a4.png"); 
}
//   高效字节流(字节缓冲输入流)一次读取一个字节数组
// 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();
// }
//  基本字节流(FileInputStream)一次读取一个字节数组
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(bys)) != -1) {
fos.write(bys, 0, len);
}
fis.close();
fos.close();
}
//   基本字节流(FileInputStream)一次读取一个字节
// private static void method1(String srcString, String destString) throws
// IOException {
// // TODO Auto-generated method stub
// 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() ;
// }
}


3.已知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;
//已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”
//请编写程序读取数据内容,把数据排序后写入ss.txt中。
//1.创建文件s.txt
//2.读取文件中的数据
//3.把数据存在一个字符串中
//4.把字符串转换成字符串数组
//5.对字符串数组进行排序
//6.数组转换成字符串
//7.把字符串写入文件ss.txt中
//8.把ss.txt中内容打印到控制台
public class StringDemo {
public static void main(String[] args) throws IOException {
// 创建文件s.txt 用字符缓冲输入流
BufferedWriter bw = new BufferedWriter(new FileWriter("s.txt"));
// 添加字符串
bw.write("hcexfgijkamdnoqrzstuvwybpl");
// 刷新
bw.flush();
// 释放资源
bw.close();
// 读取文件中的数据 字符缓冲输入流
BufferedReader br = new BufferedReader(new FileReader("s.txt"));
// 一次读取一行,把数据存入一个字符串中
String s = br.readLine();
// 释放资源
br.close();
// 把字符串转换成字符串数组
char[] chs = s.toCharArray();
// 对字符串数组进行排序
Arrays.sort(chs);
// 把数组转换成字符串
String str = new String(chs);
// 把字符串写入文件中 用字符缓冲输出流
BufferedWriter bwr = new BufferedWriter(new FileWriter("ss.txt"));
bwr.write(str);
bwr.flush();
bwr.close();
// 打印到控制台
BufferedReader brr = new BufferedReader(new FileReader("ss.txt"));
// 一次读取一行,把数据存入一个字符串中
String line = brr.readLine();
// 打印
System.out.println(line);
// 释放资源
brr.close();
}
}
4.键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Comparator;
import java.util.TreeSet;
public class StudentScores {
public static void main(String[] args) throws IOException,ClassNotFoundException {
// 创建一个TreeSet集合
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.getChinesescore() - s2.getChinesescore() : num;
// 总分相同的不一定数学相同
int num3 = num2 == 0 ? s1.getMathscore() - s2.getMathscore() : num2;
// 总分相同的不一定英语相同
int num4 = num3 == 0 ? s1.getEnglishscore() - s2.getEnglishscore() : num3;
// 姓名还不一定相同呢
int num5 = num4 == 0 ? s1.getName().compareTo(s2.getName()) : num4;
return num5;
}
});
System.out.println("录入学生信息:");
// 键盘录入5个学生信息
for (int x = 1; x <= 5; x++) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入第" + x + "个学生的姓名:");
String name = br.readLine();
System.out.println("请输入第" + x + "个学生的语文成绩:");
String chineseScore = br.readLine();
System.out.println("请输入第" + x + "个学生的数学成绩:");
String mathScore = br.readLine();
System.out.println("请输入第" + x + "个学生的英语成绩:");
String englishScore = br.readLine();
// 把数据封装到学生对象中
Student s = new Student();
s.setName(name);
s.setChinesescore(Integer.parseInt(chineseScore));
s.setMathscore(Integer.parseInt(mathScore));
s.setEnglishscore(Integer.parseInt(englishScore));
// 把学生对象添加到集合
ts.add(s);
}
System.out.println("------------------------------------");
System.out.println("学习按照总分从高到低排序如下:");
System.out.println("姓名\t语文成绩\t数学成绩\t英语成绩");
// 遍历集合
for (Student s : ts) {
System.out.println(
s.getName() + "\t" + s.getChinesescore() + "\t" + s.getMathscore() + "\t" + s.getEnglishscore());
}
FileOutputStream fos = new FileOutputStream("Student.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream("Student.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
oos.writeObject(ts);
oos.writeBoolean(true);
}

}


import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = -1574497842445296967L;
// 姓名
private String name;
// 语文成绩
private int chinesescore;
// 数学成绩
private int mathscore;
// 英语成绩
private int englishscore;


public Student() {
super();
// TODO Auto-generated constructor stub
}


public Student(String name, int chinesescore, int mathscore, int englishscore) {
super();
this.name = name;
this.chinesescore = chinesescore;
this.mathscore = mathscore;
this.englishscore = englishscore;
}


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 getMathscore() {
return mathscore;
}


public void setMathscore(int mathscore) {
this.mathscore = mathscore;
}


public int getEnglishscore() {
return englishscore;
}


public void setEnglishscore(int englishscore) {
this.englishscore = englishscore;
}


public int getSum() {
return this.chinesescore + this.mathscore + this.englishscore;
}
}


原创粉丝点击