Java 如何创建可再次读取的文本文件?
我上过Java的基础课,但我的知识很少。自从我越来越熟悉Java以来,我在过去一两个月里创建了一个基于文本的rpg。我想知道是否有任何方法可以让程序创建一个“保存”文件存储在某个文件夹中,并提示用户是否要打开保存的字符。我还没有学习Java的任何面向对象的部分。我可以做些什么来实现这一点 我想知道有没有办法让程序创建一个 “保存”要存储在某个文件夹中的文件,并提示用户 他们想打开保存的角色 编写和读取文本文件是一种为游戏保存状态的可靠新手方法 有很多方法可以解决这个问题,但这里有一个例子:Java 如何创建可再次读取的文本文件?,java,import,Java,Import,我上过Java的基础课,但我的知识很少。自从我越来越熟悉Java以来,我在过去一两个月里创建了一个基于文本的rpg。我想知道是否有任何方法可以让程序创建一个“保存”文件存储在某个文件夹中,并提示用户是否要打开保存的字符。我还没有学习Java的任何面向对象的部分。我可以做些什么来实现这一点 我想知道有没有办法让程序创建一个 “保存”要存储在某个文件夹中的文件,并提示用户 他们想打开保存的角色 编写和读取文本文件是一种为游戏保存状态的可靠新手方法 有很多方法可以解决这个问题,但这里有一个例子: Pr
PrintWriter out = new PrintWriter("filename.txt");//create the writer object
out.println(text);//write
out.close() //when you're done writing, this will save
现在,这里有一种读取文件的简单方法:
只要谷歌一步之遥,网上就有很多教程。|=^]
注意:这是我为保存文本而构建的类
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class Savetext {
/**
* Saves text to a <i>.txt</i> file with the specified name,
*
* @param name the file name without the extension
*
* @param txt the text to be written in the file
*
* @param message the message to be displayed when done saving, if <tt>null</tt> no
* message will be displayed
*/
public static void Save(String name, String txt, String message) {
name += ".txt";
gtxt(name, txt, message);
}
private static void gtxt(String name, String txt, String mess) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
File file = null;
fc.showSaveDialog(null);
file = fc.getSelectedFile();
String path = file.getPath();
path += "\\";
path += name;
File f1 = new File(path);
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(f1)));
wrtxt(out, txt, mess);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e.getMessage().toString());
}
}
private static void wrtxt(PrintWriter out, String txt, String mess) {
out.println(txt);
out.flush();
out.close();
if (mess != null)
JOptionPane.showMessageDialog(null, mess);
}
}
import java.io.*;
导入javax.swing.JFileChooser;
导入javax.swing.JOptionPane;
公共类保存文本{
/**
*将文本保存到具有指定名称的.txt文件中,
*
*@param name不带扩展名的文件名
*
*@param txt要写入文件的文本
*
*@param message保存完成时显示的消息,如果为空则为否
*将显示消息
*/
公共静态无效保存(字符串名称、字符串txt、字符串消息){
名称+=“.txt”;
gtxt(名称、文本、消息);
}
私有静态void gtxt(字符串名称、字符串txt、字符串mess){
JFileChooser fc=新的JFileChooser();
fc.setFileSelectionMode(仅限JFileChooser.DIRECTORIES_);
File=null;
fc.showsavedilog(空);
file=fc.getSelectedFile();
字符串路径=file.getPath();
路径+=“\\”;
路径+=名称;
文件f1=新文件(路径);
试一试{
PrintWriter out=新的PrintWriter(新的BufferedWriter(
新文件编写器(f1));
wrtxt(out、txt、mess);
}捕获(IOE异常){
showMessageDialog(null,例如getMessage().toString());
}
}
私有静态void wrtxt(打印输出、字符串txt、字符串混乱){
out.println(txt);
out.flush();
out.close();
if(mess!=null)
showMessageDialog(null,mess);
}
}
您应该调用static save()方法,而不创建类的实例,检查文件类、InputStreams、OutputStreams、FileReader和FileWriter。还有其他一些有用的读者和作者,他们将使阅读和编写文件变得更容易。开始阅读,你将发现如何做所有这些和更多,包括一些“面向对象的部分”。对于初学者来说,不加解释地给出OP代码块不是一个好方法……对,但这就是我目前的做法,@MohammadS.:他不仅仅是在没有解释的情况下发布代码,更糟糕的是,他发布了错误的代码,包括使用与创建PrintWriter不同的方法关闭PrintWriter,防止在循环中重复使用它,或在finally块中关闭它,并将GUI代码与文件I/O代码组合在同一块中,应该避免的事情。问题不仅与GUI或Swing编码无关。@Hovercraft Full Of EEL:将一个简单字符串保存为.txt文件难道不起作用吗?这是一个糟糕的代码,不应该作为新手的示例-1票否决。@Relytnever Java中的某些方法将抛出异常。您必须告诉代码要么让它被抛出,要么捕获它。有多种方法可以处理此错误。我建议在我拥有的
try
和finally
块之间放置一个catch
语句。这里有很多值得一读的东西:谢谢你的帮助。在这个游戏上的工作帮助了我很多话题。我只是将它改为使用父类和子类来清理代码;必须被抓住或宣布被抛出。你能给我解释一下出了什么问题吗@穆罕默德。
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class Savetext {
/**
* Saves text to a <i>.txt</i> file with the specified name,
*
* @param name the file name without the extension
*
* @param txt the text to be written in the file
*
* @param message the message to be displayed when done saving, if <tt>null</tt> no
* message will be displayed
*/
public static void Save(String name, String txt, String message) {
name += ".txt";
gtxt(name, txt, message);
}
private static void gtxt(String name, String txt, String mess) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
File file = null;
fc.showSaveDialog(null);
file = fc.getSelectedFile();
String path = file.getPath();
path += "\\";
path += name;
File f1 = new File(path);
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(f1)));
wrtxt(out, txt, mess);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e.getMessage().toString());
}
}
private static void wrtxt(PrintWriter out, String txt, String mess) {
out.println(txt);
out.flush();
out.close();
if (mess != null)
JOptionPane.showMessageDialog(null, mess);
}
}