Java gui&IO流练习

来源:互联网 发布:数据库处理查询的步骤 编辑:程序博客网 时间:2024/05/16 10:48

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

//创建单行文本框,单击按钮将文本框内容通过FileOutputStream单字节输出保存到.txt
public class TestDemo1 {
public static void main(String[] args) throws FileNotFoundException {
JFrame jf = new JFrame();
jf.setBounds(1378/4, 768/4, 1378/2, 768/2);
jf.setTitle(“E2”);
jf.setLayout(new FlowLayout());
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final JTextField jtf = new JTextField(10);
JButton jb = new JButton(“文本框输入 (鼠标单击按钮事件) 保存到文本”);
jb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(“G:\eclipse\JavaWorkSpace\output.txt”);
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}

String starray = jtf.getText();
byte[] bys = starray.getBytes();

for(byte b : bys){
try {fos.write(b);
} catch (IOException e1) {
e1.printStackTrace();}
}
jtf.setText(“”);
}
});

jf.add(jtf);
jf.add(jb);
jf.setVisible(true);
}
}

//创建单行文本框,单击按钮将文本框内容通过BufferedOutputStream单字节输出保存到.txt
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class TestDemo1 {
public static void main(String[] args) throws FileNotFoundException {
JFrame jf = new JFrame();
jf.setBounds(1378/4, 768/4, 1378/2, 768/2);
jf.setTitle(“E2”);
jf.setLayout(new FlowLayout()); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

final JTextField jtf = new JTextField(10);
JButton jb = new JButton(“文本框输入 (鼠标单击按钮事件) 保存到文本”);
jb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(“G:\eclipse\JavaWorkSpace\output.txt”));
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}

String starray = jtf.getText();
byte[] bys = starray.getBytes();
for(byte b : bys){
try {bos.write(b);
} catch (IOException e1) {
e1.printStackTrace();
}
}

try {
bos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
jtf.setText(“”);

}
});

jf.add(jtf);
jf.add(jb);
jf.setVisible(true);
}
}

//创建单行文本框,单击按钮将文本框内容通过BufferedOutputStream字节数组输出保存到.txt
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import java.lang.Math;
import java.util.Arrays;

public class TestDemo1 {
public static void main(String[] args) throws FileNotFoundException {
JFrame jf = new JFrame();
jf.setBounds(1378/4, 768/4, 1378/2, 768/2);
jf.setTitle(“E2”);
jf.setLayout(new FlowLayout()); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

final JTextField jtf = new JTextField(10);
JButton jb = new JButton(“文本框输入 (鼠标单击按钮事件) 保存到文本”);
jb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(“G:\eclipse\JavaWorkSpace\output.txt”));
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}

String starray = jtf.getText();
byte[] bys = starray.getBytes();
int len = 6;
System.out.println(“input byte array length:”+bys.length);
for(int startindex=0,endindex = len;endindex <= bys.length; startindex = endindex,endindex = Math.min(endindex + len , bys.length)){
System.out.println(“start:”+startindex+”endindex:”+endindex);
byte[] subby = Arrays.copyOfRange(bys, startindex, endindex);
try {
bos.write(subby);
} catch (IOException e1) {
e1.printStackTrace();
}
if(endindex == bys.length) break;
}

try {
bos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
jtf.setText(“”);
}
});

jf.add(jtf);
jf.add(jb);
jf.setVisible(true);
}
}

//创建单行文本框,单击按钮将文本框内容通过OutputStreamWriter字符数组输出保存到.txt

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class TestDemo2 {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setBounds(1378/4, 768/4, 1378/2, 768/2);
jf.setTitle(“E2”);
jf.setLayout(new FlowLayout());
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

final JTextField jtf = new JTextField(10);
JButton jb = new JButton(“文本框输入 (鼠标单击按钮事件) 保存到文本”);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
OutputStreamWriter osw = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(“G:\eclipse\JavaWorkSpace\output.txt”));
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
String starray = jtf.getText();
char[] chs = starray.toCharArray();
int len = 6;
System.out.println(“input char array length:”+chs.length);
for(int startindex=0,endindex = len;
endindex <= chs.length;
startindex = endindex,endindex = Math.min(endindex + len , chs.length)){
System.out.println(“start:”+startindex+”endindex:”+endindex);
char[] subch = Arrays.copyOfRange(chs, startindex, endindex);
try {
osw.write(subch);
} catch (IOException e1) {
e1.printStackTrace();
}

if(endindex == chs.length) break;
}

try {
osw.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
jtf.setText(“”);
}
});
jf.add(jtf);
jf.add(jb);
jf.setVisible(true);
}
}

原创粉丝点击