Java文件输入

初识I/O,利用WindowsBuilder组件画一个用户登录界面,并将登录名和密码存入文件中
一、文件写入

package com.junel.ioTest;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class testFW {
	private static FileWriter fileWriter;
	private static BufferedWriter bw;

	private static File file = new File("/home/junel/junel/Eclipse/test/test.docx");

	public static void creatFile() {
		if (!file.getParentFile().exists()) {
			try {
				file.getParentFile().mkdirs();
				file.createNewFile();
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}

	public static void fileWriter(String s) {
		try {
			fileWriter = new FileWriter(file, true);
			bw = new BufferedWriter(fileWriter);
			bw.write(s);
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				bw.close();
				fileWriter.close();
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
}

二、界面和监听事件

package com.junel.ioTest;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

public class testFrame extends JFrame {

	private JPanel contentPane;
	private JTextField userName;
	private JTextField userPassword;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		testFW tf = new testFW();
		tf.creatFile();

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					testFrame frame = new testFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public testFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 602, 436);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("用户名:");
		lblNewLabel.setFont(new Font("Dialog", Font.BOLD, 20));
		lblNewLabel.setBounds(126, 111, 122, 29);
		contentPane.add(lblNewLabel);

		JLabel label = new JLabel("密  码:");
		label.setFont(new Font("Dialog", Font.BOLD, 20));
		label.setBounds(126, 187, 122, 29);
		contentPane.add(label);

		JButton addInfo = new JButton("添加");
		addInfo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean flag = false;
				StringBuilder s = new StringBuilder();
				String userName1 = userName.getText();
				String userPassword1 = userPassword.getText();
				if (userName1 == null || userName1.equals("") || userPassword1.equals("") || userPassword1 == null) {
					JOptionPane.showMessageDialog(null, "请输入完整", "警告", JOptionPane.PLAIN_MESSAGE);
				} else {
					s.append(userName1);
					s.append(",").append(userPassword1);
					s.append("\n");
					String ss = s.toString();
					testFW.fileWriter(ss);
					flag = true;
				}
				if (flag) {
					JOptionPane.showMessageDialog(null, "添加成功", "成功", JOptionPane.PLAIN_MESSAGE);
					userName.setText(null);
					userPassword.setText(null);
					flag = false;
				}
			}
		});
		addInfo.setFont(new Font("Dialog", Font.BOLD, 22));
		addInfo.setBounds(202, 260, 150, 46);
		contentPane.add(addInfo);

		userName = new JTextField();
		userName.setBounds(241, 113, 193, 29);
		contentPane.add(userName);
		userName.setColumns(10);

		userPassword = new JTextField();
		userPassword.setColumns(10);
		userPassword.setBounds(241, 187, 193, 29);
		contentPane.add(userPassword);
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java是一种面向对象的编程语言,它提供了很多内置的输入输出库。下面两段代码分别演示了Java中如何进行文件输入和输出。 文件输入: ``` import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class FileInput { public static void main(String[] args) { try { File file = new File("input.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } scanner.close(); } catch (FileNotFoundException e) { System.out.println("文件不存在"); } } } ``` 文件输出: ``` import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; public class FileOutput { public static void main(String[] args) { try { File file = new File("output.txt"); PrintWriter writer = new PrintWriter(file); writer.println("Hello World!"); writer.close(); } catch (FileNotFoundException e) { System.out.println("文件不存在"); } } } ``` 以上代码中,文件输入使用了Scanner类,其构造参数为一个File对象,表示要读取的文件。然后使用while循环读取文件中每一行的内容,直到文件结束。最后记得关闭Scanner对象。 文件输出使用了PrintWriter类,其构造参数同样为一个File对象,表示要写入的文件。然后使用println方法向文件写入内容。最后记得关闭PrintWriter对象。 以上是Java文件输入输出代码的基本格式,当然,在实际开发中还会涉及到更多的细节问题,比如文件编码、文件路径、读写异常处理等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值