2015-08-21 39 views
0

我有一个简单的java购物应用程序与netbeans图形用户界面,当有人按结帐jbutton我想保存jtextfield值到外部.txt文件。每次有人启动结账选项时,我希望将每个交易值随时间保存到.txt文件中。我怎样才能做到这一点 ?如何将jtextfield值保存为.txt文件作为日志?

+0

在此处发布相关的代码。到目前为止你做了什么? –

回答

1

首先获得您的JTextField通过的文本价值,

JTextField textField = ...; // 
String text = textField.getText(); 

则该值传递给writeToFile方法如下图所示,

writeToFile(text); 

将writeToFile方法

void writeToFile(String fileName, String text) throws Exception { 
    FileOutputStream out = new FileOutputStream(fileName, true); 
    out.write(text); 
} 
0

使用此代码

String content = textFieldName.getText(); //step1: get the content of the textfield 

    try { 

       File file = new File("https://stackoverflow.com/users/mkyong/filename.txt"); 

       // if file doesnt exists, then create it 
       if (!file.exists()) { 
        file.createNewFile(); 
       } 

       FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
       BufferedWriter bw = new BufferedWriter(fw); 
       bw.write(content); //step2: write it 
       bw.close(); 

       System.out.println("Done"); 

      } catch (IOException e) { 
       e.printStackTrace(); 
} 
相关问题