Java如何读取文本区中的内容并写入文件里?

2025-03-10 18:49:34
推荐回答(3个)
回答1:

import javax.swing.*;
import java.io.*;
public class GUIRWFile extends JFrame implements ActionListener {
JTextArea txtMessage;
JButton btnRead, btnWrite;
public GUIRWFile() {
super("文本读写");
txtMessage = new JTextArea(15, 30);
btnRead = new JButton("读取");
btnWrite = new JButton("写入");
this.setLayout(new FlowLayout());
this.add(new JLabel("文件内容:"));
this.add(txtMessage);
this.add(btnRead);
this.add(btnWrite);
btnRead.addActionListener(this);
btnWrite.addActionListener(this);
this.setSize(400, 400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new GUIRWFile();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
String fileName = "D:\\test.txt";
if(btn.getText().equals("读取")){
try{
InputStream is = new FileInputStream(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer buffer = new StringBuffer();
while((line = br.readLine()) != null){
buffer.append(line + "\n");
}
txtMessage.setText(buffer.toString());
br.close();
is.close();
}
catch(Exception ex){
JOptionPane.showMessageDialog(this, "系统I/O错误!");
}
}
else if(btn.getText().equals("写入")){
try{
String msg = txtMessage.getText();
OutputStream os = new FileOutputStream(fileName);
PrintStream ps = new PrintStream(os);
ps.print(msg);
ps.close();
os.close();
}
catch(Exception ex){
JOptionPane.showMessageDialog(this, "系统I/O错误!");
}
}
}
}

回答2:

package com.kd.zt.action.qek;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
* @作者 QiErKang
* @创建日期 2013-10-30 下午08:36:41
* @项目名 crec
* @包名 com.kd.zt.action.qek
* @类名 DownloadAction.java
* @版本 V 1.0
*/
public class DownloadAction extends ActionSupport {

private String fileName;

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
//处理下载文件名为中文状态下的编码方式
try {
this.fileName=new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 该方法必须以get开头,get后的 首字母是小写 ,返回必须是流
* @return
*/
public InputStream getDownFile() throws Exception{
try {
// ServletActionContext.getResponse().setHeader("content-disposition", "attachment;fileName="+URLEncoder.encode(fileName, "UTF-8").toString());
return new FileInputStream(new File(ServletActionContext.getServletContext().getRealPath("/")+"filePacket/"+fileName));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 下载成功,必须定义一个空方法
* @return
*/
public String execute(){
return "ok";
}

}
上面是java文件

class="com.sunspoter.lib.web.struts2.dispatcher.StreamResultX">




downFile
attachment; filename=${fileName}


上面是action
希望楼主早日解决!我手打的啊

回答3:

如果想获得比如空格回车用java能够判断出来,写文件的时候可以写进去,如果说把字体什么的保存到文件里,如果是txt那真是不行,不知道富编辑框对你有没有用http://ueditor.baidu.com/website/download.html