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);
}
}
- Java gui&IO流练习
- Java IO流--练习
- java-GUI小练习
- Java gui练习
- 07 JAVA IO流 练习
- Java IO流--练习2
- Java练习--GUI(9)
- Java io流练习(1)
- Java基础知识IO流(练习)
- Java.io练习
- java IO 练习感悟!
- Java IO 练习
- Java IO练习
- 20160228 java练习io
- java IO练习
- 【JAVA 日常练习】+ IO
- Java -- IO练习
- java基础 IO/线程/GUI,装饰模式
- 字符编码(哈夫曼编码)
- 安信可A7模块GPS定位功能测试及解析
- linux学习之pxe
- 乱码
- 约瑟夫环问题
- Java gui&IO流练习
- Prime Gap UVA
- Java Swing3-MyDialog的基本实现
- 原生JS实现省市区(县)三级联动选择
- 2017年上半年个人总结
- 计步器最多最小值自己输入(OC)
- C语言的内存分配malloc()和free()
- Matrix-Tree定理学习小记(详细介绍+证明)
- 51NOD 1255 字典序最小的子序列 【贪心】