java学习代码

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) {
		// TODO Auto-generated method stub

		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) {
		// TODO Auto-generated method stub

		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); // 创建Date类对象
				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) {
		// TODO Auto-generated method stub

		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) {
		// TODO Auto-generated method stub

		while(true){
			try{
				//在当前目录下创建名为word.txt的文本文件
				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) {
		// TODO Auto-generated method stub

		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) {
		// TODO Auto-generated method stub

		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) {
		// TODO Auto-generated method stub

		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) {
		// TODO Auto-generated method stub

		new JButtonText();
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值