在我的GWT项目中(它是一个游戏)我想将播放它的用户的分数存储在位于服务器端的文件中。并使用String。在输出中显示它们。
我可以从文件中读取数据,但我无法写入文件,它总是说Google App Engine不支持此功能。 我想知道为什么Google App Engine不支持它? 有什么办法可以将数据添加到服务器端的文件中吗? 请随意添加您的所有意见,每件事都将受到赞赏。
答案 0 :(得分:3)
您无法在App Engine上写入file
,但还有其他两个选项。
首先,如果您的文字小于1MB,则可以使用Text entity将文本存储在数据存储中。
其次,您可以将文字存储在Blobstore。
中答案 1 :(得分:0)
可以在GWT项目中使用用于文本文件写入的代码或从属jar文件,但是用于执行cmd命令的代码可以。
使用这样的技巧来规避问题。下载commons-codec-1.10并添加到构建路径。添加以下代码段,可以在线复制到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,其中入口点包含一个这样的main方法:
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项目编写文本文件。