【问题标题】:How to store values of a file in an array; [duplicate]如何将文件的值存储在数组中; [复制]
【发布时间】:2015-10-04 15:24:50
【问题描述】:

基本上我在 srcard 上有一个 .txt 文件,我想读取该特定文件并将该文件具有的所有值保存到一个字符串数组中。 我怎样才能成功?

P.S:文件的值除以空格。

【问题讨论】:

    标签: java android arrays


    【解决方案1】:

    如果你想用空格分割它,那么你可以在 readLine() 输出中使用 stringtokenizer 或 string split。 如果你想用某种模式分割它,那么你可以在 readLine() 输出中使用正则表达式。

    【讨论】:

      【解决方案2】:

      使用这个

      File dir = Environment.getExternalStorageDirectory();
      File yourFile = new File(dir, "path/file.txt");
      FileInputStream fis = new FileInputStream(yourFile);
      byte[] data = new byte[(int) yourFile.length()];
      fis.read(data);
      fis.close();
      
      String str = new String(data, "UTF-8");
      String[] array = str.splite(" ");
      

      【讨论】:

      • 是的,谢谢您的回复!我会尝试并回复!
      • @CaptainLemon,我编辑了答案,一个变量是错误的。
      • 我现在如何排序这个数组?谢谢!
      • @CaptainLemon ,使用这个 Arrays.sort(array);
      猜你喜欢
      • 1970-01-01
      • 2021-07-04
      • 2013-08-26
      • 2021-04-11
      • 1970-01-01
      • 2014-09-25
      • 2021-05-21
      • 2015-03-24
      • 1970-01-01
      相关资源
      最近更新 更多