[英]How to end java program
我必須添加一些exit
語句嗎?
代碼似乎運行良好,但在完成任務后我仍然無法與控制台交互。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package excelb;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
/**
*
* @author hp
*/
public class Excelb {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
String row;
String row2;
File fn = null;
int counter = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter the run identifier for output file");
String output = in.nextLine();
//input
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setCurrentDirectory(new File("C:/"));
int result = jFileChooser.showOpenDialog(new JFrame());
if (result == JFileChooser.APPROVE_OPTION) {
fn=jFileChooser.getSelectedFile();
System.out.println("Data file: " + fn.getAbsolutePath());
//System.out.println("Data file: " + fn.getTitle());
}
BufferedReader csvReader = new BufferedReader(new FileReader("Keywords.csv"));
BufferedReader csvReader2 = new BufferedReader(new FileReader(fn.getAbsolutePath()));
row = csvReader.readLine();
String[] data = row.split(",");
while (counter < data.length) {
while ((row2 = csvReader2.readLine()) != null) {
String[] data2 = row2.split(",");
for (int j = 0; j < data.length; j++) {
if ((data2[data2.length - 1].contains(data[j]))) {
//DateFormat df = new SimpleDateFormat("dd/MM/yy");
//Date dateobj = new Date();
//String d = df.format(dateobj);
//d = d.replaceAll("/", "");
String newFile = data[j] + "_" + output + ".csv";
FileWriter csvWriter = new FileWriter(newFile, true);
for (int i = 0; i < data2.length; i++) {
csvWriter.append(data2[i]);
csvWriter.append(",");
}
csvWriter.append("\n");
csvWriter.close();
}
}
}
counter++;
}
System.out.println("Finished");
csvReader.close();
csvReader2.close();
}
}
Pshemo 的回答解釋了為什么您的應用程序沒有結束。
但它確實沒能說出關鍵點,那就是:
要退出 Java 應用程序,請運行System.exit(0);
- 如果發生錯誤,請考慮返回非 0 值。 不要等待 JVM 自行退出(一旦剩下的活動線程設置了守護程序標志,它就會退出) - 如果關鍵是關閉應用程序,這是一個脆弱的策略。 只需調用exit
。
您的應用程序由兩個獨立的線程組成:
main
方法 即使主線程將結束其執行, swing線程仍在運行並且不允許您的應用程序退出。
順便說一句,這是一件好事。 我們在main
方法中構建 GUI 組件,例如帶有按鈕、標簽等的框架。 現在,當主方法/線程結束時,是否應該關閉應用程序並連同它一起構建 GUI? 那沒有多大意義。
因為您使用new JFrame()
,它啟動了 swing 線程,負責處理與 swing/AWT 組件相關的事件(如按鈕單擊等)。
如果您不打算基於 swing GUI 創建應用程序,請不要使用new JFrame()
啟動 swing 線程。
在您的情況下,簡單的解決方案是將null
而不是new JFrame()
傳遞給jFileChooser.showOpenDialog
。 這也將使該對話框放置在屏幕的中心:
int result = jFileChooser.showOpenDialog(null);
要強制關閉您的應用程序,您可以使用System.exit(0)
。
但一般來說,防止問題發生比解決問題要好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.