Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何读取文件的内容?_Java_File_File Io - Fatal编程技术网

Java 如何读取文件的内容?

Java 如何读取文件的内容?,java,file,file-io,Java,File,File Io,我有一个文本编辑器,我将使用它来编辑HTML。我想能够创建某种文件打开器,它将继续并“打印”文本到文本区域。我不确定我将如何继续这样做。这是我的密码: package main; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileWriter; import j

我有一个文本编辑器,我将使用它来编辑HTML。我想能够创建某种文件打开器,它将继续并“打印”文本到文本区域。我不确定我将如何继续这样做。这是我的密码:

package main;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class TextEditor {
static boolean saved = false;
static final JTextArea textArea = new JTextArea(20,50);
static final JFileChooser chooser = new JFileChooser("save");
static final JFileChooser openChooser = new JFileChooser("Open");
public static void start(){
    JFrame f = new JFrame("Text editor");
    JPanel savePanel = new JPanel();
    JButton sb = new JButton("Save");
    JButton qsb = new JButton("Quick save");
    JButton ob = new JButton("Open");

    sb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            save();
        }
    });

    qsb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            quickSave();
        }
    });

    ob.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            open();
        }
    });

    savePanel.add(sb);
    savePanel.add(qsb);

    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setLayout(new BorderLayout(2,1));
    f.add(savePanel,BorderLayout.PAGE_START);
    f.add(textArea);
    f.pack();
    f.setVisible(true);
}

public static void save(){
    File dir;
    chooser.showDialog(null, "Save");
    dir = chooser.getSelectedFile();
    System.out.println(dir);
    try {
        FileWriter fw = new FileWriter(dir);
        String text = textArea.getText().replace("\n", System.getProperty("line.separator").toString());
        fw.write(text);
        fw.close();
        saved = true;
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, e);
}
}

public static void quickSave(){
if (saved == false){
    JOptionPane.showMessageDialog(null, "You must save at least once to use this function");
}
else{
    File dir = chooser.getSelectedFile();
    String text = textArea.getText().replace("\n", System.getProperty("line.separator").toString());
    try{
    FileWriter fw = new FileWriter(dir);
    fw.write(text);
    fw.close();
    }catch(IOException e){
        JOptionPane.showMessageDialog(null, e);
        }
    }
}

public static void open(){
openChooser.showDialog(null, "Oopen");
File dir = openChooser.getSelectedFile();
//want to open file which is dir.
}
}
简单明了:

textArea.read(new FileReader(dir), null);
这就是它的全部内容。

又好又简单:

textArea.read(new FileReader(dir), null);

这就是它的全部内容。

在询问之前,您是否尝试过使用谷歌搜索?我能找到这个:,。你问之前试过谷歌吗?我能找到这个:。