libgdx文件句柄..读取一行 [英] Libgdx file handle.. reading a single line

查看:82
本文介绍了libgdx文件句柄..读取一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在libgdx上编码的项目上保存和加载文件.这意味着我不能使用缓冲的读取器,因为android不会读取它..并且我不能将项目移至android,因为它必须处于核心位置……日复一日或了解所有..现在我正在尝试文件处理哪个应该工作正常??但是我不能让它逐行读取..它把所有文本都放在字符串上..帮助plzz ..我的理解也是正确的,并且保存和加载比应该的还要复杂吗?这是代码..

  FileHandle句柄= Gdx.files.local("words.txt");字符串文本= handle.readString();words.add(文本); 

解决方案

有几种方法可以逐行读取此内容.当您使用LibGDX FileHandle API读取文件时,该文件包括字符串,字节数组并进入各种读取器;有几种读取数据的方法.我假设您在此文件中有某种形式的字典,列表中的单词用换行符分隔?在这种情况下,您可以使用现有的字符串并在新行终止符上进行拆分.

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

您实际上只需要担心两个换行符(UNIX和Windows).

FileHandle API

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);

解决方案

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

这篇关于libgdx文件句柄..读取一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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