⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 streamlesson.java

📁 这些是用java编写的小程序
💻 JAVA
字号:
package com.lesson;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class StreamLesson {
	
	private JFrame window;
	private JButton open;
	private JButton save;
	private JTextArea jta;
	
	public void initGUI(){
		window = new JFrame("写字板");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(new Dimension(800,600));
		JPanel bp= new JPanel();
		open = new JButton("打开");
		save = new JButton("保存");
		open.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成方法存根
				open();
			}
		});
		save.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成方法存根
				save();
			}
		});
		
		bp.add(open);
		bp.add(save);
		window.add(bp,BorderLayout.NORTH);
		jta = new JTextArea();
		JScrollPane jp = new JScrollPane(jta);
		window.add(jp,BorderLayout.CENTER);
		window.setVisible(true);
	}
	
	private void open(){
		String file = JOptionPane.showInputDialog("请输入文件名及路径:");
		File f = new File(file);
		if(f.exists()){
			String content = read(f);
			jta.setText(content);
		}
		else
			JOptionPane.showMessageDialog(window, "文件不存在");
		
	}
	
	private void save(){
		String content = jta.getText();
		String file = JOptionPane.showInputDialog("请输入文件名及路径:");
		File f = new File(file);
		if(f.exists()){
			int i = JOptionPane.showConfirmDialog(window, "文件已存在,是否覆盖?");
			if(i==1)
				save();
			if(i==2)
				return;
			if(i==0)
				write(f,content);
		}
		else
		{
			write(f,content);
		}
	}
	
	public String read(File file){
		try {
			BufferedReader fw = new BufferedReader(new FileReader(file));
			
			StringBuffer rows = new StringBuffer();
			String row = null;
			while((row = fw.readLine())!=null){
				rows.append(row);
			}
			fw.close();
			return rows.toString();
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
			return null;
		}
		
		
	}
	
	public void write(File file,String content){
		PrintWriter pw=null;
		try {
			pw = new PrintWriter(new FileWriter(file));
			pw.write(content);
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} finally{
			if(pw!=null)
			pw.close();
		}
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自动生成方法存根
		//File f = new File("c:\\a.txt");
		StreamLesson sl = new StreamLesson();
		sl.initGUI();
//		String send = JOptionPane.showInputDialog("请输入内容:");
//		sl.write(f,send);
//		if(f.exists()){
//		String c = sl.read(f);
//		System.out.println("content:"+c);
//		}
		
		

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -