Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab4;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.File;
- import java.io.IOException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import java.util.Date;
- public class Lab4 extends JFrame {
- private JButton button = new JButton("Выполнить");
- private JTextField input = new JTextField("", 5);
- private JLabel label = new JLabel("Введите путь к интересующему файлу:");
- private JRadioButton radio1 = new JRadioButton("Войти в каталог");
- private JRadioButton radio2 = new JRadioButton("Создать каталог");
- private JRadioButton radio3 = new JRadioButton("Создать файл");
- private JRadioButton radio4 = new JRadioButton("Удалить");
- private JRadioButton radio5 = new JRadioButton("Вывод свойств");
- private JRadioButton radio6 = new JRadioButton("Открыть файл");
- public Lab4() {
- // задаем название окна
- this.setTitle("Файловый менеджер");
- // задаем координаты и размеры окна
- this.setBounds(100, 100, 400, 200);
- // обработка закрытия окна
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // создаем контейнер (container)
- Container container = this.getContentPane();
- // делим контейнер сеткой 3, 3, 3, 3
- container.setLayout(new GridLayout(3, 3, 3, 3));
- // задаем начальное значение radio1 (Войти в каталог) на true
- radio1.setSelected(true);
- // необходимо для обработки нажатия кнопки (Выполнить)
- button.addActionListener(new ButtonEventListener());
- // добавляем элементы в контейнер (container)
- container.add(label);
- container.add(input);
- container.add(radio1);
- container.add(radio2);
- container.add(radio3);
- container.add(radio4);
- container.add(radio5);
- container.add(radio6);
- container.add(button);
- }
- //
- class ButtonEventListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- String path = input.getText();
- File dir = new File(path);
- printFiles(dir);
- // переход в каталог по адресу...
- if (radio1.isSelected()) {
- String name = read("Введите адрес:");
- dir = getDir(dir, name);
- printFiles(dir);
- input.setText(path + name);
- }
- // создание нового каталога
- else if (radio2.isSelected()) {
- String name = read("Введите название нового каталога:");
- File dir4 = getDir(dir, name);
- dir4.mkdirs();
- dir = dir4;
- }
- // создание файла
- else if (radio3.isSelected()) {
- try {
- String name = read("Введите название текстового файла:");
- getDir(dir, name).createNewFile();
- } catch (IOException ex) {
- Logger.getLogger(Lab4.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- // удаление файла
- else if (radio4.isSelected()) {
- String name = read("Введите название файла, который хотите удалить:");;
- File dir5 = getDir(dir, name);
- boolean delete = dir5.delete();
- }
- // вывод свойств
- else if (radio5.isSelected()) {
- String name = read("Свойства. Введите каталог:");
- File dir7 = getDir(dir, name);
- println("Имя: " + dir7.getName()
- + "\nПуть: " + dir7.getPath()
- + "\nРазмер: " + getSize(dir7) + " байт"
- + "\nПоследние изменение: " + new Date(dir7.lastModified()).toString());
- }
- // открытие файла с помощью notepad.exe
- else if (radio6.isSelected()) {
- try {
- String name = read("Введите название файла вместе с его форматом:");
- String[] text2 = new String[2];
- text2[0] = "notepad.exe";
- text2[1] = getDir(dir, name).getAbsolutePath();
- Process process = Runtime.getRuntime().exec(text2, null);
- } catch (IOException ex) {
- Logger.getLogger(Lab4.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- }
- // точка входа программы
- public static void main(String[] args) {
- Lab4 app = new Lab4();
- app.setVisible(true);
- }
- // функция получения пути файла
- public static File getDir(File dir, String path) {
- File dirs = new File(path);
- return new File(dir.getPath() + File.separator + dirs.getName());
- }
- // функция вывода содержащихся файлов в каталоге
- public static void printFiles(File dir) {
- String message = "";
- for (File item : dir.listFiles()) {
- message = message + "\n" + item.getName();
- }
- println(message);
- }
- // функция получения размера файла
- public static long getSize(File file) {
- long size;
- if (file.isDirectory()) {
- size = 0;
- for (File child : file.listFiles()) {
- size += getSize(child);
- }
- } else {
- size = file.length();
- }
- return size;
- }
- // функция вывода диалогового окна
- public static void println(String str) {
- JOptionPane.showMessageDialog(null,
- str,
- "Output",
- JOptionPane.PLAIN_MESSAGE);
- }
- // функция считывания с диаологового окна
- public String read(String str) {
- return JOptionPane.showInputDialog(this, str);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement