【问题标题】:Writing to text file on in GWT project?在 GWT 项目中写入文本文件?
【发布时间】:2013-01-21 04:27:45
【问题描述】:

在我的 GWT 项目(它是一个游戏)中,我想将玩它的用户的分数存储到位于服务器端的文件中。并使用字符串将它们显示在输出中。

我可以从文件中读取数据,但我不能写入文件,它总是说 Google App Engine 不支持这个。 我想知道为什么 Google App Engine 不支持它? 有什么办法可以将数据添加到服务器端的文件中? 请随意添加您的所有意见,每件事将不胜感激。

【问题讨论】:

    标签: file google-app-engine gwt fwrite java-io


    【解决方案1】:

    您无法在 App Engine 上写入 file,但您有两个其他选项。

    首先,如果您的文本小于 1MB,您可以使用 Text entity 将文本存储在 Datastore 中。

    其次,您可以将文本存储在Blobstore

    【讨论】:

    • 我确信我的文件不会超过 1MB,它是 10 对 (String name, int score)。那么你更喜欢哪一个对我有用?您能否为此提供一些样本?
    • 满足您的需求:这些选项都不是。您可以将它们保存为 10 个单独的实体,或一个具有 String 属性的实体。您应该阅读如何在 App Engine 上存储数据:developers.google.com/appengine/docs/java/datastore/overview
    • 我更喜欢一个带有数组或列表字符串的实体。
    • @all:如果我在 Blobstore 中存储一个文本文件,我可以写入该文本文件吗?还是我只能读?
    • 您可以检索该文件、更新它并再次存储。如果您不更改 blob 密钥,它将重写旧版本。现在还有一个选项可以将文件存储在 Google Cloud Storage 中,这比 Blobstore 便宜。
    【解决方案2】:

    GWT项目中不能使用写文本文件的代码或依赖jar文件,但是执行cmd命令的代码可以。

    使用这样的技巧来规避问题。下载 commons-codec-1.10 并添加到构建路径。将以下可以在线复制的 sn-p 添加到 CMDUtils.java 并放入 'shared' 包中:

    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 项目的文本文件写入工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      相关资源
      最近更新 更多