Advertisement
Guest User

Untitled

a guest
May 26th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. package lab4;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import java.util.Date;
  11.  
  12. public class Lab4 extends JFrame {
  13. private JButton button = new JButton("Выполнить");
  14. private JTextField input = new JTextField("", 5);
  15. private JLabel label = new JLabel("Введите путь к интересующему файлу:");
  16. private JRadioButton radio1 = new JRadioButton("Войти в каталог");
  17. private JRadioButton radio2 = new JRadioButton("Создать каталог");
  18. private JRadioButton radio3 = new JRadioButton("Создать файл");
  19. private JRadioButton radio4 = new JRadioButton("Удалить");
  20. private JRadioButton radio5 = new JRadioButton("Вывод свойств");
  21. private JRadioButton radio6 = new JRadioButton("Открыть файл");
  22.  
  23. public Lab4() {
  24. // задаем название окна
  25. this.setTitle("Файловый менеджер");
  26.  
  27. // задаем координаты и размеры окна
  28. this.setBounds(100, 100, 400, 200);
  29.  
  30. // обработка закрытия окна
  31. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.  
  33. // создаем контейнер (container)
  34. Container container = this.getContentPane();
  35.  
  36. // делим контейнер сеткой 3, 3, 3, 3
  37. container.setLayout(new GridLayout(3, 3, 3, 3));
  38.  
  39. // задаем начальное значение radio1 (Войти в каталог) на true
  40. radio1.setSelected(true);
  41.  
  42. // необходимо для обработки нажатия кнопки (Выполнить)
  43. button.addActionListener(new ButtonEventListener());
  44.  
  45. // добавляем элементы в контейнер (container)
  46. container.add(label);
  47. container.add(input);
  48. container.add(radio1);
  49. container.add(radio2);
  50. container.add(radio3);
  51. container.add(radio4);
  52. container.add(radio5);
  53. container.add(radio6);
  54. container.add(button);
  55. }
  56.  
  57. //
  58. class ButtonEventListener implements ActionListener {
  59. public void actionPerformed(ActionEvent e) {
  60. String path = input.getText();
  61. File dir = new File(path);
  62. printFiles(dir);
  63. // переход в каталог по адресу...
  64. if (radio1.isSelected()) {
  65. String name = read("Введите адрес:");
  66. dir = getDir(dir, name);
  67. printFiles(dir);
  68. input.setText(path + name);
  69. }
  70. // создание нового каталога
  71. else if (radio2.isSelected()) {
  72. String name = read("Введите название нового каталога:");
  73. File dir4 = getDir(dir, name);
  74. dir4.mkdirs();
  75. dir = dir4;
  76. }
  77. // создание файла
  78. else if (radio3.isSelected()) {
  79. try {
  80. String name = read("Введите название текстового файла:");
  81. getDir(dir, name).createNewFile();
  82. } catch (IOException ex) {
  83. Logger.getLogger(Lab4.class.getName()).log(Level.SEVERE, null, ex);
  84. }
  85. }
  86. // удаление файла
  87. else if (radio4.isSelected()) {
  88. String name = read("Введите название файла, который хотите удалить:");;
  89. File dir5 = getDir(dir, name);
  90. boolean delete = dir5.delete();
  91. }
  92. // вывод свойств
  93. else if (radio5.isSelected()) {
  94. String name = read("Свойства. Введите каталог:");
  95. File dir7 = getDir(dir, name);
  96.  
  97. println("Имя: " + dir7.getName()
  98. + "\nПуть: " + dir7.getPath()
  99. + "\nРазмер: " + getSize(dir7) + " байт"
  100. + "\nПоследние изменение: " + new Date(dir7.lastModified()).toString());
  101. }
  102. // открытие файла с помощью notepad.exe
  103. else if (radio6.isSelected()) {
  104. try {
  105. String name = read("Введите название файла вместе с его форматом:");
  106. String[] text2 = new String[2];
  107. text2[0] = "notepad.exe";
  108. text2[1] = getDir(dir, name).getAbsolutePath();
  109. Process process = Runtime.getRuntime().exec(text2, null);
  110. } catch (IOException ex) {
  111. Logger.getLogger(Lab4.class.getName()).log(Level.SEVERE, null, ex);
  112. }
  113. }
  114. }
  115. }
  116.  
  117. // точка входа программы
  118. public static void main(String[] args) {
  119. Lab4 app = new Lab4();
  120. app.setVisible(true);
  121. }
  122.  
  123. // функция получения пути файла
  124. public static File getDir(File dir, String path) {
  125. File dirs = new File(path);
  126. return new File(dir.getPath() + File.separator + dirs.getName());
  127. }
  128.  
  129. // функция вывода содержащихся файлов в каталоге
  130. public static void printFiles(File dir) {
  131. String message = "";
  132. for (File item : dir.listFiles()) {
  133. message = message + "\n" + item.getName();
  134. }
  135. println(message);
  136. }
  137.  
  138. // функция получения размера файла
  139. public static long getSize(File file) {
  140. long size;
  141. if (file.isDirectory()) {
  142. size = 0;
  143. for (File child : file.listFiles()) {
  144. size += getSize(child);
  145. }
  146. } else {
  147. size = file.length();
  148. }
  149. return size;
  150. }
  151.  
  152. // функция вывода диалогового окна
  153. public static void println(String str) {
  154. JOptionPane.showMessageDialog(null,
  155. str,
  156. "Output",
  157. JOptionPane.PLAIN_MESSAGE);
  158. }
  159.  
  160. // функция считывания с диаологового окна
  161. public String read(String str) {
  162. return JOptionPane.showInputDialog(this, str);
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement