「java调用文本」java调用文件
本篇文章给大家谈谈java调用文本,以及java调用文件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java输出到界面文本框调用什么类
在GUI中,常用文本框和文本区实现数据的输入和输出。如果采用文本区输入,通常另设一个数据输入完成按钮。当数据输入结束时,点击这个按钮。事件处理程序利用getText()方法从文本区中读取字符串信息。对于采用文本框作为输入的情况,最后输入的回车符可以激发输入完成事件,通常不用另设按钮。事件处理程序可以利用单词分析器分析出一个个数,再利用字符串转换数值方法,获得输入的数值。对于输出,程序先将数值转换成字符串,然后通过setText()方法将数据输出到文本框或文本区。
java读取文本文件代码
java读取文本文件的方法有很多 这个例子主要介绍最简单 最常用的BufferedReader类 完整例子如下 package net chinaunix blog hzm text;import java io BufferedReader;import java io FileReader;import java io IOException;public class ReadFile {private String path;public ReadFile(String filePath){path = filePath;}public String[] openFile() throws IOException{FileReader fr = new FileReader(path) BufferedReader textReader = new BufferedReader(fr) String[] textData = new String[readLines()];int i;for(i= ; i readLines() i++){textData[i] = textReader readLine() }textReader close() return textData;}int readLines() throws IOException{FileReader fileToRead = new FileReader(path) BufferedReader bf = new BufferedReader(fileToRead) int numberOfLines = ;@SuppressWarnings( unused )String oneLine;while((oneLine = bf readLine()) != null){numberOfLines++;}bf close() return numberOfLines;}}package net chinaunix blog hzm text;import java io IOException;public class FileData {public static void main(String[] args) throws IOException{String filePath = C:/text txt ;try{ReadFile reader = new ReadFile(filePath) String[] content = reader openFile() int i;for(i= ;icontent length;i++){System out println(content[i]) }}catch(IOException e){System out println( 异常信息 + e getMessage()) }}}java io BufferedReaderThe buffer size may be specified or the default size may be used The default is large enough for most purposes In general each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly such as FileReaders and InputStreamReaders For example BufferedReader in = new BufferedReader(new FileReader( foo in )) will buffer the input from the specified file Without buffering each invocation of read() or readLine() could cause bytes to be read from the file converted into characters and then returned which can be very inefficient Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader java io FileReaderFileReader is meant for reading streams of characters For reading streams of raw bytes consider using a FileInputStream lishixinzhi/Article/program/Java/hx/201311/26249
java如何实现调用文本编辑器
String path = "notepad.exe";
try {
//相当于window的"开始"-"运行"的效果
Runtime.getRuntime().exec(path);
} catch (IOException e) {
e.printStackTrace();
}
JAVA 如何去调用文本框里输入的数据
你要从文本框得到数据,首先你要有一个图形界面才行,这个图形界面里面有文本框和确定按钮,实现你需要的功能。
下面是根据你的字符界面的程序改成的图形界面程序,你看看吧。
import java.awt.BorderLayout;import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GUITest { ArrayListCat b=new ArrayListCat();
public void addCat(){
final JFrame jf=new JFrame("addCat");
JPanel jp1=new JPanel(new GridLayout(5,2));
JPanel jp2=new JPanel();
JLabel jl1=new JLabel("Id:");
JLabel jl2=new JLabel("Name:");
JLabel jl3=new JLabel("Age:");
JLabel jl4=new JLabel("Color:");
JLabel jl5=new JLabel("Price:");
final JTextField jtf1=new JTextField(15);
final JTextField jtf2=new JTextField(15);
final JTextField jtf3=new JTextField(15);
final JTextField jtf4=new JTextField(15);
final JTextField jtf5=new JTextField(15);
JButton confirm=new JButton("Confirm");
confirm.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
int p=1;
String Id=jtf1.getText();
if(Id==null || Id.equals("")){
JOptionPane.showMessageDialog(null, "The Cat's id could not be empty!");
jtf1.requestFocus();
}else{
for(int j=0;jb.size();j++){
Cat l=(Cat)b.get(j);
if(l.getId().equals(jtf1.getText())){
p=0;
}
}
if(p==0){
JOptionPane.showMessageDialog(null, "The Cat's id has exist,plese type it again!");
jtf1.requestFocus();
}else if(p==1){
String Name=jtf2.getText();
String Age=jtf3.getText();
String Color=jtf4.getText();
String Price=jtf5.getText();
Cat c = new Cat(Id,Name,Age,Color,Price);
b.add(c);
JOptionPane.showMessageDialog(null, "The Cat add to arraylist!");
jtf1.requestFocus();
//jf.dispose();//这一句在你需要关闭addCat窗口的时候调用
}
}
}
});
JButton cancel=new JButton("Cancel");
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
jf.dispose();
}
});
jp1.add(jl1);jp1.add(jtf1);
jp1.add(jl2);jp1.add(jtf2);
jp1.add(jl3);jp1.add(jtf3);
jp1.add(jl4);jp1.add(jtf4);
jp1.add(jl5);jp1.add(jtf5);
jp2.add(confirm);jp2.add(cancel);
jf.add(jp1,BorderLayout.CENTER);
jf.add(jp2,BorderLayout.SOUTH);
jf.setSize(300,200);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args) {
GUITest guitest=new GUITest();
guitest.addCat();
}
}
class Cat{
String Id;
String Name;
String Age;
String Color;
String Price;
Cat(){}
Cat(String Id,String Name,String Age,String Color,String Price){
this.Id=Id;
this.Name=Name;
this.Age=Age;
this.Color=Color;
this.Price=Price;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
}
java 怎么反复调用相同的文本框和按钮
通过equals方式进行判断。
1、定义2个文本框和一个按钮
JTextField textField1 = new JTextField(20);//文本框1
JTextField textField2 = new JTextField(20);//文本框2
JButton button = new JButton("按钮");
2、定义按钮事件,判断文字是否一致
button.addActionListener(new buttonListenerClass());//设置按钮监听事件
class buttonListenerClass implements ActionListener{ //定义按钮监听
@Override public void actionPerformed(ActionEvent e) {
if(textField1.getText().equals(textField1.getText())){//通过equals方式比较
//文字一致
}else{
//文字不一致
}
}}
关于java调用文本和java调用文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。