File 在GWT项目中写入上的文本文件?
在我的GWT项目(它是一个游戏)中,我想将玩它的用户的分数存储到服务器端的一个文件中。并使用字符串在输出中显示它们 我可以从文件中读取数据,但我不能写入文件,它总是说谷歌应用程序引擎不支持这一点。 我想知道为什么谷歌应用引擎不支持它? 是否有任何方法可以将数据添加到服务器端的文件中?File 在GWT项目中写入上的文本文件?,file,google-app-engine,gwt,fwrite,java-io,File,Google App Engine,Gwt,Fwrite,Java Io,在我的GWT项目(它是一个游戏)中,我想将玩它的用户的分数存储到服务器端的一个文件中。并使用字符串在输出中显示它们 我可以从文件中读取数据,但我不能写入文件,它总是说谷歌应用程序引擎不支持这一点。 我想知道为什么谷歌应用引擎不支持它? 是否有任何方法可以将数据添加到服务器端的文件中? 请随意添加您的所有意见,我们将不胜感激 你不能在appengine上写入文件,但你还有两个选择 首先,如果文本小于1MB,则可以使用将文本存储在数据存储中 其次,您可以将文本存储在一个文件中。GWT项目中不能使用用
请随意添加您的所有意见,我们将不胜感激 你不能在appengine上写入
文件
,但你还有两个选择
首先,如果文本小于1MB,则可以使用将文本存储在数据存储中
其次,您可以将文本存储在一个文件中。GWT项目中不能使用用于编写文本文件的代码或依赖jar文件,但可以使用用于执行cmd命令的代码 用这样的技巧来回避这个问题。不加载并添加到生成路径。添加以下可在线复制到CMDUtils.java并放入“共享”包的代码段:
public static StringBuilder execute(String... commands) {
StringBuilder result = new StringBuilder();
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(new String[] { "cmd" });
// put a BufferedReader
InputStream inputstream = proc.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputstream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
PrintWriter stdin = new PrintWriter(proc.getOutputStream());
for (String command : commands) {
stdin.println(command);
}
stdin.close();
// MUST read the output even though we don't want to print it,
// else waitFor() may fail.
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
result.append('\n');
}
} catch (IOException e) {
System.err.println(e);
}
return result;
}
添加相应的ABCService.java和ABCServiceAsync.java,然后添加:
public class ABCServiceImpl extends RemoteServiceServlet implements ABCService {
public String sendText(String text) throws IllegalArgumentException {
text= Base64.encodeBase64String(text.getBytes());
final String command = "java -Dfile.encoding=UTF8 -jar \"D:\\abc.jar\" " + text;
CMDUtils.execute(command);
return "";
}
而abc.jar被创建为一个可执行的jar,其中入口点包含如下主方法:
public static final String TEXT_PATH = "D:\\texts-from-user.txt";
public static void main(String[] args) throws IOException {
String text = args[0];
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(TEXT_PATH, true));
text = new String(Base64.decodeBase64(text));
writer.write("\n" + text);
writer.close();
}
我已经尝试过了,它成功地用于GWT项目的文本文件编写 我确信我的文件不会超过1MB,它是一个10对(字符串名,整数分数)。你喜欢哪一个对我有用?请您提供一些样品,以满足您的需要:这两种选择都没有。可以将它们另存为10个单独的实体,或一个具有字符串属性的实体。您应该阅读如何在App Engine上存储数据:我更喜欢一个具有数组或列表字符串的实体。@all:如果我在Blobstore中存储一个文本文件,我可以写入该文本文件吗?或者我只能读取它?您可以检索此文件,更新它,然后再次存储。如果不更改blob键,它将重写旧版本。现在还有一个选项可以在谷歌云存储中存储文件,这比Blobstore便宜。