问题描述
你好,我目前正在为一个大学模块的java文本编辑工作。我有一些麻烦获取字体样式和大小保存到一个丰富的文本文件我目前正在使用printwriter方法将文本区域中的文本写入文件。我在下面包含了我的saveFile代码。这是我第一次发布如此抱歉,如果我的帖子有什么问题,提前致谢。
public class saveFile implements ActionListener
$ b $覆盖
public void actionPerformed(ActionEvent e){
//允许用户输入文件名
String s = JOptionPane.showInputDialog(Input文件名称:);
String w =;
尝试
{
//允许用户进入目录
w = JOptionPane.showInputDialog(输入你想要的目录:\\\
+eg C:\\ C驱动器);
catch(Exception writeFile)
{
JOptionPane.showMessageDialog(null,Task can not be completed)\\\
请再试一次
File Write Error ,JOptionPane.ERROR_MESSAGE,null);
//尝试catch块
尝试
{
//检查字段为空或空
if(!(s.isEmpty() && w.isEmpty())&&(w!= null& s!= null))
{
try
{
//创建新文件并附加.rtf
文件userFile = new File(w + s +.rtf);
PrintWriter文件=新的PrintWriter(userFile);
//设置文件内容=文本
file.print(text.getText());
file.close();
文件dir = new File(w);
if(dir.exists())
{
JOptionPane.showMessageDialog(null,File+ s +created\\\
+Saved in+ w,null,JOptionPane .PLAIN_MESSAGE);
}
else
{
throw new Exception();
$ b catch(Exception fileNotCreatedException)
{
JOptionPane.showMessageDialog(null,File not created\\\
+请检查目录是否存在,文件错误,
JOptionPane.ERROR_MESSAGE,null);
fileNotCreatedException.printStackTrace();
$ b 解决如果您正在使用内容类型 text / rtf
处理JTextPane,则RTFEditorKit用于StyledDocument。 OutputStream out = new FileOutputStream(userFile);
StyledDocument doc = textPane.getStyledDocument();
StyledEditorKit editorKit = textPane.getStyledEditorKit();
editorKit.write(out,doc,0,doc.getLength());由于HTMLEditorKit也是基于StyledDocument的,所以有时候可能首先尝试使用HTML来付费,就像HTML一样语法更简单。
Hello I am currently working on a java text editor for a module in college. I am having some trouble getting the font styles and sizes to save to a rich text file I am currently using the printwriter method to write the text in the text area to the file. I have included my saveFile code below. This my first time posting so sorry if there's anything wrong with my post, thanks in advance.
public class saveFile implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e){
//allow user to enter file name
String s = JOptionPane.showInputDialog("Input name of file:");
String w = "";
try
{
//Allow user to enter directory
w = JOptionPane.showInputDialog("Input Directory where you want the file:\n"
+ "eg C:\\ for C drive");
}
catch(Exception writeFile)
{
JOptionPane.showMessageDialog(null, "Task could not be completed\nPlease try again"
,"File Write Error", JOptionPane.ERROR_MESSAGE, null);
}
//try catch block
try
{
//check if fields are null or empty
if(!(s.isEmpty() && w.isEmpty()) && (w != null && s != null))
{
try
{
//Create new file and append with .rtf
File userFile = new File(w + s + ".rtf");
PrintWriter file = new PrintWriter(userFile);
//set files content = text
file.print(text.getText());
file.close();
File dir = new File(w);
if(dir.exists())
{
JOptionPane.showMessageDialog(null, "File " + s + " created\n" + "Saved in " + w, null,JOptionPane.PLAIN_MESSAGE );
}
else
{
throw new Exception();
}
}
catch(Exception fileNotCreatedException)
{
JOptionPane.showMessageDialog(null,"File not created\n"
+ "Please check to see directory exists","File Error",
JOptionPane.ERROR_MESSAGE, null);
fileNotCreatedException.printStackTrace();
}
}
}
解决方案 If you are working in swing with a JTextPane using content type text/rtf
, then the RTFEditorKit is used for the StyledDocument.
OutputStream out = new FileOutputStream(userFile);
StyledDocument doc = textPane.getStyledDocument();
StyledEditorKit editorKit = textPane.getStyledEditorKit();
editorKit.write(out, doc, 0, doc.getLength());
As the HTMLEditorKit also is StyledDocument based, it might sometimes pay to first experiment with HTML, as the HTML syntax is easier.
这篇关于如何在java中保存字体样式和字体大小到丰富的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-02 15:50