尝试从文本区写入文件,并从文件写入文本区

huangapple go评论117阅读模式
英文:

Trying to write to file from text area and write to text area from file

问题

class Window {
    file = new File("/Users/samuelballard/eclipse-workspace/cse_1322/ZModule_6/src/lab12/WriteFile.java");
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public File getFile() {
    return file;
}

public void setEmpty() {
    textArea = new JTextArea("Empty");
}

public void setJTextArea() {
    textArea = new JTextArea("");
    try {
        reader = new FileReader("/Users/samuelballard/eclipse-workspace/cse_1322/ZModule_6/src/lab12/WriteFile.java");
        textArea.read(reader, "File");
        reader.close();
    } catch(IOException i) {
        i.printStackTrace();
    }
}

public void saveJTextArea() {
    try {
        writer = new FileWriter("/Users/samuelballard/eclipse-workspace/cse_1322/ZModule_6/src/lab12/WriteFile.java");
        textArea.write(writer);
        writer.close();
    } catch (IOException i) {
        i.printStackTrace();
    }
}
}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

final class EventHandler extends Window {
    EventHandler() {
        put();
        get();
    }

    public void put() {
        put.addActionListener(new ActionListener() {
            // save text area to file
            public void actionPerformed(ActionEvent e) {
                saveJTextArea();
            }

        });
    }

    public void get() {
        get.addActionListener(new ActionListener() {
            // Display Contents of file
            public void actionPerformed(ActionEvent e) {
                setJTextArea();
            }

        });
    }
}
英文:

I can successfully write from text area to file, but am having trouble writing from file to text area. The problem seems to lie in Window.setJTextArea(). I have tried using different types of I/O options but am at an impasse. I'm out of my depth here, please offer guidance. There's more body text in class Window but it wasnt passing this site's review stage so I deleted it.

class Window {
file = new File("/Users/samuelballard/eclipse-workspace/cse_1322/ZModule_6/src/lab12/WriteFile.java");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
public File getFile() {
return file;
}
public void setEmpty() {
textArea = new JTextArea("Empty");
}
public void setJTextArea() {
textArea = new JTextArea("");
try {
reader = new FileReader("/Users/samuelballard/eclipse-workspace/cse_1322/ZModule_6/src/lab12/WriteFile.java");
textArea.read(reader, "File");
reader.close();
} catch(IOException i) {
i.printStackTrace();
}
}
public void saveJTextArea() {
try {
writer = new FileWriter("/Users/samuelballard/eclipse-workspace/cse_1322/ZModule_6/src/lab12/WriteFile.java");
textArea.write(writer);
writer.close();
} catch (IOException i) {
i.printStackTrace();
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
final class EventHandler extends Window {
EventHandler() {
put();
get();
}
public void put() {
put.addActionListener(new ActionListener() {
// save text area to file
public void actionPerformed(ActionEvent e) {
saveJTextArea();
}
});
}
public void get() {
get.addActionListener(new ActionListener() {
// Display Contents of file
public void actionPerformed(ActionEvent e) {
setJTextArea();
}
});
}
}

答案1

得分: 2

> 但是在将文件写入文本区域时遇到了问题。问题似乎出现在 Window.setJTextArea() 函数中。

你的方法是正确的。你应该使用 JTextArea 的 read(…) 方法。

然而,问题在于你创建了一个新的 JTextArea,但是你从未将这个文本区域添加到窗口中。所以文本实际上存储在内存中,但并未显示在窗口上。

textArea = new JTextArea("");

只需删除上述语句:

//textArea = new JTextArea("");

read(…) 方法会在加载文件之前清空文本区域中的文本。

参考:https://stackoverflow.com/questions/5880169/loading-a-text-file-into-a-textarea/5887380#5887380,这里有一个简单的可工作示例。

英文:

> but am having trouble writing from file to text area. The problem seems to lie in Window.setJTextArea().

Your approach is correct. You should use the read(…) method of the JTextArea.

However, the problem is that you create a new JTextArea and you never add the text area to the frame. So the text is just sitting in memory but is not displayed on the frame.

textArea = new JTextArea("");

Just delete the above statement:

//textArea = new JTextArea("");

The read(…) method will clear the text in the text area before loading the file.

See: https://stackoverflow.com/questions/5880169/loading-a-text-file-into-a-textarea/5887380#5887380 for a simple working example.

huangapple
  • 本文由 发表于 2020年4月11日 05:06:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61148674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定