package com.cumin;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;

public class FileUtils {
    private static String[] filenames; // 存储文件名

    // 编写两个静态方法方便调用

    /*
     * 1.方法fileDir参数传递文件路径 
     * 2.判断file,如果不存在或不是目录并抛出异常
     * 3.使用list()方法,并for-each遍历filenames 
        * 4.最后返回mes
     */
    public static String fileDir(File file) throws IOException {
        if (!file.exists()) {
            System.out.println("目录不存在");
            throw new IOException();
        }
        if (!file.isDirectory()) {
            System.out.println(file + "不是目录");
            throw new IOException();
        }
        filenames = file.list();
        String mes = "";
        for (String str : filenames) {
            mes += str + '\n';
        }
        return mes;
    }

    /*
     * 1.方法eachFileDir参数传递文件路径
     * 2.利用filenames[i].substring(filenames[i].lastIndexOf("."))获取文件后缀名
     * 3.判断后返回mes
     */
    public static String eachFileDir(String str) throws IOException {
        String mes = "";
        for (int i = 0; i < filenames.length; i++) {
            
            // 获取文件后缀名
            String postfix = filenames[i].substring(filenames[i].lastIndexOf("."));
            
            if (postfix.equalsIgnoreCase(str)) {
                mes += filenames[i] + '\n';
            }
        }
        return mes;
    }
}
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();

    }
}
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class TestRandomAccessFile {
    private File file;

    public static void main(String[] args) {
        TestRandomAccessFile traf = new TestRandomAccessFile();
        traf.init();
        traf.record("Adom", 120);
        traf.listAllRecords();
    }

    public void record(String record_breaker, int times) {
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "rw");
            boolean flag = false;
            while (raf.getFilePointer() < raf.length()) {
                String name = raf.readUTF();
                //记录当前的指针位置!!!(重点)
                long prior = raf.getFilePointer();
                if (record_breaker.equalsIgnoreCase(name)) {
                    //判断名字相同后设置flag,否则raf会继续往下读。
                    flag = true;
                    //判断传递进来的参数与之前分数的大小,决定是否重写
                    if (raf.readInt() < times) {
                        //利用seek()方法跳转到prior的位置! (重点)
                        raf.seek(prior);
                        raf.writeInt(times);
                        break;
                    }
                } else {
                    raf.skipBytes(4);
                }
            }
             if (!flag) {
             raf.writeUTF(record_breaker);
             raf.writeInt(times);
             }
            raf.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void init() {
        if (file == null) {
            file = new File("record.txt");
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void listAllRecords() {
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            while (raf.getFilePointer() < raf.length()) {
                String name = raf.readUTF();
                int times = raf.readInt();
                System.out.println("name:" + name + "\trecord:" + times);
            }
            raf.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 

posted on 2016-04-17 21:34  猫尾  阅读(149)  评论(0编辑  收藏  举报