急急急:求JAVA修改TXT文件代码(最好带说明)
txt文件中的内容:1+1=21+2=31+3=4现在用java语句读取到了txt文件请问在java中修改txt中的内容代码应该怎么写呢(本人新手,知道应该很基础,希望明...
txt文件中的内容: 1+1=2
1+2=3
1+3=4
现在用java语句读取到了txt文件 请问在java中修改txt中的内容代码应该怎么写呢(本人新手,知道应该很基础,希望明白的朋友帮帮忙~~~~~~~~~~) 展开
1+2=3
1+3=4
现在用java语句读取到了txt文件 请问在java中修改txt中的内容代码应该怎么写呢(本人新手,知道应该很基础,希望明白的朋友帮帮忙~~~~~~~~~~) 展开
5个回答
展开全部
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
@SuppressWarnings("unused")
public class MyFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = -7619482402245033409L;
private JButton btOpen, btExit;
private JTextArea jta = new JTextArea();
private JPanel pn;
private JFileChooser jFileChooser = new JFileChooser();
private JMenuItem cnew;
public MyFrame() {
this.setTitle("文本文件阅读器");
this.setSize(500, 500);
btOpen = new JButton("打开");
btExit = new JButton("退出");
btOpen.addActionListener(this);
btExit.addActionListener(this);
pn = new JPanel(new FlowLayout());
pn.add(btOpen);
pn.add(btExit);
this.getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);
this.getContentPane().add(pn, BorderLayout.SOUTH);
jFileChooser.setCurrentDirectory(new File("."));
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("文件");
cnew = new JMenuItem("新建");
cnew.addActionListener(this);
file.add(cnew);
mb.add(file);
this.setJMenuBar(mb);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int a = JOptionPane.showConfirmDialog(null, "需要保存修改吗?", "温馨提示", JOptionPane.YES_NO_OPTION);
if (a != 0) {
System.exit(0); // 关闭
} else {
save();
}
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btOpen) {
open();
} else if (e.getSource() == btExit) {
save();
System.exit(0);
} else if (e.getSource() == cnew) {
this.file = null;
this.jta.setText("");
}
}
private void setFont(int num) {
this.jta.setFont(new Font("distraction", Font.BOLD, num * 10));
}
File file = null;
private void open() {// 打开文件
if (jFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
file = jFileChooser.getSelectedFile();
try {
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
byte[] b = new byte[in.available()];
in.read(b, 0, b.length);
jta.setText(new String(b, 0, b.length));
in.close();
} catch (IOException ex) {
jta.setText("打开文件错误" + file.getName());
}
}
}
private void save() {// 保存文件
if (file != null) {// Open File and Modify
} else {// Create New File
if (jFileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
file = jFileChooser.getSelectedFile();
}
}
// Save the file you have opened
try {
FileOutputStream fin = new FileOutputStream(file);
BufferedOutputStream in = new BufferedOutputStream(fin);
byte[] b = jta.getText().getBytes();
in.write(b, 0, b.length);
jta.setText(new String(b, 0, b.length));
in.close();
} catch (IOException ex) {
jta.setText("保存文件错误" + file.getName());
}
}
public static void main(String[] args) {
MyFrame ydq = new MyFrame();
}
}
//这是一个界面文件修改,你可以参考参考里面文件的处理方式
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
@SuppressWarnings("unused")
public class MyFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = -7619482402245033409L;
private JButton btOpen, btExit;
private JTextArea jta = new JTextArea();
private JPanel pn;
private JFileChooser jFileChooser = new JFileChooser();
private JMenuItem cnew;
public MyFrame() {
this.setTitle("文本文件阅读器");
this.setSize(500, 500);
btOpen = new JButton("打开");
btExit = new JButton("退出");
btOpen.addActionListener(this);
btExit.addActionListener(this);
pn = new JPanel(new FlowLayout());
pn.add(btOpen);
pn.add(btExit);
this.getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);
this.getContentPane().add(pn, BorderLayout.SOUTH);
jFileChooser.setCurrentDirectory(new File("."));
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("文件");
cnew = new JMenuItem("新建");
cnew.addActionListener(this);
file.add(cnew);
mb.add(file);
this.setJMenuBar(mb);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int a = JOptionPane.showConfirmDialog(null, "需要保存修改吗?", "温馨提示", JOptionPane.YES_NO_OPTION);
if (a != 0) {
System.exit(0); // 关闭
} else {
save();
}
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btOpen) {
open();
} else if (e.getSource() == btExit) {
save();
System.exit(0);
} else if (e.getSource() == cnew) {
this.file = null;
this.jta.setText("");
}
}
private void setFont(int num) {
this.jta.setFont(new Font("distraction", Font.BOLD, num * 10));
}
File file = null;
private void open() {// 打开文件
if (jFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
file = jFileChooser.getSelectedFile();
try {
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
byte[] b = new byte[in.available()];
in.read(b, 0, b.length);
jta.setText(new String(b, 0, b.length));
in.close();
} catch (IOException ex) {
jta.setText("打开文件错误" + file.getName());
}
}
}
private void save() {// 保存文件
if (file != null) {// Open File and Modify
} else {// Create New File
if (jFileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
file = jFileChooser.getSelectedFile();
}
}
// Save the file you have opened
try {
FileOutputStream fin = new FileOutputStream(file);
BufferedOutputStream in = new BufferedOutputStream(fin);
byte[] b = jta.getText().getBytes();
in.write(b, 0, b.length);
jta.setText(new String(b, 0, b.length));
in.close();
} catch (IOException ex) {
jta.setText("保存文件错误" + file.getName());
}
}
public static void main(String[] args) {
MyFrame ydq = new MyFrame();
}
}
//这是一个界面文件修改,你可以参考参考里面文件的处理方式
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已经读取到了
用 replace 方法 试试
replace (a ,b )
a是 你读到里面需要 替换的
b是 你想替换的东西
然后再写 回去
用 replace 方法 试试
replace (a ,b )
a是 你读到里面需要 替换的
b是 你想替换的东西
然后再写 回去
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
以下代码可以参考:
try{
String str="//你要写的字符串";
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("//你txt文件的绝对路径"))));
out.write(str, 0, str.length());
}catch(IOException ioe){
ioe.printStackTrace();
}
try{
String str="//你要写的字符串";
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("//你txt文件的绝对路径"))));
out.write(str, 0, str.length());
}catch(IOException ioe){
ioe.printStackTrace();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java.io
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你好哦
你现在估计已经取到了txt文件的值并且可以输出来了吧
修改你现在有的值
我写了一份粗糙的给你 应该可以看懂吧
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class reading {
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = "";
String bufferstr="";
int line = 1;
while ((tempString = reader.readLine()) != null) {
System.out.println(line+":"+tempString);
line++;
// reader.w
// writer w=new writer();
try {
tempString.getBytes("utf-8");
//在这操作你读出来的东西
bufferstr+=tempString+"嗨\n";
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
reader.close();
BufferedWriter writer=new BufferedWriter(new FileWriter(file));
writer.write(bufferstr);
writer.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
//1927
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "D:\\a.txt";
readFileByLines(fileName);
}
}
你现在估计已经取到了txt文件的值并且可以输出来了吧
修改你现在有的值
我写了一份粗糙的给你 应该可以看懂吧
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class reading {
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = "";
String bufferstr="";
int line = 1;
while ((tempString = reader.readLine()) != null) {
System.out.println(line+":"+tempString);
line++;
// reader.w
// writer w=new writer();
try {
tempString.getBytes("utf-8");
//在这操作你读出来的东西
bufferstr+=tempString+"嗨\n";
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
reader.close();
BufferedWriter writer=new BufferedWriter(new FileWriter(file));
writer.write(bufferstr);
writer.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
//1927
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "D:\\a.txt";
readFileByLines(fileName);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询