Skip to content

Instantly share code, notes, and snippets.

@Nasawa
Created February 4, 2016 21:07
Show Gist options
  • Save Nasawa/7c061b5f915aea3aefa5 to your computer and use it in GitHub Desktop.
Save Nasawa/7c061b5f915aea3aefa5 to your computer and use it in GitHub Desktop.
Backs up files from one folder to another
package backup;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import org.apache.commons.io.FileUtils;
@SuppressWarnings("serial")
public class BackupApplication extends JPanel implements ActionListener, PropertyChangeListener
{
JFileChooser chooser;
JButton src, dest, start;
JPanel srcHost, destHost, startHost, headHost;
JTextField srcText, destText, destArea, nameText, delayText;
JLabel statusText, nameLabel, delayLabel;
Thread t;
File data;
public BackupApplication()
{
Dimension d = new Dimension(120, 20);
src = new JButton("Source");
dest = new JButton("Destination");
start = new JButton("Start");
src.setPreferredSize(d);
dest.setPreferredSize(d);
start.setPreferredSize(d);
src.addActionListener(this);
dest.addActionListener(this);
start.addActionListener(this);
srcText = new JTextField(25);
destText = new JTextField(25);
nameText = new JTextField(10);
delayText = new JTextField(2);
statusText = new JLabel("Copying currently not active.");
nameLabel = new JLabel("Backup Name:");
delayLabel = new JLabel("Rate (in mins):");
d = new Dimension(320, 20);
srcText.setMaximumSize(d);
destText.setMaximumSize(d);
statusText.setMaximumSize(d);
statusText.addPropertyChangeListener(this);
headHost = new JPanel();
headHost.setLayout(new BoxLayout(headHost, BoxLayout.X_AXIS));
srcHost = new JPanel();
srcHost.setLayout(new BoxLayout(srcHost, BoxLayout.X_AXIS));
destHost = new JPanel();
destHost.setLayout(new BoxLayout(destHost, BoxLayout.X_AXIS));
startHost = new JPanel();
startHost.setLayout(new BoxLayout(startHost, BoxLayout.X_AXIS));
d = new Dimension(200, 30);
srcHost.setPreferredSize(d);
destHost.setPreferredSize(d);
startHost.setPreferredSize(d);
headHost.add(nameLabel);
headHost.add(nameText);
headHost.add(delayLabel);
headHost.add(delayText);
srcHost.add(srcText);
srcHost.add(src);
destHost.add(destText);
destHost.add(dest);
startHost.add(statusText);
startHost.add(start);
add(headHost);
add(srcHost);
add(destHost);
add(startHost);
}
public BackupApplication(JLabel label)
{
statusText = label;
}
public static void main(String[] args) throws IOException
{
JFrame frame = new JFrame("Backup Application");
BackupApplication app = new BackupApplication();
app.setLayout(new BoxLayout(app, BoxLayout.Y_AXIS));
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
int q = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?");
if (q == JOptionPane.OK_OPTION)
System.exit(0);
}
});
frame.getContentPane().add(app, "Center");
frame.setSize(app.getPreferredSize());
frame.setVisible(true);
app.settings(true);
}
public void settings(boolean isRead) throws IOException
{
data = new File("./backup.txt");
data.createNewFile();
JTextField[] fields = {nameText, delayText, srcText, destText};
List<String> lines;
if(isRead)
{
lines = FileUtils.readLines(data);
for(int i = 0; i < fields.length; i++)
fields[i].setText(lines.get(i));
}
else
{
lines = new ArrayList<String>();
for(int i = 0; i < fields.length; i++)
lines.add(fields[i].getText());
FileUtils.writeLines(data, lines);
}
}
@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == start)
{
if (t != null)
{
t.stop();
t = null;
start.setText("Start");
return;
}
if (srcText.getText() != "" && destText.getText() != "" && srcText.getText() != destText.getText())
{
try
{
settings(false);
}
catch (IOException e1)
{
e1.printStackTrace();
}
t = new Thread(new CopyWorker(nameText.getText(), Integer.parseInt(delayText.getText()) * 60000,
srcText.getText(), destText.getText(), statusText));
t.start();
statusText.setText("Copying initialized...");
start.setText("Stop");
}
else
{
JOptionPane.showMessageDialog(null, "Your paths are empty or invalid!");
}
return;
}
if (t != null)
{
t.stop();
t = null;
start.setText("Start");
}
chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
destArea = e.getSource() == src ? srcText : destText;
chooser.setCurrentDirectory(new File(destArea.getText() == "" ? "." : destArea.getText()));
chooser.setDialogTitle(String.format("Choose %s Location", destArea == srcText ? "Source" : "Destination"));
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
destArea.setText(chooser.getSelectedFile().toString());
}
public Dimension getPreferredSize()
{
return new Dimension(400, 150);
}
@SuppressWarnings("deprecation")
@Override
public void propertyChange(PropertyChangeEvent evt)
{
if (evt.getSource() == statusText)
{
if (statusText.getText().equals("Error"))
{
start.setText("Start");
if (t != null)
{
t.stop();
t = null;
}
}
}
}
}
package backup;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.apache.commons.io.FileUtils;
public class CopyWorker implements Runnable
{
String src, dest, name;
int delay = 30 * 60 * 1000;
JLabel status;
public CopyWorker(String n, int t, String s, String d, JLabel l)
{
src = s;
dest = d;
status = l;
name = n;
delay = t;
}
@Override
public void run()
{
while(true)
{
String time = System.currentTimeMillis() + "";
status.setText("Copying...");
File out = new File(String.format("%s/%s-%s", dest, name, time));
out.mkdir();
try
{
FileUtils.copyDirectory(new File(src), out);
status.setText(String.format("Last Copy @ %s", LocalDateTime.now().toString()));
Thread.sleep(delay);
}
catch (InterruptedException | IOException e)
{
JOptionPane.showMessageDialog(null, "Something broke!");
status.setText("Error");
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment