700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java 富文本框内容 保存与恢复 将富文本框内容直接保存到文件中。(不使用savefile

java 富文本框内容 保存与恢复 将富文本框内容直接保存到文件中。(不使用savefile

时间:2021-05-18 06:58:50

相关推荐

java 富文本框内容 保存与恢复 将富文本框内容直接保存到文件中。(不使用savefile

how can I save the contents of a rich text box without needing to open the save file dialog.

i think its something like:

richTextBox1.SaveFile(@"\Documents\save_file_here.rtf");

but it cant find the file to overwrite in the first place.

解决方案

The shortest way is probably:

File.WriteAllText(@"\Documents\save_file_here.rtf", richTextBox1.Text)

Not sure if that will save formatting though.

//create a file first

FileStream outStream = File.Create("Path \\filename");

//Create a variable

string data = richTextBox.Text;

//Save

StreamWriter sw = new StreamWriter(outStream);

sw.WriteLine(data);

sw.Flush();

sw.Close();

You have a problem with separation of concerns. Dialog boxes have nothing to do with file read/write operations. They only work with file system and allows to determine a file name to work with, nothing else. For read/write operations, it's absolutely irrelevant where the file name came from.

Also, there absolutely no situations where a file named hard-coded in the program could be useful, unless you run programs from the Visual Studio and modify them all the time. If you try to deploy executable, it will be defunct due to hard-coding problems. File names (even constant) are always calculated during run-time.

—SA

java 富文本框内容 保存与恢复 将富文本框内容直接保存到文件中。(不使用savefile对话框)...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。