注:游戏存档不推荐此方法但可以使用,真想做游戏存档的话建议学习json
正文:
读取文件(文本文档)的方法:
用FileReader方法的对象中的read方法获取数据。
/**
* @outhor Gotrcer
* @param path 文件的位置
* @return 文件的数据
*/
public static String FileReader(String path) throws IOException {
String FileDateString;
File file = new File(path);
FileReader FR = new FileReader(file);//用File获得FileReader的实例化
char[] FileData = new char[(int) file.length()];//初始化数组(此数组是用来获取文件里的数据)
FR.read(FileData);//读取!
FileDateString = new String(FileData);//写入String
return FileDateString;
}
写入数据的方法:
用FileWriter的示例化写入数据(注:最后要使用.flush()方法)。
/**
* @outhor Gotrcer
* @param path 文件的路径
* @param content 写入的内容
* @param append 是否选择追加
*/
public static void FileWriter(String path,String content,boolean append) throws IOException {
FileWriter fw = new FileWriter(new File(path),append);
fw.write(content);
fw.flush();
fw.close();
}