java 如何写入txt
如图的界面已经编好了,需要将页面中输入的结果通过点击save按钮输入一个路径为“C:\Users\guchao\Desktop\java.txt”的txt文件,请问该如何...
如图的界面已经编好了,需要将页面中输入的结果通过点击save按钮输入一个路径为“C:\Users\guchao\Desktop\java.txt”的txt文件,请问该如何编?急求,30分钟内解答,谢谢!
展开
3个回答
展开全部
这不简单? 点击按钮,触发的事件中,把值写入到文件就可以了
其实在网上搜索一下,java写入文件即可
其实在网上搜索一下,java写入文件即可
追问
能帮忙把具体程序写出来么?写出来的话我加悬赏
追答
http://hi.baidu.com/csisno1/item/4b7e523a07327e49033edc45
上面可以参考,现在没时间帮你写这个程序,只能帮到这里了
展开全部
Writer writer = new FileWriter("文件路径", true);
writer.append("111111\r\n");
writer.append("222222\r\n");
writer.flush();
writer.close();
writer.append("111111\r\n");
writer.append("222222\r\n");
writer.flush();
writer.close();
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这是一个打开文件,编辑文件,以及保存文件的简单例子。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TestTest extends JFrame implements ActionListener{
JPanel panel;
JTextArea text;
JScrollPane textScroll;
JButton open;
JButton save;
JFileChooser fileChooser = new JFileChooser("c:" + File.separator);
public TestTest(){
this.setTitle("TestChange");
this.setSize(800, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
open = new JButton("open");
save = new JButton("save");
text = new JTextArea();
textScroll = new JScrollPane(text);
open.addActionListener(this);
save.addActionListener(this);
panel.add(open);
panel.add(save);
this.add(panel, BorderLayout.NORTH);
this.add(textScroll, BorderLayout.CENTER);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] str){
new TestTest();
}
@Override
public void actionPerformed(ActionEvent e) {
File getFile;
int chooser;
if(e.getSource() == open)
{
chooser = fileChooser.showOpenDialog(this);
if(chooser == JFileChooser.APPROVE_OPTION)
{
getFile = fileChooser.getSelectedFile();
FileInputStream fileInput = null;
try {
fileInput = new FileInputStream(getFile);
} catch (FileNotFoundException e1) {
System.out.println(000);
}
int count = 0;
try {
count = fileInput.available();
if(count > 0)
{
byte[] by = new byte[count];
try {
fileInput.read(by);
} catch (IOException e1) {
System.out.println(333);
}
text.append(new String(by));
}
} catch (IOException e1) {
System.out.println(111);
}finally{
if(fileInput != null)
try {
fileInput.close();
} catch (IOException e1) {
System.out.println(666);
}
}
}
}else
if(e.getSource() == save)
{
chooser = fileChooser.showSaveDialog(this);
FileOutputStream fileOut = null;
if(chooser == JFileChooser.APPROVE_OPTION)
{
File saveFile = fileChooser.getSelectedFile();
if(saveFile.exists())
{
JOptionPane.showMessageDialog(this, "保存失败!");
}else{
try {
if(saveFile.createNewFile())
{
fileOut = new FileOutputStream(saveFile);
byte[] outby = text.getText().getBytes();
fileOut.write(outby);
}
} catch (IOException e1) {
System.out.println(444);
}finally{
if(fileOut != null)
try {
fileOut.close();
} catch (IOException e1) {
System.out.println(555);
}
}
}
}
}
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TestTest extends JFrame implements ActionListener{
JPanel panel;
JTextArea text;
JScrollPane textScroll;
JButton open;
JButton save;
JFileChooser fileChooser = new JFileChooser("c:" + File.separator);
public TestTest(){
this.setTitle("TestChange");
this.setSize(800, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
open = new JButton("open");
save = new JButton("save");
text = new JTextArea();
textScroll = new JScrollPane(text);
open.addActionListener(this);
save.addActionListener(this);
panel.add(open);
panel.add(save);
this.add(panel, BorderLayout.NORTH);
this.add(textScroll, BorderLayout.CENTER);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] str){
new TestTest();
}
@Override
public void actionPerformed(ActionEvent e) {
File getFile;
int chooser;
if(e.getSource() == open)
{
chooser = fileChooser.showOpenDialog(this);
if(chooser == JFileChooser.APPROVE_OPTION)
{
getFile = fileChooser.getSelectedFile();
FileInputStream fileInput = null;
try {
fileInput = new FileInputStream(getFile);
} catch (FileNotFoundException e1) {
System.out.println(000);
}
int count = 0;
try {
count = fileInput.available();
if(count > 0)
{
byte[] by = new byte[count];
try {
fileInput.read(by);
} catch (IOException e1) {
System.out.println(333);
}
text.append(new String(by));
}
} catch (IOException e1) {
System.out.println(111);
}finally{
if(fileInput != null)
try {
fileInput.close();
} catch (IOException e1) {
System.out.println(666);
}
}
}
}else
if(e.getSource() == save)
{
chooser = fileChooser.showSaveDialog(this);
FileOutputStream fileOut = null;
if(chooser == JFileChooser.APPROVE_OPTION)
{
File saveFile = fileChooser.getSelectedFile();
if(saveFile.exists())
{
JOptionPane.showMessageDialog(this, "保存失败!");
}else{
try {
if(saveFile.createNewFile())
{
fileOut = new FileOutputStream(saveFile);
byte[] outby = text.getText().getBytes();
fileOut.write(outby);
}
} catch (IOException e1) {
System.out.println(444);
}finally{
if(fileOut != null)
try {
fileOut.close();
} catch (IOException e1) {
System.out.println(555);
}
}
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询