Guest User

Untitled

a guest
Dec 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 KB | None | 0 0
  1. [Maria;32] ; [Joao;44] e [Luna,12]`
  2.  
  3. package model;
  4.  
  5. public class Arquivo {
  6.  
  7. public static String Read(String Caminho){
  8. String conteudo = "";
  9. try {
  10. FileReader arq = new FileReader(Caminho);
  11. BufferedReader lerArq = new BufferedReader(arq);
  12. String linha="";
  13. try {
  14. linha = lerArq.readLine();
  15. while(linha!=null){
  16. linha = lerArq.readLine();
  17. conteudo += linha+"rn";
  18. }
  19. arq.close();
  20. return conteudo;
  21. } catch (IOException ex) {
  22. System.out.println("Erro: Não foi possível ler o arquivo!");
  23. return "";
  24. }
  25. } catch (FileNotFoundException ex) {
  26. System.out.println("Erro: Arquivo não encontrado!");
  27. return "";
  28. }}
  29.  
  30. public static boolean Write(String Caminho,String Texto){
  31. try {
  32. FileWriter arq = new FileWriter(Caminho,true);
  33. PrintWriter gravarArq = new PrintWriter(arq);
  34. gravarArq.println(Texto);
  35. gravarArq.close();
  36. return true;
  37. }catch(IOException e){
  38. System.out.println(e.getMessage());
  39. return false;
  40. }
  41. }}
  42.  
  43. package model;
  44.  
  45. public class ModelAluno {
  46.  
  47. String nome;
  48. String idade;
  49.  
  50. public String getNome() {
  51. return nome;
  52. }
  53. public void setNome(String nome) {
  54. this.nome = nome;
  55. }
  56. public String getIdade() {
  57. return idade;
  58. }
  59. public void setIdade(String idade) {
  60. this.idade = idade;
  61. }
  62.  
  63. public boolean persisitir(){
  64. return true;
  65. }
  66. }
  67.  
  68. package visao;
  69.  
  70. import java.awt.BorderLayout;
  71. import java.awt.EventQueue;
  72.  
  73. import javax.swing.JFrame;
  74. import javax.swing.JPanel;
  75. import javax.swing.border.EmptyBorder;
  76. import javax.swing.JLabel;
  77. import javax.swing.JTextField;
  78. import javax.swing.JButton;
  79. import javax.swing.JTextPane;
  80.  
  81. public class ViewNomeIdade extends JFrame {
  82.  
  83. public JPanel contentPane;
  84. public JTextField txtNome;
  85. public JTextField txtIdade;
  86. public JButton btSalvar;
  87. private JButton btnExibir;
  88.  
  89. public ViewNomeIdade() {
  90. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  91. setBounds(100, 100, 446, 139);
  92. contentPane = new JPanel();
  93. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  94. setContentPane(contentPane);
  95. contentPane.setLayout(null);
  96.  
  97. JLabel lblNome = new JLabel("Nome");
  98. lblNome.setBounds(10, 11, 46, 14);
  99. contentPane.add(lblNome);
  100.  
  101. JLabel lblIdade = new JLabel("Idade");
  102. lblIdade.setBounds(10, 50, 46, 14);
  103. contentPane.add(lblIdade);
  104.  
  105. txtNome = new JTextField();
  106. txtNome.setBounds(60, 8, 86, 20);
  107. contentPane.add(txtNome);
  108. txtNome.setColumns(10);
  109.  
  110. txtIdade = new JTextField();
  111. txtIdade.setBounds(60, 47, 86, 20);
  112. contentPane.add(txtIdade);
  113. txtIdade.setColumns(10);
  114.  
  115. btSalvar = new JButton("Salvar");
  116. btSalvar.setBounds(57, 77, 89, 23);
  117. contentPane.add(btSalvar);
  118.  
  119. btnExibir = new JButton("Exibir");
  120. btnExibir.setBounds(172, 77, 89, 23);
  121. contentPane.add(btnExibir);
  122. }
  123.  
  124. public JTextField getTxtNome() {
  125. return txtNome;
  126. }
  127.  
  128. public void setTxtNome(JTextField txtNome) {
  129. this.txtNome = txtNome;
  130. }
  131.  
  132. public JTextField getTxtIdade() {
  133. return txtIdade;
  134. }
  135.  
  136. public void setTxtIdade(JTextField txtIdade) {
  137. this.txtIdade = txtIdade;
  138. }
  139.  
  140. public JButton getBtSalvar() {
  141. return btSalvar;
  142. }
  143.  
  144. public void setBtSalvar(JButton btSalvar) {
  145. this.btSalvar = btSalvar;
  146. }
  147.  
  148. public JButton getBtnExibir() {
  149. return btnExibir;
  150. }
  151. }
  152.  
  153. package visao;
  154.  
  155. import java.awt.Dimension;
  156. import java.awt.Font;
  157.  
  158. import javax.swing.BorderFactory;
  159. import javax.swing.JFrame;
  160. import javax.swing.JPanel;
  161. import javax.swing.JScrollPane;
  162. import javax.swing.JTextArea;
  163. import javax.swing.border.EmptyBorder;
  164.  
  165. public class ViewExibir extends JFrame {
  166.  
  167. private JPanel contentPane;
  168. JTextArea textArea;
  169.  
  170. public ViewExibir() {
  171. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  172. setBounds(100, 100, 301, 203);
  173. contentPane = new JPanel();
  174. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  175. setContentPane(contentPane);
  176. contentPane.setLayout(null);
  177.  
  178. textArea = new JTextArea();
  179. textArea.setBounds(62, 11, 146, 127);
  180. contentPane.add(textArea);
  181. }
  182.  
  183. public JTextArea getTextArea() {
  184. return textArea;
  185. }
  186.  
  187. public void setTextArea(JTextArea textArea) {
  188. this.textArea = textArea;
  189. }
  190. }
  191.  
  192. package controle;
  193.  
  194. import java.awt.event.ActionEvent;
  195. import java.awt.event.ActionListener;
  196.  
  197. import model.Arquivo;
  198. import model.ModelAluno;
  199. import visao.ViewExibir;
  200. import visao.ViewNomeIdade;
  201.  
  202. public class ControleNomeIdade implements ActionListener{
  203.  
  204. ViewNomeIdade v ;
  205. ViewExibir vi ;
  206. ModelAluno m = new ModelAluno();
  207. String ArqConfig = "salvar.txt";
  208.  
  209. public ControleNomeIdade(ViewNomeIdade v, ViewExibir vi) {
  210. this.v = v;
  211. this.vi = vi;
  212. v.getBtSalvar().addActionListener(this);
  213. v.getBtnExibir().addActionListener(this);
  214. v.setVisible(true);
  215. }
  216.  
  217. @Override
  218. public void actionPerformed(ActionEvent e) {
  219. if(e.getSource() == v.getBtSalvar()){
  220. m.setNome(v.getTxtNome().getText());
  221. m.setIdade(v.getTxtIdade().getText());
  222. String print = m.getNome()+";"+m.getIdade();
  223.  
  224. if(Arquivo.Write(ArqConfig,print))
  225. System.out.println("Arquivo salvo com sucesso!");
  226. else
  227. System.out.println("Erro ao salvar o arquivo!");
  228. }
  229.  
  230. else if(e.getSource() == v.getBtnExibir()){
  231. String conteudo = Arquivo.Read(ArqConfig);
  232. String c1 = conteudo.split(";")[0];
  233. String c2 = conteudo.split(";")[1];
  234. System.out.println("Nome: " + c1 + "nIdade: " + c2);
  235. vi.getTextArea().append("Nome: " + c1 + "nIdade: " + c2);
  236. vi.setVisible(true);
  237. }
  238.  
  239. }
  240. }
  241.  
  242. package controle;
  243.  
  244. import visao.ViewExibir;
  245. import visao.ViewNomeIdade;
  246.  
  247. public class Principal {
  248.  
  249. public static void main(String[] args) {
  250. new ControleNomeIdade(new ViewNomeIdade(), new ViewExibir());
  251. }
  252. }
  253.  
  254. try {
  255. linha = lerArq.readLine();
  256. while(linha!=null){
  257. linha = lerArq.readLine();
  258. conteudo += linha+"rn";
  259. }
  260.  
  261. public static List<String> Read(String Caminho) {
  262. List<String> conteudo = new ArrayList<>();
  263.  
  264. try {
  265. FileReader arq = new FileReader(Caminho);
  266. BufferedReader lerArq = new BufferedReader(arq);
  267. String linha = "";
  268. try {
  269. linha = lerArq.readLine();
  270. while (linha != null) {
  271. conteudo.add(linha);
  272. linha = lerArq.readLine();
  273. }
  274. arq.close();
  275. return conteudo;
  276. } catch (IOException ex) {
  277. System.out.println("Erro: Não foi possível ler o arquivo!");
  278. }
  279. } catch (FileNotFoundException ex) {
  280. System.out.println("Erro: Arquivo não encontrado!");
  281. }
  282. return null;
  283. }
  284.  
  285. @Override
  286. public void actionPerformed(ActionEvent e) {
  287.  
  288. if (e.getSource() == v.getBtSalvar()) {
  289. m.setNome(v.getTxtNome().getText());
  290. m.setIdade(v.getTxtIdade().getText());
  291. String print = m.getNome() + ";" + m.getIdade();
  292.  
  293. if (Arquivo.Write(ArqConfig, print))
  294. System.out.println("Arquivo salvo com sucesso!");
  295. else
  296. System.out.println("Erro ao salvar o arquivo!");
  297. }
  298.  
  299. else if (e.getSource() == v.getBtnExibir()) {
  300. List<String> conteudo = Arquivo.Read(ArqConfig);
  301.  
  302. for(String linha : conteudo) {
  303. String c1 = linha.split(";")[0];
  304. String c2 = linha.split(";")[1];
  305. System.out.println("Nome: " + c1 + "nIdade: " + c2);
  306. vi.getTextArea().append("Nome: " + c1 + "nIdade: " + c2);
  307. vi.getTextArea().append("nn");
  308. }
  309. vi.setVisible(true);
  310. }
  311. }
  312.  
  313. import javax.swing.JFrame;
  314. import javax.swing.JPanel;
  315. import javax.swing.JScrollPane;
  316. import javax.swing.JTextArea;
  317. import javax.swing.border.EmptyBorder;
  318.  
  319. public class ViewExibir extends JFrame {
  320.  
  321. private JPanel contentPane;
  322. JTextArea textArea;
  323.  
  324. public ViewExibir() {
  325. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  326. setBounds(100, 100, 301, 203);
  327. contentPane = new JPanel();
  328. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  329. setContentPane(contentPane);
  330. contentPane.setLayout(null);
  331.  
  332. textArea = new JTextArea();
  333. JScrollPane scroll = new JScrollPane(textArea);
  334. scroll.setBounds(62, 11, 146, 127);
  335. contentPane.add(scroll);
  336. }
  337.  
  338. public JTextArea getTextArea() {
  339. return textArea;
  340. }
  341.  
  342. public void setTextArea(JTextArea textArea) {
  343. this.textArea = textArea;
  344. }
  345. }
Add Comment
Please, Sign In to add comment