如何以相反的顺序获取输入流的内容? [英] How to get the content of an input stream in reverse order?

查看:31
本文介绍了如何以相反的顺序获取输入流的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 txt 文件进行关卡设计.我使用下面的内容获取内容并转换为字符串缓冲区,然后遍历这些行以生成我的游戏对象.

I am using a txt file for my level desing. I use the below to take the contents and convert to string buffer, then iterate through the lines to generate my game objects.

问题在于它是从上往下读取的,所以我必须颠倒设计我的关卡,以使它们正确旋转.

The the problem is that it reads from top down and so I have to design my levels upside down for them to be right way around.

如何更改流以相反的方式读取?或者以相反的方式将这些行写入 String Builder?

How can I change the stream to read the opposite way? Or write the lines to the String Builder the opposite way?

 private static String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append((line + "\n"));
            }
        } catch (IOException e) {
            Log.w("LOG", e.getMessage());
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                Log.w("LOG", e.getMessage());
            }
        }
        return sb.toString();

推荐答案

你可以使用 sb.insert(0, line + "\n") 而不是 sb.append(line + "\n");.

You could just use sb.insert(0, line + "\n") instead of sb.append(line + "\n");.

这将始终在字符串的前面添加新行,而不是将其附加到末尾.应该完全按照您的意愿行事并且同样快,因为 StringBuilder 正是为这样的事情而设计的.

This will always add new lines to the front of the string, not append it to the end. Should do exactly what you want and will be just as fast, because StringBuilder is made exactly for things like that.

这篇关于如何以相反的顺序获取输入流的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆