将一个文本文件中的多个单词存储到一个字符串中,该如何操作?

huangapple go评论63阅读模式
英文:

How can I store more than one word inside a txt file into a String in one sitting?

问题

所以我有一个名为`dictionary.txt`的txt文件里面包含了一个非常长的单词列表每个单词占据一行格式如下

priceably
southwesterly
semipapal
genuine
cardiotoxicities
shoeman
Utahan
ungeodetically
Schurman
...


我仍然在努力寻找一种方法,将它们全部存储在一个字符串数组中,而不必一个一个地操作。

到目前为止,这是我得到的代码:

```java
String[] words = new String[0];
int i = 0;

try
{
    Scanner scnr = new Scanner(new File("dictionary.txt"));
    String line;
    while (scnr.hasNext())
    {
        line = scnr.nextLine();
        String[] lineWords = line.split("\r?\n|\r");
        words = concatenate(words, lineWords);
    }
    scnr.close();
}

如何优化这段代码,以便程序能更快地完成这个过程?


<details>
<summary>英文:</summary>

So I have a really long list of words inside a txt called `dictionary.txt`. It has one word, then it skips to a new line and has the next one as follows:

priceably
southwesterly
semipapal
genuine
cardiotoxicities
shoeman
Utahan
ungeodetically
Schurman
...


I am still struggling to find a way to store them all in an array of Strings without having to do it one by one.


This is what I got so far:

```java
        String[] words = new String[0];
        int i = 0;
  
        try
        {
            Scanner scnr = new Scanner(new File(&quot;dictionary.txt&quot;));
            String line;
            while (scnr.hasNext())
            {
              line = scnr.nextLine();
              String[] lineWords = line.split(&quot;\r?\n|\r&quot;); 
              words = concatenate(words, lineWords);
            }
            scnr.close();
        } 

How can I optimize this better so that the program finishes this process faster?

答案1

得分: 0

以下是您要求的部分内容的翻译:

如果将来有人用得上的话这是我在评论中实现解决方案的方式我没有继续使用数组列表因为我几乎不知道如何使用它们而且截止日期正在逼近根本来不及学习它们):

```java
    public static void main(String[] args) throws IOException {
        NumberFormat formatter = NumberFormat.getNumberInstance(Locale.US); // 格式化显示的纳秒数

        List&lt;String&gt; words = Files.readAllLines(Paths.get(&quot;dictionary.txt&quot;), StandardCharsets.UTF_8); // 读取文本文件并将其存储在数组列表中

        String[] transformed = words.toArray(new String[words.size()]); // 将数组列表转换为字符串数组...
        String[] wordsToSort = transformed.clone(); // 复制以便将来安全更改它...
  
        // 测试排序算法...

这行代码 List&lt;String&gt; words = Files.readAllLines(Paths.get(&quot;dictionary.txt&quot;), StandardCharsets.UTF_8); 一次性将超过 600,000 个元素存储在数组列表中。这样,您不需要循环一个个地存储它们。


<details>
<summary>英文:</summary>

In case this is useful for anyone in the future. This is how I implemented the solution in the comments (I didn&#39;t keep using array lists because I barely know how to use them and the deadline was encroaching forward to even start learning that now):

```java
    public static void main(String[] args) throws IOException {
        NumberFormat formatter = NumberFormat.getNumberInstance(Locale.US); // formats the amount of nanoseconds displayed

        List&lt;String&gt; words = Files.readAllLines(Paths.get(&quot;dictionary.txt&quot;), StandardCharsets.UTF_8); // reads txt file and stores it in an array list
  
        String[] transformed = words.toArray(new String[words.size()]); // transforms array list into a String array...
        String[] wordsToSort = transformed.clone(); // makes a copy to safely change it in the future...
  
        // Testing Sorting algorithms......

This line List&lt;String&gt; words = Files.readAllLines(Paths.get(&quot;dictionary.txt&quot;), StandardCharsets.UTF_8); stores more than 600000 elements into an array list in one sitting. In this way, you do not need a loop to store them all one-by-one.

huangapple
  • 本文由 发表于 2023年3月12日 09:23:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定