java把txt录入并使用,如何读取用户在Java中的输入并将其写入文件

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

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值