English 中文(简体)
在 Java 将巨大的 txt 成数组的过程
原标题:Process of making a huge txt into array in Java

我在一个txt文件上写了上千个句子 我的第一个Android应用程序应该从那里取一个 然后把它放在文本View上

我可以将 txt 文件作为一个资源, 或者同样, 尝试把所有的句子 转换成一个数组 。 我不想把我的 txt 放到应用程序中, 而是直接用这些句子直接放入数组 。 我怎么能自动“ 翻译” 上千个句子到一个类似数组的列表?

最佳回答
i guess this is what u dont want to do...
//InputStream is = getResources().openRawResource(R.raw.list);

,然后获得一个输入Stream 对象,并跟随代码使用

List<String> content=new ArrayList<String>();
InputStreamReader isr = new InputStreamReader(is);
linereader = new LineNumberReader(isr);
for (int i = 0; i < num; i++) {  // num is  total no of lines in file
    try {
            line = linereader.readLine();
            content.add(line);

         } 
         catch (IOException e) 
         {
        e.printStackTrace();
          }
     }// for ends
问题回答

不要重新发明车轮... 这是一个单行道:

import org.apache.commons.io.IOUtils;

List<String> sentences = (List<String>)IOUtils.readLines(new FileInputStream("filename.txt"));

如果您知道每个项目只有一个时段, 您就可以分开。 如果您不能做到这一点, 但是可以使用新线, 请使用它 。

final String blob = "Quote 1. Quote2. Quote  3 .";
String[] quotes = blob.split( \. );
> ["Quote 1", "Quote2", "Quote  3 "];

OR


final String blob = "Quote 1.
Quote 2 longer.";
String[] quotes = blob.split("
");
> ["Quote 1.", "Quote 2 longer."]

听上去您想将文本文件放入资源文件夹, 然后用缓冲 Reader 读取它 。

根据文本文件的内容,您可以逐行阅读文本行并将其添加到一个矩阵中,或者将整个文件作为字符串阅读,然后使用.split (), 这将返回一个字符串, 供您使用 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签