I/O输入输入输出
package study12;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileStreamTest {
public static void main(String[] args) {
File file=new File("word.txt");
try{
FileOutputStream out=new FileOutputStream(file);
String content="你见过凌晨4点的洛杉矶嘛?";
byte buy[]=content.getBytes();
out.write(buy);
out.close();
}catch(IOException e){
e.printStackTrace();
}
try{
FileInputStream in=new FileInputStream(file);
byte byt[]=new byte[1024];
int len=in.read(byt);
System.out.println("文件中的信息是"+new String(byt,0,len));
}catch(IOException e){
e.printStackTrace();
}
}
}
package study12;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileTest {
public static void main(String[] args) {
File file=new File("test.txt");
if(!file.exists()){
System.out.println("未在指定目录下找到文件名为test的文本文件!正在创建...");
try{
file.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
System.out.println("文件创建成功");
}
else{
System.out.println("找到文件名为test的文件");
if(file.isFile()&&file.canRead()){
System.out.println("文件可读!正在读取文件信息...");
String fileName=file.getName();
String filePath=file.getAbsolutePath();
boolean hidden=file.isHidden();
long len=file.length();
long tempTime=file.lastModified();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date(tempTime);
String time=sdf.format(date);
System.out.println("文件名:"+fileName);
System.out.println("文件的绝对路径:"+filePath);
System.out.println("文件是否隐藏文件:"+hidden);
System.out.println("文件中的字节数:"+len);
System.out.println("文件的最后修改时间:"+time);
file.delete();
System.out.println("文件被删除了");
}else{
System.out.println("文件不可读");
}
}
}
}
package study12;
import java.io.File;
public class FolerTest {
public static void main(String[] args) {
String path="C:\\Test";
for(int i=1;i<=10;i++){
File folder=new File(path+"\\"+i);
if(!folder.exists()){
folder.mkdirs();
}
}
System.out.println("文件夹创建成功");
File file=new File("C:\\");
File[] files=file.listFiles();
for(File folder:files){
if(folder.isFile()){
System.out.println(folder.getName()+"文件");
}else if(folder.isDirectory()){
System.out.println(folder.getName()+"文件夹");
}
}
}
}
package study12;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ReaderAndWriter {
public static void main(String[] args) {
while(true){
try{
File file =new File("word.txt");
if(!file.exists()){
file.createNewFile();
}else{
System.out.println("请输入要执行的操作序号");
Scanner in=new Scanner(System.in);
int choose=in.nextInt();
switch(choose){
case 1:
System.out.println("请输入要写入文件的内容");
String tempStr=in.next();
FileWriter fw=null;
try{
fw=new FileWriter(file,true);
fw.write(tempStr+"\r\n");
}catch(IOException e){
e.printStackTrace();
}finally{
fw.close();
}
System.out.println("上述文件已写入到文本文件中");
break;
case 2:
FileReader fr=null;
if(file.length()==0){
System.out.println("文本中的字符数为0!!!");
}else{
try{
fr=new FileReader(file);
char[] cbuf=new char[1024];
int has=-1;
while((has=fr.read(cbuf))!=-1){
System.out.println("文件中的内容:"+new String(cbuf,0,has));
}
}catch(IOException e){
e.printStackTrace();
}finally{
fr.close();
}
}
break;
default:
System.out.println("请输入符合要求的有效数字");
break;
}
}
}catch(InputMismatchException imexc){
System.out.println("输入的文本格式不正确");
}catch(IOException e){
e.printStackTrace();
}
}
}
}
Swing程序设计
package study11;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class JComboBoxTest extends JFrame{
public JComboBoxTest(){
Container c=getContentPane();
setLayout(new FlowLayout());
setBounds(110,11,350,350);
setVisible(true);
JComboBox<String> combox=new JComboBox<String>();
combox.setBounds(110,11, 80, 21);
combox.addItem("身份证");
combox.addItem("军人证");
combox.addItem("学生证");
combox.addItem("工作证");
c.add(combox);
JLabel lb=new JLabel("");
lb.setBounds(100, 100, 200, 300);
c.add(lb);
JButton jb=new JButton("确定");
c.add(jb);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
lb.setText("您选择的是"+combox.getSelectedItem());
}
});
}
public static void main(String[] args) {
new JComboBoxTest();
}
}
package study10;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class CheckBoxTest extends JFrame{
public CheckBoxTest(){
Container c=getContentPane();
c.setLayout(new FlowLayout());
setBounds(100,100,250,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
JCheckBox c1=new JCheckBox("1");
JCheckBox c2=new JCheckBox("2");
JCheckBox c3=new JCheckBox("3");
c.add(c1);
c.add(c2);
c.add(c3);
JButton btn=new JButton("打印");
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println(c1.getText()+"按钮中状态"+c1.isSelected());
System.out.println(c2.getText()+"按钮中状态"+c2.isSelected());
System.out.println(c3.getText()+"按钮中状态"+c3.isSelected());
}
});
c.add(btn);
}
public static void main(String[] args) {
new CheckBoxTest();
}
}
package study9;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import study8.JButtonText;
public class RadioButtonTest extends JFrame{
public RadioButtonTest(){
setLayout(new FlowLayout());
setBounds(200,200,200,200);
Container c=getContentPane();
JRadioButton rb=new JRadioButton("普通发送");
rb.setSelected(true);
rb.setFont(new Font("宋体",Font.PLAIN,12));
rb.setBounds(20,30,75,22);
rb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(rb.isSelected()){
JOptionPane.showMessageDialog(null, "您选择的是:"+rb.getText(),"提醒",JOptionPane.INFORMATION_MESSAGE);
}
}
});
c.add(rb);
JRadioButton rb1=new JRadioButton("加密发送");
rb1.setSelected(true);
rb1.setFont(new Font("宋体",Font.PLAIN,12));
rb1.setBounds(100,30,75,22);
rb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(rb1.isSelected()){
JOptionPane.showMessageDialog(null, "您选择的是:"+rb1.getText(),"提醒",JOptionPane.INFORMATION_MESSAGE);
}
}
});
c.add(rb1);
ButtonGroup group=new ButtonGroup();
group.add(rb);
group.add(rb1);
}
public static void main(String[] args) {
RadioButtonTest frame = new RadioButtonTest();
frame.setVisible(true);
}
}
package study8;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class JButtonText extends JFrame{
public JButtonText(){
Icon icon =new ImageIcon("src/study8/1.jpg");
setLayout(new GridLayout(3,2,5,5));
Container container=getContentPane();
JButton jb[]=new JButton[6];
for(int i=0;i<6;i++){
jb[i]=new JButton();
container.add(jb[i]);
}
jb[0].setText("不可用");
jb[0].setEnabled(false);
jb[1].setText("有背景色");
jb[1].setBackground(Color.YELLOW);
jb[2].setText("无边框");
jb[2].setBorderPainted(false);
jb[3].setText("有边框");
jb[3].setBorder(BorderFactory.createLineBorder(Color.RED));
jb[4].setIcon(icon);
jb[4].setToolTipText("图片按钮");
jb[5].setText("可点击");
jb[5].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(JButtonText.this, "点击按钮");
}
});
setBounds(100,100,250,250);
setVisible(true);
}
public static void main(String[] args) {
new JButtonText();
}
}