다음 코드를 살펴보고 내가하고있는 일이 분명히 잘못되었거나 이상하다는 것을 알려주고 싶습니다. 나는 단지 일반적인 코딩 조언을 찾고있다. 당신이 옳은 방향으로 나를 가리킬 수 있다면 그것은 위대 할 것입니다.Java 프로그래밍 지침
나는 자바가 처음이다. 나는 다음과 같은 수십 번을 쓰고 다시 썼다. 나는 누군가가 분명히 잘못한 것이 있는지 알아보기를 원합니다.
try {
} catch (Exception e) {
e.printStackTrace();
}
하드 코딩 된 경로와 같은
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.*;
import javax.swing.JTextArea;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JLabel;
public class FileWatcher implements ActionListener {
private JTextArea textArea = new JTextArea();
private JScrollPane pane = new JScrollPane(textArea);
private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private JFrame frame = new JFrame();
private JButton startButton = new JButton("Clear");
private JPanel panel = new JPanel();
public FileWatcher(String path) {
Font font = new Font("Arial Unicode MS", Font.PLAIN, 13);
this.textArea.setEditable(false);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.frame.add(pane);
this.frame.setSize(500, 200);
this.frame.setLocation(screenSize.width - 500, screenSize.height - 250);
this.frame.setTitle("Monitoring: " + path + "...");
this.textArea.setFont(font);
this.frame.add(panel, BorderLayout.SOUTH);
startButton.setPreferredSize(new Dimension(450, 20));
this.panel.add(startButton);
this.startButton.addActionListener(this);
this.frame.setVisible(true);
}
private void sample(String path) throws Exception {
int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED
| JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
boolean watchSubtree = true;
JNotify.addWatch(path, mask, watchSubtree, new Listener());
}
public void actionPerformed(ActionEvent e) {
this.textArea.setText("");
}
public void TouchFile(String fName) {
if ((fName.indexOf("Thumbs") == -1) && (fName.indexOf(".rar") == -1)
&& (fName.indexOf(".part") == -1)) {
String TodayDate = "";
String fModifyDate = "";
String line;
String line2 = "error";
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
File filename = new File(fName);
try {
} catch (Exception e) {
e.printStackTrace();
}
TodayDate = dateFormat.format(calendar.getTime());
fModifyDate = dateFormat.format(filename.lastModified());
try {
Thread.currentThread().sleep(1000);
while ((line2.indexOf("error") != -1)
&& (!fModifyDate.equals(TodayDate))) {
line2 = " ";
line = " ";
// Process p =
// Runtime.getRuntime().exec("C:\\Users\\BLUE\\Documents\\MYDROP~1\\Utils\\WatchFolder\\touch.exe "
// + "\"" + fName + "\"");
Process p = Runtime.getRuntime().exec("touch.exe " + "\"" + fName + "\"");
BufferedReader input = new BufferedReader(
new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
line2 = line2 + line;
showInfo(line);
}
input.close();
p.waitFor();
}
} catch (Exception err) {
err.printStackTrace();
}
}
}
String chopData(String data) {
File file = new File(data);
String fileName= file.getName();
if (data.indexOf("Touched") != -1) {
int tIndex = data.indexOf("Touched");
data = data.substring(0,tIndex+8) + "..." + fileName;
}
if ((data.indexOf("Touched") == -1) && (data.length() > 70)) {
data = data.substring(0, data.indexOf(" ")) + " ... "
+ data.substring(data.length() - 55);
}
return data;
}
public void showInfo(String data) {
data = chopData(data);
this.textArea.append(data + "\n");
this.frame.getContentPane().validate();
this.textArea.setCaretPosition(this.textArea.getText().length());
}
class Listener implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
showInfo("renamed ..." + oldName.substring(oldName.length() - 25)
+ " -> ..." + newName.substring(newName.length() - 25));
}
public void fileModified(int wd, String rootPath, String name) {
TouchFile(rootPath + "\\" + name);
}
public void fileDeleted(int wd, String rootPath, String name) {
showInfo("deleted " + name);
}
public void fileCreated(int wd, String rootPath, String name) {
TouchFile(rootPath + "\\" + name);
showInfo("created " + name);
}
void print(String msg) {
showInfo(msg);
}
}
public static void main(String[] args) {
try {
if (args.length > 0) {
FileWatcher[] fwArray = new FileWatcher[args.length];
for (int i = 0; i < args.length; i++) {
fwArray[i] = new FileWatcher(args[i]);
fwArray[i].sample(args[i]);
}
} else {
FileWatcher watcher = new FileWatcher("H:\\download");
watcher.sample("H:\\download");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
감사합니다. 그 사람들은 좀 훑어 보았지만 지금은 그것을 가지고 있습니다. 당신의 도움을 주셔서 감사합니다. – zincc