作业一

 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参数传递文件路径 
14      * 2.判断file,如果不存在或不是目录并抛出异常
15      * 3.使用list()方法,并for-each遍历filenames 
16         * 4.最后返回mes
17      */
18     public static String fileDir(File file) throws IOException {
19         if (!file.exists()) {
20             System.out.println("目录不存在");
21             throw new IOException();
22         }
23         if (!file.isDirectory()) {
24             System.out.println(file + "不是目录");
25             throw new IOException();
26         }
27         filenames = file.list();
28         String mes = "";
29         for (String str : filenames) {
30             mes += str + '\n';
31         }
32         return mes;
33     }
34 
35     /*
36      * 1.方法eachFileDir参数传递文件路径
37      * 2.利用filenames[i].substring(filenames[i].lastIndexOf("."))获取文件后缀名
38      * 3.判断后返回mes
39      */
40     public static String eachFileDir(String str) throws IOException {
41         String mes = "";
42         for (int i = 0; i < filenames.length; i++) {
43             
44             // 获取文件后缀名
45             String postfix = filenames[i].substring(filenames[i].lastIndexOf("."));
46             
47             if (postfix.equalsIgnoreCase(str)) {
48                 mes += filenames[i] + '\n';
49             }
50         }
51         return mes;
52     }
53 }
 1 package com.cumin;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.awt.event.ActionEvent;
 6 import java.awt.event.ActionListener;
 7 import java.io.File;
 8 import java.io.IOException;
 9 
10 public class ListFilesUI extends JFrame {
11     private JFrame main_frame;
12     private JTextArea list_area;
13     private JLabel text_msg;
14     private JPanel main_panel;
15     private JPanel north_panel;
16     private JComboBox<String> box;
17 
18     public ListFilesUI() {
19         makeFrame();
20     }
21 
22     public void makeFrame() {
23         // 声明
24         main_frame = new JFrame("test");
25         main_panel = new JPanel(new BorderLayout());
26         north_panel = new JPanel(new GridLayout(2, 1));
27         text_msg = new JLabel("files list");
28         list_area = new JTextArea();
29         JScrollPane jsb = new JScrollPane(list_area);
30 
31         list_area.setEnabled(false);/* list_area设置不可编辑 */
32         try {
33             // 调用fileDir获取文件里面的所有文件内容
34             list_area.setText(FileUtils.fileDir(new File("audio")));
35         } catch (IOException e) {
36             e.printStackTrace();
37         }
38 
39         box = new JComboBox<>();
40         String[] item = { "all", ".wav", ".mp3", ".au", ".aif" };
41         for (int i = 0; i < item.length; i++) {
42             box.addItem(item[i]);
43         }
44         // 组合框加监听器
45         box.addActionListener(new ActionListener() {
46 
47             @Override
48             public void actionPerformed(ActionEvent e) {
49                 try {
50                     // 获取组合框的itmename
51                     String item = box.getItemAt(box.getSelectedIndex());
52                     if (item.equalsIgnoreCase("all")) {
53                         // 调用静态方法eachFileDir筛选匹配的字符串
54                         list_area.setText(FileUtils.fileDir(new File("audio")));
55                     } else {
56                         list_area.setText(FileUtils.eachFileDir(box.getItemAt(box.getSelectedIndex())));
57                     }
58                 } catch (IOException e1) {
59                     e1.printStackTrace();
60                 }
61             }
62         });
63 
64         // 其他
65         north_panel.add(text_msg);
66         north_panel.add(box);
67         main_panel.add(north_panel, BorderLayout.NORTH);
68         main_panel.add(jsb, BorderLayout.CENTER);
69         main_frame.add(main_panel);
70 
71         main_frame.setSize(250, 350);
72         main_frame.setVisible(true);
73         main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
74 
75     }
76 
77     public static void main(String[] args) {
78         // 使用beautieye渲染UI
79         try {
80             org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
81         } catch (Exception e) {
82         }
83         new ListFilesUI();
84 
85     }
86 }

作业2:

 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 }

posted @ 2016-04-17 17:49  vtrois  阅读(145)  评论(0编辑  收藏  举报