I developed an application on Windows using Java, Swing (window builder).
On a button click, my application will go to another class (FileManager.java
file) to count the total number of files in the input folder (meanwhile progressBar
will be in indeterminate mode). Once the number of files are known, progressBar
maximum value is set.
Then I call convertToXLS(fileMgr)
to read through the content of each file (1 kb) and update the progressBar
as each file is read.
Here is the code for it:
public class xmlToXL {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
xmlToXL window = new xmlToXL();
window.frame.setVisible(true);
}
});
private void initialize() {
...... some UI code ........
btnConvertXmlTo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
preConvertToXLS();
Task task = new Task(folderPath.getText());
task.execute();
} catch (Exception e1) {
e1.printStackTrace();
}
}// end of actionPerformed method
}); // end of action listened
}//end of initialize
public void preConvertToXLS() { //method to set few UI properties
btnConvertXmlTo.setEnabled(false);
progressBar.setVisible(true);
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
progressBar.setString("Calculating Total number of files...");
progressBar.setForeground(new Color(0, 102, 0));
}
ParserUtils parUtils = new ParserUtils(); //class to parse XML files (in another .java file)
private void convertToXLS(FileManager fileMgr) {
try {
int i=1;
parUtils.reset();
progressBar.setValue(0);
List<File> files = fileMgr.getFiles();
for(File file : files) {
progressBar.setString("Reading " + i+ " of " + fileMgr.getSize()+ " files");
parUtils.parseFileUsingDOM(file); // This will read content of the input file
progressBar.setValue(i++);
}
btnConvertXmlTo.setEnabled(true);
} catch (Exception e) {
}
}
class Task extends SwingWorker<Void, Void> {
private FileManager fileMgr;
public Task(String srcPath) {
this.fileMgr = new FileManager(new File(srcPath));
}
/*
* Main task. Executed in background thread.
*/
@Override
public Void doInBackground() {
try {
progressBar.setIndeterminate(true);
fileMgr.readFiles();
progressBar.setIndeterminate(false);
progressBar.setMaximum(fileMgr.getSize());
convertToXLS(fileMgr);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* Executed in event dispatching thread
*/
@Override
public void done() {
Toolkit.getDefaultToolkit().beep();
try {
progressBar.setString("FileRead Successful");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}//end of task class
}//end of My class
My UI becomes unresponsive after fileMgr.readFiles();
. It takes a minute or two, sometimes three and then executes convertToXLS(fileMgr)
.
FileManager.java
import XMLParsing.DetermineEncoding;
public class FileManager {
public HashMap<String, ArrayList<String>> dirFiles = null;
public ArrayList<String> dirNames = null;
public int numberOfFiles;
private File src;
private List<File> files;
public FileManager(File src) {
this.src = src;
dirNames = new ArrayList<String>();
dirFiles = new HashMap<String, ArrayList<String>>();
numberOfFiles = 0;
files = new ArrayList<File>();
}
public int getSize() {
return numberOfFiles;
}
public ArrayList<String> getDirectories(){
return dirNames;
}
public List<File> getFiles() {
Iterator it = dirFiles.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
String folderName = (pair.getKey()).toString();
ArrayList<String> FileNames = (ArrayList<String>) pair.getValue();
if (FileNames != null) {
for (String fileName : FileNames) {
if(replaceSelected(fileName)) {
File fXmlFile = new File(fileName);
files.add(fXmlFile);
}
else {
}
}
}
}
return files;
}
public void readFiles() throws IOException {
readFiles(src);
}
private void readFiles(File folder) throws IOException {
if (folder.isDirectory()) {
ArrayList<String> fileNames = new ArrayList<String>();
for (final File file : folder.listFiles()) {
if (file.isDirectory()) {
readFiles(file);
} else {
String fileName = (file.getPath()).toString();
if(fileName.toLowerCase().endsWith(".xml")) {
fileNames.add(file.getPath());
numberOfFiles = numberOfFiles + 1;
System.out.println(".");
if(!dirNames.contains(file.getParentFile().getName()))
dirNames.add(file.getParentFile().getName());
}
}
}
dirFiles.put(folder.getName(), fileNames);
}
}
private boolean replaceSelected(String filePath) {
String line;
String input = "";
try {
DetermineEncoding DE = new DetermineEncoding();
String encoding = DE.getFileEncoding(filePath);
InputStreamReader file = new InputStreamReader(new FileInputStream(
filePath), encoding);
BufferedReader br = new BufferedReader(file);
while ((line = br.readLine()) != null) {
input += line.toString() + " ";
}
file.close();
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(filePath), "UTF-8"));
out.append(input.trim());
out.flush();
out.close();
} catch (Exception e) {
return false;
}
return true;
}
}
DetermineEncoding.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.mozilla.universalchardet.UniversalDetector;
public class DetermineEncoding {
public DetermineEncoding() {
// TODO Auto-generated constructor stub
}
public String getFileEncoding(String fileName) throws IOException {
byte[] buf = new byte[4096];
java.io.FileInputStream fis = new FileInputStream(fileName);
UniversalDetector detector = new UniversalDetector(null);
int nread;
while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
detector.handleData(buf, 0, nread);
}
detector.dataEnd();
String encoding = detector.getDetectedCharset();
if (encoding != null) {
return encoding;
} else {
return "";
}
}
}
Please help me identify the issue.