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

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

问题描述

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

我需要的基本成分:

- - 为用户输入的JTextArea。
$ b - ButtonHandler / ActionListener并将输入写入(txt)文件

<执行命令ButtonHandler / ActionLister

最好的方法是什么?



当前正在运行的代码(基本上是一个玩具) - 不写任何东西,只是执行 - 是:
$ b $ pre $ 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);
//这是一个Action Listener,它响应点击按钮
button.addActionListener(new ButtonHandler());


class ButtonHandler实现ActionListener {
public void actionPerformed(ActionEvent event){
double value = Double.parseDouble(
JOptionPane.showInputDialog 请输入背光值));
// File theFile = new File(thisfile.txt);
//theFile.write(valueue);
String command =xbacklight -set+ value;
try {Runtime run = Runtime.getRuntime();
Process pr = run.exec(command);}
catch(IOException t){t.printStackTrace();}
}
}


$ b

在上面的例子中,我怎样才能将值写入文件?那么,如何添加更多的输入(更多的文本框)呢?我可以在同一班上做,还是需要更多?
我的困惑来自于ButtonHandler类中我不能定义任何其他对象(即打开和写入文件等)的事实(主要但不仅仅是)。

解决方案

这是我写入文件的方式。我会让你把这个代码转换成你的GUI来练习。有关 BufferedWriter FileWriter

  import java.io.BufferedWriter; 
import java.io.FileWriter;
import java.util.Scanner;
$ b $ public class File {

public static void main(String args []){

System.out.print(Enter Text: );
扫描仪扫描=新扫描仪(System.in);
String text = scan.nextLine();
FileWriter fWriter = null;
BufferedWriter writer = null;
尝试{
fWriter = new FileWriter(text.txt);
writer = new BufferedWriter(fWriter);
writer.write(text);
writer.newLine();
writer.close();
System.err.println(您输入的+ text.length()+字符已保存。);
catch(Exception e){
System.out.println(Error!);
}
}

}


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.

Basic ingredients that I need:

-- JTextArea for users input.

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

-- ButtonHandler/ActionLister to execute a command

What is the best way to do it?

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();}
                  }
                }

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).

解决方案

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天全站免登陆