我正在製作一個程序,用戶在文本字段中輸入值,然後生成一個目錄和文本文件。但問題是當我運行它生成的程序目錄時,當程序發現異常時,它也會創建一個文本文件。我只希望發生在用戶輸入值上。然後它使目錄和文本文件。Java:文件目錄和替換問題
第二個: 當生成文本文件,然後再次運行程序運行文本文件被替換爲新的。舊的被替換。我想使新的文本文件上再次運行程序一樣open1.txt & open2.txt
代碼:
public class V extends JFrame{
private JTextField t1;
private JTextField t2;
private JButton b1;
public V(){
File file1 = new File("C:\\Users\\hamel\\Desktop\\z");
file1.mkdirs();
File file2 = new File("C:\\Users\\hamel\\Desktop\\z\\open.txt");
getContentPane().setLayout(null);
t1=new JTextField();
t1.setBounds(30,26,47,28);
getContentPane().add(t1);
t2=new JTextField();
t2.setBounds(103,26,47,28);
getContentPane().add(t2);
b1=new JButton("Click");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String w = t1.getText();
String x = t2.getText();
int i,j ,k;
try{
PrintWriter pw = new PrintWriter(file2);
i = Integer.parseInt(w);
k = Integer.parseInt(x);
pw.println("You Enter in Text Field 1:"+i);
pw.println("You Enter in Text Field 2:"+k);
pw.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "Error");
}
}
});
b1.setBounds(235,26,87,28);
getContentPane().add(b1);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
主營:
public class Open_2 {
public static void main(String[] args) {
V next = new V();
}
}
第一個問題是解決。你可以做一個第二個問題的代碼。 – hamel123
答案已經給第二個問題的選項也...你不滿意嗎? – Codebender
嘗試{ \t \t \t PrintWriter pw = new PrintWriter(file2); \t \t \t \t i = Integer.parseInt(w); \t \t \t \t \t \t \t K =的Integer.parseInt(X); \t \t \t file1.mkdirs();當單擊按鈕時它會發生異常錯誤 – hamel123