java第五次作业

作业1:

GUI界面如下(用beautieye渲染了一下,哈~):

代码如下。整体思路以及实现方法在代码中以注释指出,GUI界面很简单,就不写了。代码如下:

 1 package com.cumin;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.io.ObjectInputStream.GetField;
 6 
 7 public class FileUtils {
 8     private static String[] filenames; // 存储文件名
 9 
10     // 编写两个静态方法方便调用
11 
12     /*
13      * 1.方法fileDir参数传递文件路径 
     * 2.判断file,如果不存在或不是目录并抛出异常
14 * 3.使用list()方法,并for-each遍历filenames
* 4.最后返回mes
15 */ 16 public static String fileDir(File file) throws IOException { 17 if (!file.exists()) { 18 System.out.println("目录不存在"); 19 throw new IOException(); 20 } 21 if (!file.isDirectory()) { 22 System.out.println(file + "不是目录"); 23 throw new IOException(); 24 } 25 filenames = file.list(); 26 String mes = ""; 27 for (String str : filenames) { 28 mes += str + '\n'; 29 } 30 return mes; 31 } 32 33 /* 34 * 1.方法eachFileDir参数传递文件路径 35 * 2.利用filenames[i].substring(filenames[i].lastIndexOf("."))获取文件后缀名 36 * 3.判断后返回mes 37 */ 38 public static String eachFileDir(String str) throws IOException { 39 String mes = ""; 40 for (int i = 0; i < filenames.length; i++) { 41 42 // 获取文件后缀名 43 String postfix = filenames[i].substring(filenames[i].lastIndexOf(".")); 44 45 if (postfix.equalsIgnoreCase(str)) { 46 mes += filenames[i] + '\n'; 47 } 48 } 49 return mes; 50 } 51 }
package com.cumin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class ListFilesUI extends JFrame {
    private JFrame main_frame;
    private JTextArea list_area;
    private JLabel text_msg;
    private JPanel main_panel;
    private JPanel north_panel;
    private JComboBox<String> box;

    public ListFilesUI() {
        makeFrame();
    }

    public void makeFrame() {
        // 声明
        main_frame = new JFrame("test");
        main_panel = new JPanel(new BorderLayout());
        north_panel = new JPanel(new GridLayout(2, 1));
        text_msg = new JLabel("files list");
        list_area = new JTextArea();
        JScrollPane jsb = new JScrollPane(list_area);

        list_area.setEnabled(false);/* list_area设置不可编辑 */
        try {
            // 调用fileDir获取文件里面的所有文件内容
            list_area.setText(FileUtils.fileDir(new File("audio")));
        } catch (IOException e) {
            e.printStackTrace();
        }

        box = new JComboBox<>();
        String[] item = { "all", ".wav", ".mp3", ".au", ".aif" };
        for (int i = 0; i < item.length; i++) {
            box.addItem(item[i]);
        }
        // 组合框加监听器
        box.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    // 获取组合框的itmename
                    String item = box.getItemAt(box.getSelectedIndex());
                    if (item.equalsIgnoreCase("all")) {
                        // 调用静态方法eachFileDir筛选匹配的字符串
                        list_area.setText(FileUtils.fileDir(new File("audio")));
                    } else {
                        list_area.setText(FileUtils.eachFileDir(box.getItemAt(box.getSelectedIndex())));
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });

        // 其他
        north_panel.add(text_msg);
        north_panel.add(box);
        main_panel.add(north_panel, BorderLayout.NORTH);
        main_panel.add(jsb, BorderLayout.CENTER);
        main_frame.add(main_panel);

        main_frame.setSize(250, 350);
        main_frame.setVisible(true);
        main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        // 使用beautieye渲染UI
        try {
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
        } catch (Exception e) {
        }
        new ListFilesUI();

    }
}

结果图片如下:

作业2:

主要利用seek()方法跳转指针 和getFilePointer()方法获取当前指针位置。

贴代码:

   

 1 import java.io.File;
 2 import java.io.IOException;
 3 import java.io.RandomAccessFile;
 4 
 5 public class TestRandomAccessFile {
 6     private File file;
 7 
 8     public static void main(String[] args) {
 9         TestRandomAccessFile traf = new TestRandomAccessFile();
10         traf.init();
11         traf.record("Adom", 120);
12         traf.listAllRecords();
13     }
14 
15     public void record(String record_breaker, int times) {
16         try {
17             RandomAccessFile raf = new RandomAccessFile(file, "rw");
18             boolean flag = false;
19             while (raf.getFilePointer() < raf.length()) {
20                 String name = raf.readUTF();
21                 //记录当前的指针位置!!!(重点)
22                 long prior = raf.getFilePointer();
23                 if (record_breaker.equalsIgnoreCase(name)) {
24                     //判断名字相同后设置flag,否则raf会继续往下读。
25                     flag = true;
26                     //判断传递进来的参数与之前分数的大小,决定是否重写
27                     if (raf.readInt() < times) {
28                         //利用seek()方法跳转到prior的位置! (重点)
29                         raf.seek(prior);
30                         raf.writeInt(times);
31                         break;
32                     }
33                 } else {
34                     raf.skipBytes(4);
35                 }
36             }
37              if (!flag) {
38              raf.writeUTF(record_breaker);
39              raf.writeInt(times);
40              }
41             raf.close();
42         } catch (Exception e) {
43             e.printStackTrace();
44         }
45     }
46 
47     public void init() {
48         if (file == null) {
49             file = new File("record.txt");
50             try {
51                 file.createNewFile();
52             } catch (IOException e) {
53                 e.printStackTrace();
54             }
55         }
56     }
57 
58     public void listAllRecords() {
59         try {
60             RandomAccessFile raf = new RandomAccessFile(file, "r");
61             while (raf.getFilePointer() < raf.length()) {
62                 String name = raf.readUTF();
63                 int times = raf.readInt();
64                 System.out.println("name:" + name + "\trecord:" + times);
65             }
66             raf.close();
67         } catch (Exception e) {
68             e.printStackTrace();
69         }
70     }
71 }

结果贴图如下:

测试分数大小是否成功:

交换选手,测试无BUG 。

 

当然其他选手的分数只要比之前高就会在相应的位置重写,就不一一列举了。

                                                          Lcumin' 2016.4.15 中午

--------------------------------------------我是分割线----------------------------------------------------------------

本次作业我提交的有点仓促,错别字请老师见谅!!!                                    

                

posted @ 2016-04-15 13:13  Lcumin  阅读(313)  评论(1编辑  收藏  举报