Java 在我进行时追加数据,而不是一次追加
我正在编写一个swing应用程序。这里的任务是从给定的URL下载文件,然后获取它们的计数。程序运行时没有问题/错误 问题是我的框架中有一个测试区域,当下载Java 在我进行时追加数据,而不是一次追加,java,swing,Java,Swing,我正在编写一个swing应用程序。这里的任务是从给定的URL下载文件,然后获取它们的计数。程序运行时没有问题/错误 问题是我的框架中有一个测试区域,当下载文件1时,我希望文本区域显示下载的文件1,当文件2完成下载的文件1时,依此类推 目前,在我的程序中,信息会同时显示。我的意思是如果我有10个文件,就会显示出来 downloaded file 1 downloaded file 2 . . . . . downloaded file 10 但只有在下载完所有10个文件后才会显示。为了查看是否还
文件1
时,我希望文本区域显示下载的文件1
,当文件2完成下载的文件1
时,依此类推
目前,在我的程序中,信息会同时显示。我的意思是如果我有10个文件,就会显示出来
downloaded file 1
downloaded file 2
.
.
.
.
.
downloaded file 10
但只有在下载完所有10个文件后才会显示。为了查看是否还有其他问题,我在textarea
代码下方添加了sysout
。令我惊讶的是,sysout
s被打印了10次,我的意思是每次处理一个文件时都会触发,但是textarea
一次会附加所有数据
下面是我的代码
guiforPDF
import java.awt.BorderLayout;
public class GuiForPDFs extends JFrame {
private JPanel contentPane;
private JTextField srcTextField;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiForPDFs frame = new GuiForPDFs();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
FileDownloadTest downloadTest = new FileDownloadTest();
public GuiForPDFs() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 552, 358);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
srcTextField = new JTextField();
srcTextField.setBounds(10, 26, 399, 20);
contentPane.add(srcTextField);
srcTextField.setColumns(10);
JButton srcBtn = new JButton("Source Excel");
srcBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fChoose = new JFileChooser();
if (fChoose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
srcTextField.setText(fChoose.getSelectedFile().getAbsolutePath());
} else {
System.out.println("No Selection");
}
}
});
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBounds(10, 106, 516, 203);
contentPane.add(textArea);
srcBtn.setBounds(419, 25, 107, 23);
contentPane.add(srcBtn);
JButton dNcButton = new JButton("Process");
dNcButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
downloadTest.fileDownloadTest(srcTextField.getText(), textArea);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
dNcButton.setBounds(212, 70, 89, 23);
contentPane.add(dNcButton);
}
}
文件下载测试
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JTextArea;
import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class FileDownloadTest {
public void fileDownloadTest(String src, JTextArea textArea) throws IOException, InterruptedException {
String path = src;
FileInputStream fin = new FileInputStream(new File(path));
XSSFWorkbook workbook = new XSSFWorkbook(fin);
XSSFSheet sheet = workbook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
System.out.println("rows are " + rows);
XSSFCell cell;
// Make sure that this directory exists
String dirName = src.substring(0, src.lastIndexOf("\\"));
File f = new File(dirName);
if (!f.exists()) {
f.mkdir();
}
System.out.println(f.getAbsolutePath() + " is Directory Name " + src + " is the file");
for (int i = 1; i < rows; i++) {
XSSFCell url = sheet.getRow(i).getCell(0);
String URLString = url.toString();
cell = sheet.getRow(i).getCell(7);
String fileName = URLString.substring(URLString.lastIndexOf("/") + 1);
int pageNumbers = downloadFiles(dirName, url.toString().replace("http", "https"));
if (cell == null) {
cell = sheet.getRow(i).createCell(1);
}
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(pageNumbers);
FileOutputStream fileOut = new FileOutputStream(path);
workbook.write(fileOut);
fileOut.close();
textArea.append("downloaded " + fileName + "\n");
System.out.println("Done");
}
workbook.close();
fin.close();
}
public static int downloadFiles(String dirName, String urlString) {
System.out.println("Downloading \'" + urlString + "\' PDF document...");
String fileName = urlString.substring(urlString.lastIndexOf("/") + 1);
System.out.println(fileName + " is file name");
try {
saveFileFromUrlWithJavaIO(dirName + "\\" + fileName, urlString);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Downloaded \'" + urlString + "\' PDF document...");
int pageNumber = 0;
try {
pageNumber = getNoOfPages(dirName + "\\" + fileName);
} catch (IOException e) {
e.printStackTrace();
}
return pageNumber;
}
public static int getNoOfPages(String fileName) throws IOException {
PDDocument document = PDDocument.load(new File(fileName));
int numberOfPages = document.getNumberOfPages();
return numberOfPages;
}
public static void saveFileFromUrlWithJavaIO(String fileName, String fileUrl)
throws MalformedURLException, IOException {
BufferedInputStream in = null;
FileOutputStream fout = null;
try {
in = new BufferedInputStream(new URL(fileUrl).openStream());
fout = new FileOutputStream(fileName);
byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
} finally {
if (in != null)
in.close();
if (fout != null)
fout.close();
}
}
public static void saveFileFromUrlWithCommonsIO(String fileName, String fileUrl)
throws MalformedURLException, IOException {
FileUtils.copyURLToFile(new URL(fileUrl), new File(fileName));
}
}
import java.io.BufferedInputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.net.MalformedURLException;
导入java.net.URL;
导入javax.swing.JTextArea;
导入org.apache.commons.io.FileUtils;
导入org.apache.pdfbox.pdmodel.PDDocument;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.xssf.usermodel.XSSFCell;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公共类文件下载测试{
public void fileDownloadTest(String src,JTextArea textArea)抛出IOException、interruptedeexception{
字符串路径=src;
FileInputStream fin=新FileInputStream(新文件(路径));
XSSF工作簿=新XSSF工作簿(fin);
XSSFSheet sheet=workbook.getSheetAt(0);
int rows=sheet.getPhysicalNumberOfRows();
System.out.println(“行为”+行);
XSSFCell细胞;
//确保此目录存在
字符串dirName=src.substring(0,src.lastIndexOf(“\\”);
文件f=新文件(目录名);
如果(!f.exists()){
f、 mkdir();
}
System.out.println(f.getAbsolutePath()+“是目录名”+src+“是文件”);
对于(int i=1;i
请让我知道如何解决这个问题
谢谢以上评论是正确的,您在向文本区域添加文本时处于EDT状态。这是因为您正在从dNcButton ActionListener实现调用downloadTest.fileDownloadTest 退出EDT是一个很好的实践,因为您需要执行一个长时间运行的操作,下载文件通常作为您不想在EDT上做的事情的示例 网上有很多关于如何摆脱EDT的资源,包括这个很棒的网站上的很多资源
与其他注释一样,您只需要在EDT上保留与UI相关的任务。否则,如果用户正在下载100个文件,UI将锁定,直到下载任务完成。SwingWorker是为Swing应用程序执行此操作的完美类。下面是一个简单的例子:
// Run your FileDownloadTest inside a SwingWorker
// or have the class extend SwingWorker
new SwingWorker<Void, String>() {
@Override
protected Void doInBackground() throws Exception {
//Perform downloading here (done off the EDT)
//Call publish(downloadedFileName) to be sent to the process method()
return null;
}
@Override
protected void process(List<String> chunks) {
// Modify textArea (done on the EDT)
StringBuilder downloadedFiles = new StringBuilder();
for (String fileName : chunks) {
downloadedFiles.append("downloaded" + fileName + "\n");
}
textArea.setText(downloadedFiles);
}
@Override
protected void done() {
//Anything you want to happen after doInBackground() finishes
}
}.execute();
//在SwingWorker中运行FileDownloadTest
//o