JastRedd

Untitled

Aug 12th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.*;
  5.  
  6. class FileStreams
  7. {
  8. public static void main(String args[])
  9. {
  10. FrameWindow frame;
  11. frame = new FrameWindow("Simplest Java Notepad");
  12. frame.setVisible(true);
  13. }
  14. }
  15.  
  16. // ================================
  17. class FrameWindow extends Frame implements ActionListener, WindowListener
  18. {
  19. TextArea ta;
  20. MenuBar mb;
  21. Menu mFile;
  22.  
  23.  
  24. MenuItem miOpen;
  25. MenuItem miSave;
  26. MenuItem miSaveAs;
  27. MenuItem miExit;
  28.  
  29. String PolniiPut = "";
  30. byte buf[];
  31.  
  32.  
  33. public FrameWindow(String szTitle)
  34. {
  35. super(szTitle);
  36. setSize(800, 600);
  37.  
  38. mb = new MenuBar();
  39. mFile = new Menu("File");
  40.  
  41. miOpen = new MenuItem("Open...");
  42. mFile.add(miOpen);
  43.  
  44. miSave = new MenuItem("Save");
  45. mFile.add(miSave);
  46.  
  47. miSaveAs = new MenuItem("Save As...");
  48. mFile.add(miSaveAs);
  49.  
  50. mFile.add("-");
  51.  
  52. miExit = new MenuItem("Exit");
  53. mFile.add(miExit);
  54.  
  55. mb.add(mFile);
  56.  
  57.  
  58. miOpen.addActionListener(this);
  59. miSave.addActionListener(this);
  60. miSaveAs.addActionListener(this);
  61. miExit.addActionListener(this);
  62.  
  63. setMenuBar(mb);
  64. this.addWindowListener(this);
  65.  
  66. ta = new TextArea(10, 30);
  67. setLayout(new BorderLayout());
  68. add("Center", ta);
  69. }
  70.  
  71. // ============================================
  72. // actionPerformed
  73. // ============================================
  74. public void actionPerformed(ActionEvent e)
  75. {
  76. if(e.getSource().equals(miOpen))
  77. {
  78. FileOpen();
  79. }
  80.  
  81. else if(e.getSource().equals(miSave))
  82. {
  83. FileSave();
  84. }
  85.  
  86. else if(e.getSource().equals(miSaveAs))
  87. {
  88. FileSaveAs();
  89. }
  90.  
  91. else if(e.getSource().equals(miExit))
  92. {
  93. setVisible(false);
  94. System.exit(0);
  95. }
  96. }
  97.  
  98.  
  99. public void windowClosing(WindowEvent e)
  100. {
  101. setVisible(false);
  102. System.exit(0);
  103. }
  104.  
  105. public void windowOpened(WindowEvent e) {}
  106. public void windowClosed(WindowEvent e) {}
  107. public void windowIconified(WindowEvent e) {}
  108. public void windowDeiconified(WindowEvent e) {}
  109. public void windowActivated(WindowEvent e) {}
  110. public void windowDeactivated(WindowEvent e) {}
  111.  
  112. // ============================================
  113. void FileOpen()
  114. {
  115. FileDialog FD;
  116. FD = new FileDialog(this, "Open file",FileDialog.LOAD);
  117. FD.show();
  118.  
  119. PolniiPut = FD.getDirectory()+FD.getFile();
  120.  
  121. setTitle("Simplest Java Notepad" + " - " +PolniiPut);
  122.  
  123.  
  124. FileInputStream FIS = null;
  125.  
  126. try
  127. {
  128. FIS = new FileInputStream(PolniiPut);
  129.  
  130. buf = new byte[FIS.available()];
  131. FIS.read(buf);
  132. }
  133. catch (IOException ex)
  134. {
  135. System.out.println(ex.toString());
  136. }
  137.  
  138. ta.selectAll();
  139.  
  140. ta.replaceRange("", 0, ta.getSelectionEnd());
  141.  
  142. String szStr = new String(buf);
  143.  
  144. StringTokenizer st;
  145. st = new StringTokenizer(szStr, "\r\n");//выделяем перевод строки...
  146.  
  147. while(st.hasMoreElements())
  148. {
  149. szStr = new String((String)st.nextElement());
  150. ta.append(szStr + "\r\n");
  151. }
  152.  
  153.  
  154. try
  155. {
  156. FIS.close();
  157. }
  158. catch (IOException ex)
  159. {
  160. System.out.println(ex.toString());
  161. }
  162. }
  163.  
  164. // ==============================
  165. void FileSaveAs()
  166. {
  167. FileDialog FD;
  168. FD = new FileDialog(this, "Save file as...", FileDialog.SAVE);
  169. FD.show();
  170.  
  171. if(FD.getDirectory() == null || FD.getFile() == null)
  172. {
  173. System.out.println("No File Selected!!!");
  174. }
  175. else
  176. {
  177. PolniiPut = FD.getDirectory()+FD.getFile();
  178. setTitle("Simplest Java Notepad" + " - " +PolniiPut);
  179. FileSave();
  180. }
  181. }
  182.  
  183. // ========================================
  184. void FileSave()
  185. {
  186. FileOutputStream OS = null;
  187.  
  188. String sz = ta.getText();
  189. buf = sz.getBytes();
  190.  
  191. if(PolniiPut=="" || PolniiPut==null)
  192. {
  193. System.out.println("No File Selected!!!");
  194. }
  195. else
  196. {
  197. try
  198. {
  199. OS = new FileOutputStream(PolniiPut);
  200. OS.write(buf);
  201. OS.close();
  202. }
  203. catch (IOException ex)
  204. {
  205. System.out.println(ex.toString());
  206. }
  207. }
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment