JAVA 输入输出+图形用户界面

输入输出

一.流的分类
1.按方向分为输入流(外部—>程序)和输出流(程序—>外部);
2.按照读取单位分为字节流和字符流;
3.按是否直接与数据源打交道分为节点流和处理流。

调用流对象的读写方法大都需要处理IOException,该异常是检验异常,需要捕捉或在调用方法后加声明。

二、分为五个需要掌握的方法
1.字节输入流
2.字节输出流
利用一张图片读取二进制数生成一张一摸一样的图片。

public class Main {
	public static void main(String[] args) throws Exception{
		FileInputStream fis=new FileInputStream("e:/2.jpg");
		List<Integer> l1=new ArrayList<>();
		while(true)
		{
			int a=fis.read();
			if(a==-1)
				break;
			l1.add(a);
		}
		fis.close():
		File f=new File("e:/1");

		FileOutputStream fos=new FileOutputStream("e:/1.jpg");
		for(int x:l1)
			fos.write(x);
		fos.close();
	}
}

3.字符输入流
4.字符输出流
注意点:
1.写数字到文档中时,不要加换行符之类的其他东西,不然也会当成要写入的信息处理。
2.会影响后续对写入数据的处理。

public class Main {
	public static void main(String[] args) throws Exception{
		//生成随机数写入文件
		FileWriter fw=new FileWriter("e:/1.txt");
		List<Integer> l1=new ArrayList<>();
		for(int i=0;i<100;i++) {
			int tmp=(int)(Math.random()*10);
			l1.add(tmp);
			//System.out.println(tmp);
		}
		for(int x:l1)
			fw.write(x);
		fw.close();
		
		//读出随机数统计次数
		FileReader fr=new FileReader("e:/1.txt"); 
		Map<Integer, Integer> mp=new TreeMap<>();
		while(true)
		{
			int tmp=fr.read();
			if(tmp==-1)
				break;
			//System.out.println(tmp);
			if(mp.get(tmp)==null)
				mp.put(tmp,1);
			else {
				mp.put(tmp, mp.get(tmp)+1);
			}
		}
		fr.close();
		for(Entry<Integer, Integer> x:mp.entrySet()) {
			System.out.print(x.getKey()+"\t"+x.getValue()+"\r\n");
		}
		
		//结果写回原文档
		fw=new FileWriter("e:/1.txt");
		for(Entry<Integer, Integer> x:mp.entrySet()) {
			fw.write(x.getKey()+"/t"+x.getValue()+"\r\n");
		}
		fw.close();
	}
}

5.Scanner方式对字符的读取

Scanner sc=new Scanner(new File("e:/1.txt"));
		while(sc.hasNextLine())
		{
			String tmp=sc.nextLine();
			
			System.out.println(tmp+"-------");
		}

1、目录、文件操作

(1)在d盘下建立一个目录dir1

    File f1=new File("d:/dir1");

	f1.mkdir();

(2)在目录dir1下建立文本文件1.txt,并在里面输入内容。

    File f2=new File("d:/dir1/1.txt");

	f2.createNewFile();

(3)输出1.txt文件的大小及最后修改日期。

    System.out.println(f2.length());

	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	System.out.println(sdf.format(new Date(f2.lastModified())));

(4)将1.txt重命名为2.txt。

    File f3=new File("d:/dir1/2.txt");

	f2.renameTo(f3);

(5)将目录dir1删除。

	f3.delete();

	f1.delete();

图形用户界面

对于添加文本、标签、按钮、设置监听的简单使用,框架如下:
1.将要用到的窗口、面板、标签、文本、按钮在构造函数前就准备好。
2.根据需要装入构造函数。
4.事件源注册监听器,为了处理方便,通常让容器自身作为监听器。
5.监听器类实现监听接口,在相应方法中编写事件处理代码。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main {
	
	public static void main(String[] args) {
		new Fr();
	}
}
class Fr extends JFrame implements ActionListener{
	JTextField f1=new JTextField(10);
	JTextField f2=new JTextField(10);
	JLabel add=new JLabel("+");
	JTextField f3=new JTextField(10);
	JLabel eq=new JLabel("=");
	JButton gen=new JButton("生成随机数");
	JButton cal=new JButton("计算");
	JPanel p=new JPanel();
	JLabel tips=new JLabel();
	Fr(){
		this.setLayout(new FlowLayout());
		this.add(f1);
		this.add(add);
		this.add(f2);
		this.add(eq);
		this.add(f3);
		this.add(p);
		p.add(gen);
		p.add(cal);
		this.add(tips);
		gen.addActionListener(this);
		cal.addActionListener(this);
		this.setSize(400,250);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand()=="生成随机数") {
			int a=(int)(Math.random()*100);
			int b=(int)(Math.random()*100);
			f1.setText(a+"");
			f2.setText(b+"");
		}
		else if(e.getSource()==cal) {
			try {
				int a=Integer.parseInt(f1.getText());
				int b=Integer.parseInt(f2.getText());
				int c=Integer.parseInt(f3.getText());
				int ans=a+b;
				if(ans==c)
					tips.setText("计算正确!");
				else {
					tips.setText("计算错误!");
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				tips.setText("输入格式错误!");
			}
		}
		
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,要复制Java创建的图形用户界面写入的文档,可以按照以下步骤进行: 1. 创建一个包含两个文本框和一个复制按钮的窗口,其中一个文本框用于输入源文件路径,另一个文本框用于输入目标文件路径。 2. 创建一个事件监听器,当用户点击复制按钮时,触发事件,执行以下代码。 3. 使用Java文件输入流(FileInputStream)读取源文件路径的文本框中的内容,并使用Java文件输出流(FileOutputStream)创建目标文件路径的文本框中的内容。 4. 使用Java的缓冲输入流(BufferedInputStream)和缓冲输出流(BufferedOutputStream)来提高读写效率。 5. 使用Java的字节数组(byte[])来存储读取到的数据。 6. 使用Java的循环结构,不断地从源文件中读取数据,并将读取到的数据写入到目标文件中,直到源文件中的数据全部读取完毕。 7. 最后,关闭文件输入流和文件输出流,释放资源。 以下是一个简单的实现示例: ```java import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class FileCopyGUI extends JFrame implements ActionListener { private JTextField sourceField, destinationField; private JButton copyButton; public static void main(String[] args) { new FileCopyGUI(); } public FileCopyGUI() { super("文件复制"); // 创建UI组件 sourceField = new JTextField(20); destinationField = new JTextField(20); copyButton = new JButton("复制"); // 将组件添加到窗口 JPanel panel = new JPanel(); panel.add(new JLabel("源文件路径:")); panel.add(sourceField); panel.add(new JLabel("目标文件路径:")); panel.add(destinationField); panel.add(copyButton); getContentPane().add(panel); // 添加事件监听器 copyButton.addActionListener(this); // 设置窗口属性 setSize(400, 100); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == copyButton) { // 获取源文件路径和目标文件路径 String sourcePath = sourceField.getText(); String destinationPath = destinationField.getText(); try { // 创建输入流和输出流 FileInputStream fis = new FileInputStream(sourcePath); BufferedInputStream bis = new BufferedInputStream(fis); FileOutputStream fos = new FileOutputStream(destinationPath); BufferedOutputStream bos = new BufferedOutputStream(fos); // 复制文件 byte[] buffer = new byte[1024]; int len; while ((len = bis.read(buffer)) != -1) { bos.write(buffer, 0, len); } // 关闭流 bis.close(); bos.close(); JOptionPane.showMessageDialog(this, "复制成功"); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "复制失败: " + ex.getMessage()); } } } } ``` 在这个示例中,我们创建了一个包含源文件路径、目标文件路径和复制按钮的窗口,当用户点击复制按钮时,程序会读取源文件路径和目标文件路径的文本框中的内容,并进行文件复制操作。当复制完成时,程序会弹出一个对话框提示用户复制成功或失败。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值