Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JOptionPane;
- import javax.swing.filechooser.FileFilter;
- // @author rnxn
- public class Handle implements ActionListener {
- private File file;
- private final Window window;
- public Handle(final Window window) {
- this.window = window;
- for(JButton btn:window.getButtons())
- btn.addActionListener(this);
- window.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- switch(e.getActionCommand()){
- case "search":
- search();
- break;
- case "create":
- if(create())
- JOptionPane.showMessageDialog(window, "Arquivo gerado.", "", JOptionPane.INFORMATION_MESSAGE);
- break;
- }
- }
- private boolean search(){
- JFileChooser flchooser = new JFileChooser(System.getProperty("user.home"));
- flchooser.setDialogTitle("Buscar arquivo .txt");
- flchooser.setFileFilter(new FileFilter() {
- @Override
- public boolean accept(File f) {
- return f.getName().toLowerCase().endsWith(".txt");
- }
- @Override
- public String getDescription() {
- return "Arquivos .txt";
- }
- });
- flchooser.setAcceptAllFileFilterUsed(false);
- flchooser.setMultiSelectionEnabled(false);
- int response = flchooser.showOpenDialog(window);
- if(response == JFileChooser.APPROVE_OPTION){
- file = flchooser.getSelectedFile();
- if(file != null){
- window.setButtonState(true);
- } else {
- window.setButtonState(false);
- }
- }
- return false;
- }
- private boolean create(){
- String path = file.getParent();
- try {
- // System.out.println(path);
- File nwFile = new File(path+"\\tags.txt");
- nwFile.createNewFile();
- try (BufferedWriter bw = new BufferedWriter(new FileWriter(nwFile))) {
- bw.write(parse(file));
- }
- return true;
- } catch(Exception e){
- //System.out.println(e.getMessage());
- return false;
- }
- }
- private String parse(File slFile) throws FileNotFoundException, IOException{
- StringBuilder sb = new StringBuilder();
- try (BufferedReader br = new BufferedReader(new FileReader(slFile))) {
- while(br.ready()){
- String line = br.readLine();
- String words[] = line.split("[\\W]"); /*regex*/
- if(words.length > 0){
- for(String str:words)
- if(str.length() > 0)
- sb.append(Enum.IN.get()).append(str).append(Enum.OUT.get());
- }
- }
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement