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

📄 file_rw.java

📁 文件读写文件读写文件读写文件读写文件读写
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class File_RW extends JFrame implements ActionListener{
	String str_area,str_text;
	char ch_text;
	JLabel label1=new JLabel("文本文件的读写"); 
	//label1.setFont(new Font("Serif",Font.PLAIN,20));
	JLabel label2=new JLabel("请输入文件名:");
	JTextArea t1=new JTextArea(6,20);
	JTextField t2=new JTextField(12);
	JButton read=new JButton("读取");
	JButton write=new JButton("写入");
	JButton exit=new JButton("退出");
	
	public File_RW(){
		setTitle("文件读写");
		JPanel p1=new JPanel();
		JPanel p2=new JPanel();
		JPanel p3=new JPanel();
		JPanel p4=new JPanel();
		JPanel p5=new JPanel();
		JPanel p6=new JPanel();
		JPanel p7=new JPanel();
		JPanel p8=new JPanel();
		JPanel p9=new JPanel();
		JPanel p10=new JPanel();
		JScrollPane scroll=new JScrollPane(t1);
	
		p1.setLayout(new FlowLayout());
		p1.add(label1);
	
		
		p2.setLayout(new FlowLayout());
		p2.add(scroll);
		
		
		p3.setLayout(new BorderLayout());	
		p3.add("North",p1);
		p3.add("South",p2);
		
		
		p4.setLayout(new BorderLayout());
		p4.add("North",label2);
	
		
		p5.setLayout(new FlowLayout());
		p5.add(t2);
			
		p6.setLayout(new BorderLayout());
		p6.add("North",p4);
		p6.add("Center",p5);
	
		
		p7.setLayout(new FlowLayout());
		p7.add(read);
		p7.add(write);
		p7.add(exit);
		
		p8.setLayout(new BorderLayout());
		p8.add("North",p3);
		p8.add("Center",p6);
		p8.add("South",p7);
		add(p8);
		read.addActionListener(this);
		write.addActionListener(this);
		exit.addActionListener(this);
		setBounds(250,200,300,300);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==read){
			
			try{read();}
			catch(IOException ioe) {}
			}
		if(e.getSource()==write){
			try{write();}
			catch(IOException ioe) {}
			}
		if(e.getSource()==exit)
			System.exit(0);
		}
		
	public void read() throws IOException{
		str_text=t2.getText();
		File f=new File(str_text);
		if(!f.exists()){JOptionPane.showMessageDialog(File_RW.this,"文件"+str_text+"不存在!!");}
		FileReader in=new FileReader(str_text);
		BufferedReader bin=new BufferedReader(in);
		String str;
			while((str=bin.readLine())!=null){
				t1.append(str+"\r\n");	
			}
			in.close();
			JOptionPane.showMessageDialog(File_RW.this,"文件读取成功!!");
		}
		
		
	public void write()throws IOException{
		str_text=t2.getText();
		FileOutputStream fout=new FileOutputStream(str_text,true);
		DataOutputStream dout=new DataOutputStream(fout);
		String str,str_1;
		str=t1.getText();
		str_1=turn(str);
		
		int m=str_1.length();
		t1.append("\r\n写入了"+String.valueOf(m)+"字符\n");
		
		dout.writeBytes(str_1);
		dout.close();
		JOptionPane.showMessageDialog(File_RW.this,"文件已成功写入"+str_text+"中!!");	
		}
		
	public String turn(String str){
		while(str.indexOf("\n")!=-1){
			str=str.substring(0,str.indexOf("\n"))+"<br>"+str.substring(str.indexOf("\n")+1);
		}
		while(str.indexOf(" ")!=-1){
			str=str.substring(0,str.indexOf(" "))+"&nbsp;"+str.substring(str.indexOf(" ")+1);
		}
		return str;
	}
			
public static void main(String arg[]){
		File_RW t=new File_RW();
		}
		
}	
		
		

⌨️ 快捷键说明

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