我无法让 Java Swing 进度监视器工作。下面的脚本转换所选目录中的所有文件。循环期间从不显示进度条窗口。可能我必须为 ProgressMonitorInputStream 提供更多提示,例如最大值和最小值。我无法单独解决这个问题,并将感谢任何帮助。
public class csvtoxls {
public static void main() throws IOException {
//here you enter the path to your directory.
//for example: Path workDir = Paths.get("C:\\Users\\Kamil\Desktop\\csvtoxlspython\\Nowy folder (2)")
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setDialogTitle("Wybierz folder do konwersji: ");
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.setAcceptAllFileFilterUsed(false);
int returnValue = jfc.showSaveDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
if (jfc.getSelectedFile().isDirectory()) {
System.out.println("You selected the directory: " + jfc.getSelectedFile());
String z;
//@SuppressWarnings("deprecation")
//Path workDir = jfc.getCurrentDirectory().toPath();
//Path workDir = FileSystems.getDefault().getPath(jfc.getCurrentDirectory());
Path workDir = Paths.get(gui.pickPath(jfc));
System.out.println(workDir);
//the if checks whether the directory truly exists
if (!Files.notExists(workDir )) {;
//this part stores all files withn the directory in a list
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(workDir )) {
for (Path path : directoryStream) {
@SuppressWarnings("deprecation")
ArrayList<ArrayList<String>> allRowAndColData = null;
ArrayList<String> oneRowData = null;
Path fName = workDir;
String currentLine;
FileInputStream fis = new FileInputStream(path.toString());
DataInputStream myInput = new DataInputStream(fis);
ProgressMonitorInputStream pm = new ProgressMonitorInputStream(null ,"Reading the big file", fis);
int c;
while((c=pm.read()) != -1) {
int i = 0;
allRowAndColData = new ArrayList<ArrayList<String>>();
while ((currentLine = myInput.readLine()) != null) {
oneRowData = new ArrayList<String>();
String oneRowArray[] = currentLine.split(";");
for (int j = 0; j < oneRowArray.length; j++) {
oneRowData.add(oneRowArray[j]);
}
allRowAndColData.add(oneRowData);
System.out.println();
i++;
}
try {
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet sheet = workBook.createSheet("sheet1");
for (int i1 = 0; i1 < allRowAndColData.size(); i1++) {
ArrayList<?> ardata = (ArrayList<?>) allRowAndColData.get(i1);
HSSFRow row = sheet.createRow((short) 0 + i1);
for (int k = 0; k < ardata.size(); k++) {
System.out.print(ardata.get(k));
HSSFCell cell = row.createCell((short) k);
cell.setCellValue(ardata.get(k).toString());
}
System.out.println();
}
// Write the output to a file
z = path.toString();
z = z = z.substring(0, z.lastIndexOf('.'));
FileOutputStream fileOutputStream = new FileOutputStream(z + ".xls");
workBook.write(fileOutputStream);
fileOutputStream.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
finally {
// ... cleanup that will execute whether or not an error occurred ...
}
}
}
}
}
}
}
}
}