3

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.

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();
Till Helge
  • 9,253
  • 2
  • 40
  • 56
Rhys
  • 2,807
  • 8
  • 46
  • 68

1 Answers1

7

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

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.

Till Helge
  • 9,253
  • 2
  • 40
  • 56