如何从Java中的用户输入中读取并将其写入文件 [英] How to read from user's input in Java and write it to a file

查看:20
本文介绍了如何从Java中的用户输入中读取并将其写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的独立应用程序,它将接受用户的一些输入(一些数字和数学函数 f(x,y...))并将它们写入文件.然后在这个文件的帮助下,我将运行一个命令.

I want to create a simple stand-alone application that will take some input from user (some numbers and mathematical functions f(x,y...)) and write them to a file. Then with the help of this file I will run a command.

我需要的基本成分:

-- 用于用户输入的 JTextArea.

-- JTextArea for users input.

-- ButtonHandler/ActionListener 并将输入写入 (txt) 文件

-- ButtonHandler/ActionListener and writing of the input to a (txt) file

-- ButtonHandler/ActionLister 执行命令

-- ButtonHandler/ActionLister to execute a command

最好的方法是什么?

我拥有的当前正在运行的代码(基本上是一个玩具)——它不写任何东西,只是执行——是:

A current running code that I have (basically a toy) - which does not write anything, just executes - is:

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 类内部我不能定义任何其他对象(即打开和写入文件等).

In the above example how can I write 'value' to a file? Then, how can I add more input (more textfields)? Can I do it in the same class or I need more? My confusion comes (mainly but not only) from the fact that inside the ButtonHandler class I can NOT define any other objects (ie, open and write files etc).

推荐答案

这是我写入文件的方式.我会让你把这段代码转换成你的 GUI 来练习.查看更多关于 BufferedWriterFileWriter

This is the way I would write to a file. I will let you convert this code into your GUI for practice. See more on BufferedWriter and FileWriter

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!");
        }
    }

}

这篇关于如何从Java中的用户输入中读取并将其写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆