簡體   English   中英

如何從Java中的用戶輸入中讀取並將其寫入文件

[英]How to read from user's input in Java and write it to a file

我想創建一個簡單的獨立應用程序,該應用程序將從用戶那里獲取一些輸入(一些數字和數學函數f(x,y ...))並將其寫入文件。 然后借助該文件,我將運行命令。

我需要的基本成分:

-JTextArea供用戶輸入。

-ButtonHandler / ActionListener並將輸入寫入(txt)文件

-ButtonHandler / ActionLister執行命令

最好的方法是什么?

我擁有的當前正在運行的代碼(基本上是玩具)-不編寫任何內容,僅執行-是:

import java.applet.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dialog;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.util.*;
import java.io.BufferedWriter;

public class Runcommand3
{
  public static void main(String[] args) throws FileNotFoundException, IOException
  {
    //JApplet applet = new JApplet();
    //applet.init();

    final JFrame frame = new JFrame("Change Backlight");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setLayout(null);
    frame.add(panel);
    JButton button = new JButton("Click me to Run");
    button.setBounds(55,100,160,30);
    panel.add(button);

    frame.setSize(260,180);
    frame.setVisible(true);
    //This is an Action Listener which reacts to clicking on the button
    button.addActionListener(new ButtonHandler());
  }
}
 class ButtonHandler implements ActionListener{
                public void actionPerformed(ActionEvent event){
                double value = Double.parseDouble(
                JOptionPane.showInputDialog("please enter backlight value"));
                //File theFile = new File("thisfile.txt");
                //theFile.write(value);
                String command = "xbacklight -set " + value;
                try{Runtime run = Runtime.getRuntime();
                Process pr = run.exec(command);}
                catch(IOException t){t.printStackTrace();}
                  }
                }

在上面的示例中,如何將“值”寫入文件? 然后,如何添加更多輸入(更多文本字段)? 我可以在同一堂課上做還是需要更多? 我的困惑(主要但不僅限於)是因為在ButtonHandler類中我無法定義任何其他對象(即,打開和寫入文件等)。

這就是我要寫入文件的方式。 我將讓您將此代碼轉換為GUI進行練習。 查看有關BufferedWriterFileWriter的更多信息

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Scanner;

public class Files {

    public static void main(String args[]){

        System.out.print("Enter Text: ");
        Scanner scan = new Scanner(System.in);
        String text = scan.nextLine();
        FileWriter fWriter = null;
        BufferedWriter writer = null;
        try {
          fWriter = new FileWriter("text.txt");
          writer = new BufferedWriter(fWriter);
          writer.write(text);
          writer.newLine();
          writer.close();
          System.err.println("Your input of " + text.length() + " characters was saved.");
        } catch (Exception e) {
            System.out.println("Error!");
        }
    }

}

對於第二個問題,您可能想考慮在JFrame上有一個JTextField,供用戶輸入行而不是JOptionPane。 這只是一個簡單的文本框,您可以在每次按下按鈕時將框的內容添加到文件中:

public static void main(String[] args) {
    JTextField myTextField = new JTextField();
    // Your code, set size and position of textfield
    panel.add(myTextField);
}

class ButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        String text = myTextField.getText();
        myTextField.setText("");
        new BufferedWriter(new FileWriter("text.txt")).write(text).newLine().close();
        // the rest of your code
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM