javalibgdxsaveloadfilehandle

Libgdx file handle.. reading a single line


I am trying to save and load files on a project that is coded on libgdx. Which means that i cant use a buffered reader because android wont read it.. and i cant move the project to android because it has to be in the core... after days and days or understanding all.. now i am trying File handing which should work right?? but i cant get it to read line by line.. it puts all the text in on string.. Help plzz.. also is my understanding correct and saving and loading is waaaay more complicated than it should be?? here is the code..

  FileHandle handle = Gdx.files.local("words.txt");
  String text = handle.readString();
  words.add(text);

Solution

  • There are several ways to read this line by line. When your reading a file in using the LibGDX FileHandle API which include strings, byte arrays and into various readers; there are several ways to read the data in. I am assuming you have some form of dictionary in this file, with the words in a list separated by newlines? If this is the case you can take your existing string and split on the new line terminator.

      FileHandle handle = Gdx.files.local("words.txt");
      String text = handle.readString();
      String wordsArray[] = text.split("\\r?\\n");
      for(String word : wordsArray) {
          words.add(word);
      }
    

    There's only really two newlines (UNIX and Windows) that you need to worry about.

    FileHandle API