11.25java作业

来源:互联网 发布:哪的java技术培训好 编辑:程序博客网 时间:2024/06/05 19:11

1.复制文本文件:有5种方式

答: 1)        字节输入流:FileInputStream

                  字节输出流: FileOutputStream

       2)      字节缓冲输入流:BufferedInputStream

                  字节缓冲输出流: BufferedOutputStream

         3)  字符输入流:InputStreamReader

                  字符输出流:OutputStreamWriter 

         4)      字符转换输入流:FileReader

                   字符转换输出流:FileWriter

         5)      字符缓冲输入流:BufferedReader

                   字符缓冲输出流:BufferedWriter                    

2.复制图片:4种方式

答:1) 字节缓冲输入流:BufferedInputStream

                    字节缓冲输出流: BufferedOutputStream

         3)  字符输入流:InputStreamReader

                  字符输出流:OutputStreamWriter 

         4)      字符转换输入流:FileReader

                   字符转换输出流:FileWriter

         5)      字符缓冲输入流:BufferedReader

                  字符缓冲输出流:BufferedWriter           

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;

 

publicclass Test1 {

       publicstaticvoid main(String[]args) throws IOException {

              //创建缓冲字符输入流

              BufferedReader br = new BufferedReader(new FileReader("s.txt"));

              //将数据存在一个字符串中

              String s = br.readLine();

              //关闭资源

              br.close();

              //把字符串转换成字符数组

              char []chs = s.toCharArray();

              //对字符数组进行排序

              Arrays.sort(chs);

              //将字符数组转换成字符串

              String result = new String(chs);

             

              //把字符串写入文件中,字符缓冲输出流

              BufferedWriter bw = new BufferedWriter(new FileWriter("ss.txt"));

              bw.write(result);

              bw.close();

       }

}

排序前:

排序后:

4.键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件

publicclass Student {

      

       private Stringname ;//姓名

       privateintchinese ;//语文

       privateintmath ;//数学

       privateintenglish;//英语

       public Student() {

              super();

             

       }

       public Student(Stringname, intchinese,intmath,intenglish) {

              super();

              this.name =name;

              this.chinese =chinese;

              this.math =math;

              this.english =english;

       }

      

       public String getName() {

              returnname;

       }

      

       publicvoid setName(Stringname) {

              this.name =name;

       }

       publicint getChinese() {

              returnchinese;

       }

       publicvoid setChinese(intchinese) {

              this.chinese =chinese;

       }

       publicint getMath() {

              returnmath;

       }

       publicvoid setMath(intmath) {

              this.math =math;

       }

       publicint getEnglish() {

              returnenglish;

       }

       publicvoid setEnglish(intenglish) {

              this.english =english;

       }

      

      

       //总分的方法

       publicint getSum(){

              returnthis.chinese + this.math+this.english ;

       }

 

}

 

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Comparator;

import java.util.Scanner;

import java.util.TreeSet;

 

/**

 * 键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件

 *  

 * 分析:

 *             1)定义学生类

 *             2)创建TreeSet集合对象

 *             3)键盘录入学生信息存储到集合

 *             4)遍历集合,把数据写到文本文件

 *

 */

public class Test2 {

        

         publicstatic void main(String[] args) throws IOException {

                  

                   //创建TreeSet集合对象,使用有参构造

                   //比较器排序匿名内部类的方式

                   TreeSet<Student>ts = new TreeSet<Student>(new Comparator<Student>() {

 

                            @Override

                            publicint compare(Student s1, Student s2) {

                                     //主要条件:总分从高到到第进行排序

                                     intnum = s2.getSum() - s1.getSum();

 

                                     //总分相同,不一定语文成绩相同,比较语文成绩

                                     intnum2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;

 

                                     //总分相同,语文成绩相同,比较数学成绩

                                     intnum3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;

 

                                     //总分相同,语文成绩相同,数学相同,比较英语

                                     intnum4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3;

 

                                     //总分以及各科成绩都相同,不一定是同一个人,姓名内容是否相同

                                     intnum5 = num4 == 0 ? s1.getName().compareTo(s2.getName())

                                                        :num4;

                                     returnnum5;

                            }

                   });

 

                   System.out.println("录入学生信息开始:");

                   //键盘录入5个学生的信息,姓名,语文成绩,数学成绩,英语成绩

                   for(int x = 1; x <= 5; x++) {

                            //创建键盘录入对象

                            //为了方便录入数据,数据类型都使用String类型接收

                            Scannersc = new Scanner(System.in);

                            System.out.println("请输入第" +x + "个学生的姓名:");

                            Stringname = sc.nextLine();

                            System.out.println("请输入第" +x + "个学生的语文成绩:");

                            StringchineseString = sc.nextLine();

                            System.out.println("请输入第" +x + "个学生的数学成绩:");

                            StringmathString = sc.nextLine();

                            System.out.println("请输入第" +x + "个学生的英语成绩:");

                            StringenglishString = sc.nextLine();

 

                            //创建一个学生对象,把这些信息封装到学生对象中

                            Students = new Student();

                            s.setName(name);

                            s.setChinese(Integer.parseInt(chineseString));

                            s.setMath(Integer.parseInt(mathString));

                            s.setEnglish(Integer.parseInt(englishString));

 

                            //将学生对象添加到集合中

                            ts.add(s);

                   }

 

                   //便利集合,把数据写入文本文件

                   BufferedWriterbw = new BufferedWriter(new FileWriter("student.txt"));

                   bw.write("学生信息如下:");

                   bw.newLine();

                   bw.flush();

                   bw.write("姓名,语文成绩,数学成绩,英语成绩");

                   bw.newLine();

                   bw.flush();

                   for(Students : ts) {

                            StringBuildersb = new StringBuilder();

                            sb.append(s.getName()).append(",").append(s.getChinese()).append(",")

                            .append(s.getMath()).append(",").append(s.getEnglish());

                   bw.write(sb.toString());

                   bw.newLine();

                   bw.flush();

                  

                   }

                   bw.close();

         }

}

运行结果:

原创粉丝点击