Erstmal Hallo an alle! Das ist mein erster Beitrag bei euch.
So nun zu dem Programm an dem Ich seit 'ner Woche arbeite.
Das Programm heist DirectorySync und soll zwei Verzeichnisse synchronisieren. Man wählt ein Quellverzeichnis a und ein Zielverzeichnis b. Dann werden alle Ordner und Dateien von a nach b kopiert. Während des Kopiervorgangs wird jedes File von a mit jedem File im selben unterverzeichnis von b verglichen. Wenn ein File mit dem selben Namen vorhanden ist, wird zum nächsten gegangen. Ansonsten wird es kopiert.
Damit auch Unterordner eingeschlossen werden, verwende ich eine Rekursion. Das funktioniert soweit auch ganz gut. Jedoch sobald die Ordnerstruckturen etwas komplizierter sind, "vergisst" er einige Ordner und Dateien zu kopieren. Ich weis jetzt nicht so recht wo ich nach dem Fehler suchen soll. ???:L
Klasse CopyData:
Klasse Gui:
Ich hoffe, dass ihr mir etwas helfen könnt. Falls ihr Fragen zum Quellcode habt: fragt bitte.^^
So nun zu dem Programm an dem Ich seit 'ner Woche arbeite.
Das Programm heist DirectorySync und soll zwei Verzeichnisse synchronisieren. Man wählt ein Quellverzeichnis a und ein Zielverzeichnis b. Dann werden alle Ordner und Dateien von a nach b kopiert. Während des Kopiervorgangs wird jedes File von a mit jedem File im selben unterverzeichnis von b verglichen. Wenn ein File mit dem selben Namen vorhanden ist, wird zum nächsten gegangen. Ansonsten wird es kopiert.
Damit auch Unterordner eingeschlossen werden, verwende ich eine Rekursion. Das funktioniert soweit auch ganz gut. Jedoch sobald die Ordnerstruckturen etwas komplizierter sind, "vergisst" er einige Ordner und Dateien zu kopieren. Ich weis jetzt nicht so recht wo ich nach dem Fehler suchen soll. ???:L
Klasse CopyData:
Java:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import com.sun.jmx.snmp.tasks.Task;
/**
* @author Jens Uwe Becker
*
*/
public class CopyData {
private static File[] fileArrayIn,fileArrayOut;
private static int counter=0;
public static int c=0;
static Task task;
private static boolean progress=false;
private static boolean recursive=false;
/**
*
* @param fileIn file with the source path
* @param fileOut file with the target path
* @throws IOException
* @throws InterruptedException
*/
public static void startCopying(File fileIn, File fileOut) throws IOException, InterruptedException{
fileArrayIn = fileIn.listFiles();
fileArrayOut = fileOut.listFiles();
progress=true;
if(fileArrayIn!=null)
for(int i=counter;i<fileArrayIn.length;i++){
if(!recursive)c=i;
boolean fileFound=false;
File file = null;
counter=i;
if(fileArrayIn[i].isFile()) c+=(int) fileArrayIn[i].length();
//if(Main.debugMode)Main.gui.appendTaInfo("[debugInfo]: actual file space: "+c+"\n");
Main.gui.progressBar.setValue(c);
Main.gui.panelSouth.repaint();
Main.gui.repaint();
Thread.sleep(5);
/*
* recursive function call
*
*/
if(fileArrayIn[i].isDirectory()){
file = new File(fileOut.getAbsolutePath()+"\\"+fileArrayIn[i].getAbsoluteFile().getName());
recursive=true;
if(file.mkdir()){
Main.gui.appendTaInfo("[info]: create directory: "+file.getPath()+"\n");
if(Main.debugMode)Main.gui.appendTaInfo("[debugInfo]: recursive function call: new function instance opened.\n");
startCopying(fileArrayIn[i],file);
counter++;
startCopying(fileIn,fileOut);
}
else if(file.exists()){
if(Main.debugMode)Main.gui.appendTaInfo("[debugInfo]: recursive function call: new function instance opened.\n");
startCopying(fileArrayIn[i],file);
counter++;
startCopying(fileIn,fileOut);
}
else{
Main.gui.attribute = Main.gui.orange;
Main.gui.appendTaInfo("[warning]: could not create directory "+file.getPath()+"\n");
Main.gui.attribute = Main.gui.black;
}
recursive=false;
}
//continues here, if fileArrayIn[i] is no directory
if(fileArrayOut!=null)
for(int j=0;j<fileArrayOut.length;j++){
//Check if fileArrayIn[i] equals fileArrayOut[j]. If not: Copy fileIn to output folder
if(fileArrayOut[j].canRead())
if(fileArrayIn[i].getAbsoluteFile().getName().equals(fileArrayOut[j].getAbsoluteFile().getName())){
j=fileArrayOut.length;
fileFound=true;
}
}
//create and copy file, if not found
if(!fileFound){
if(fileArrayIn[i].canRead())
if(fileArrayIn[i].getAbsoluteFile().isFile()){
file = new File(fileOut.getAbsolutePath()+"\\"+fileArrayIn[i].getAbsoluteFile().getName());
Main.gui.appendTaInfo("[info]: file \""+fileArrayIn[i].getAbsoluteFile().getName()+"\" not found in target folder\n");
Main.gui.appendTaInfo("[info]: copy file \""+file.getAbsoluteFile().getName()+"\" \n");
if(fileArrayIn[i].exists()){
file.createNewFile();
copyFile(fileArrayIn[i],file);
}
else{
Main.gui.attribute=Main.gui.orange;
Main.gui.appendTaInfo("[warning]: file does not exist!\n");
Main.gui.attribute=Main.gui.black;
}
}
}
}
else Main.gui.appendTaInfo("[info]: No files found in selected directory");
if(counter<fileArrayIn.length){counter++; startCopying(fileIn,fileOut);}
}
/**
*
* @param dir
* @return the directory size
*/
public static long getDirSize(File dir) {
if (dir.isFile())
return dir.length();
File[] subFiles = dir.listFiles();
long size = 0;
for (File file : subFiles) {
if (file.isFile())
size += file.length();
else
size += getDirSize(file);
}
return size;
}
/**
* resets the Class CopyData, so that you can call them again for another folder/directory
*/
public static void reset(){
Main.gui.progressBar.setValue(0);
fileArrayIn=null;
fileArrayOut=null;
counter=0;
}
/**
*
* @param in source file to copy
* @param out target file to override
* @throws IOException
*/
public static void copyFile(File in, File out) throws IOException {
FileChannel inChannel = null;
FileChannel outChannel = null;
if(in.getAbsoluteFile().isFile()){
inChannel = new FileInputStream(in).getChannel();}
if(out.getAbsoluteFile().isFile()){
outChannel = new FileOutputStream(out).getChannel();}
try {
if(inChannel!=null && outChannel!=null)
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
Main.gui.attribute=Main.gui.red;
Main.gui.appendTaInfo("[IOException]: "+e.getMessage());
Main.gui.attribute=Main.gui.black;
e.printStackTrace();
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
}
Klasse Gui:
Java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import data.CopyData;
/**
*
* @author Jens Uwe Becker
*
*/
public class Gui extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JFileChooser fileChooser = new JFileChooser();
public JPanel panelSouth = new JPanel();
public JEditorPane taInfo = new JEditorPane();
private JButton buttonSource,buttonTarget,buttonClearTa;
public JButton buttonStart;
private File pathSource,pathTarget;
private JScrollPane scroll = new JScrollPane();
public AttributeSet attribute;
public MutableAttributeSet black;
public MutableAttributeSet red;
public MutableAttributeSet orange;
private Document doc;
public JTextField tf;
public JProgressBar progressBar;
public Thread thread;
public DefaultCaret caret;
public Gui(){
super("DirectorySync - v0.031 - Alpha");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(2,1));
setBounds(500,100,600,600);
setResizable(true);
black = new SimpleAttributeSet();
StyleConstants.setForeground(black, Color.black);
red = new SimpleAttributeSet();
StyleConstants.setForeground(red, Color.red);
orange = new SimpleAttributeSet();
StyleConstants.setForeground(orange, Color.orange);
taInfo.setEditable(false);
taInfo.setBackground(Color.WHITE);
taInfo.setEditorKit(new StyledEditorKit());
taInfo.setText("Welcome to DirectorySync!\n");
scroll = new JScrollPane(taInfo);
scroll.setAutoscrolls(rootPaneCheckingEnabled);
scroll.setSize(new Dimension(200,200));
scroll.setAutoscrolls(true);
caret = (DefaultCaret)taInfo.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
doc = taInfo.getDocument();
fileChooser.setAcceptAllFileFilterUsed(true);
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setBounds(0,0,200,200);
buttonSource = new JButton("Source folder");
buttonSource.setBounds(225,50,150,30);
buttonTarget = new JButton("Target folder");
buttonTarget.setBounds(225,100,150,30);
buttonStart = new JButton("Start");
buttonStart.setBounds(225,150,150,30);
buttonClearTa = new JButton("Clear log");
buttonClearTa.setBounds(225,200,150,30);
progressBar = new JProgressBar();
progressBar.setBounds(225,10,150,30);
tf = new JTextField();
tf.setEditable(false);
tf.setBounds(225,10,150,30);
panelSouth.setLayout(null);
//init panels
//panelSouth.add(tf);
panelSouth.add(progressBar);
panelSouth.add(buttonSource);
panelSouth.add(buttonTarget);
panelSouth.add(buttonStart);
panelSouth.add(buttonClearTa);
//add panels to layout
getContentPane().add(scroll);
getContentPane().add(panelSouth);
setVisible(true);
attribute=red;
appendTaInfo("[warning]: This program is an alpha version! Long runtimes expected after starting copying!\n");
attribute=black;
/*
* ActionListener comes here
*/
buttonSource.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
fileChooser.showOpenDialog(null);
setPathSource(fileChooser.getSelectedFile());
appendTaInfo("[info]: source folder "+getPathSource().getAbsolutePath()+ " selected\n");
}
});
buttonTarget.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
fileChooser.showOpenDialog(null);
setPathTarget(fileChooser.getSelectedFile());
appendTaInfo("[info]: target folder "+getPathTarget().getAbsolutePath()+ " selected.\n");
}
});
buttonStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
@SuppressWarnings("unused")
JProgressBar progress;
thread = new Thread(){
public void run(){
if(pathSource==null){
attribute=orange;
appendTaInfo("[warning]: please select source path.\n[warning]: copying aborded.\n");
attribute=black;
}
else if(pathTarget==null){
attribute=orange;
appendTaInfo("[warning]: please select target path.\n[warning]: copying aborded.\n");
attribute=black;
}
else{
try {
buttonStart.setEnabled(false);
buttonSource.setEnabled(false);
buttonTarget.setEnabled(false);
int dirSize=(int) CopyData.getDirSize(pathSource);
CopyData.c=dirSize;
appendTaInfo("[info]: copying started.\n");
appendTaInfo("[info]: dir size:"+dirSize+"\n");
progressBar.setMaximum(dirSize);
CopyData.startCopying(pathSource, pathTarget);
appendTaInfo("[info]: file copying finished!\n");
JOptionPane.showMessageDialog(null, "File copying successful finished!");
tf.setText(" Progress: 100%");
} catch (IOException e1) {
// TODO Auto-generated catch block
attribute=red;
appendTaInfo("[IOException]: "+e1.getMessage());
JOptionPane.showMessageDialog(null, e1.getMessage());
attribute=black;
e1.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
CopyData.reset();
buttonStart.setEnabled(true);
buttonSource.setEnabled(true);
buttonTarget.setEnabled(true);
}
};
thread.start();
}
});
buttonClearTa.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
taInfo.setText("");
doc=taInfo.getDocument();
appendTaInfo("[info]: log cleared.");
}
});
}
/**
* @return the pathSource
*/
public File getPathSource() {
return pathSource;
}
/**
* @param pathSource the pathSource to set
*/
public void setPathSource(File pathSource) {
this.pathSource = pathSource;
}
/**
* @return the pathTarget
*/
public File getPathTarget() {
return pathTarget;
}
/**
* @param pathTarget the pathTarget to set
*/
public void setPathTarget(File pathTarget) {
this.pathTarget = pathTarget;
}
/**
* @return the taInfo
*/
public JEditorPane getTaInfo() {
return taInfo;
}
/**
* @param taInfo the taInfo to set
*/
public void setTaInfo(JEditorPane taInfo) {
this.taInfo = taInfo;
}
/**
*
* @param arg0 the text to append
*/
public void appendTaInfo(String text){
try {
doc.insertString(doc.getLength(), text, attribute);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
}
Ich hoffe, dass ihr mir etwas helfen könnt. Falls ihr Fragen zum Quellcode habt: fragt bitte.^^