如何在java中保存应用程序内编辑的txt文件

时间:2014-10-31 11:57:57

标签: java file save

我一直坚持如何在文本编辑器中保存文件我正在创建。如果你可以帮助我,那就是我的代码

import java.awt.*;

import java.awt.event.*;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;


import javax.swing.*;

import javax.swing.border.*;


public class test1 {

    public static void main ( String[] args )
    {
        JButton b1 = new JButton("Press to read a file");
        JPanel middlePanel = new JPanel ();
        middlePanel.setBorder ( new TitledBorder ( new EtchedBorder (), "Text Reading Box" ) );

        // create the middle panel components

        final JTextArea display = new JTextArea ( 16, 58 );
        display.setEditable ( false);
        JScrollPane scroll = new JScrollPane ( display );
        scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );

        //Add Text area in to middle panel
        middlePanel.add ( scroll );

        // My code
        JFrame frame = new JFrame ("Text Reader 0.4 Beta"); 
        frame.add ( middlePanel );
        frame.add(b1, BorderLayout.NORTH);
        frame.pack ();
        frame.setLocationRelativeTo ( null );
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible ( true );
        frame.setLayout(new BorderLayout());
        b1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                chooser.showOpenDialog(null);
                File f = chooser.getSelectedFile();
                String filename = f.getAbsolutePath();

                try {

                    FileReader reader = new FileReader(filename);
                    BufferedReader br = new BufferedReader(reader);
                    display.read(br, null);
                    br.close();
                    display.requestFocus();

                }

                catch(Exception error) {
                    System.err.println("Could'nt read a file");
                }
            }

          });

    }
}

2 个答案:

答案 0 :(得分:0)

我猜您应该有另一个按钮说'保存',以便将已编辑的内容保存回文件。为此,您必须处理保存按钮的操作并将内容写回文件。

答案 1 :(得分:0)

package com.test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class Random
{
    public static void main(String[] args)
    {
        JButton b1 = new JButton("Press to read a file");
        JButton b2 = new JButton("Save");
        JPanel middlePanel = new JPanel();
        final StringBuilder filename = new StringBuilder("");
        middlePanel.setBorder(new TitledBorder(new EtchedBorder(), "Text Reading Box"));

        // create the middle panel components

        final JTextArea display = new JTextArea(16, 58);
        display.setEditable(true);
        JScrollPane scroll = new JScrollPane(display);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        // Add Text area in to middle panel
        middlePanel.add(scroll);

        // My code
        JFrame frame = new JFrame("Text Reader 0.4 Beta");
        frame.add(middlePanel);
        frame.add(b1, BorderLayout.NORTH);
        frame.add(b2, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setLayout(new BorderLayout());
        b1.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                JFileChooser chooser = new JFileChooser();
                chooser.showOpenDialog(null);
                File f = chooser.getSelectedFile();
                filename.append(f.getAbsolutePath());

                try
                {

                    FileReader reader = new FileReader(filename.toString());
                    BufferedReader br = new BufferedReader(reader);
                    display.read(br, null);
                    br.close();
                    display.requestFocus();

                }

                catch (Exception error)
                {
                    System.err.println("Could'nt read a file");
                }
            }

        });
        b2.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                String textToSet = display.getText();
                try
                {
                    PrintWriter writer = new PrintWriter(filename.toString(), "UTF-8");
                    writer.write(textToSet);
                    writer.close();
                }
                catch (FileNotFoundException | UnsupportedEncodingException exc)
                {
                    System.err.println("Could'nt write to the file");
                }

            }
        });
    }
}