简体   繁体   English

将单词从文件存储到字符串Java中

[英]Storing words from a file into a string Java

I need help constructing an array in which it would hold the values of .txt file. 我需要构造一个数组来保存.txt文件的值。

Text file (example): 文本文件(示例):

this is the text file.

I would want the array to look like: 我希望数组看起来像:

Array[0]:This
Array[1]:is 
etc..

Hoping someone can give me a hand, i am familar with how to open, create and read froma text file, but currently that is about it. 希望有人可以帮我忙,我熟悉如何打开,创建和读取文本文件,但目前仅此而已。 I do not know how to use/play around with the data once i can read it. 一旦读取数据,我不知道如何使用/处理数据。 This is the could i have constructed so far. 这是我到目前为止构建的。

import java.util.*;
import java.io.*;

public class file {

private Scanner x;

    public void openFile(){
      try{
        x=new Scanner(new File("note3.txt"));
      }
      catch(Exception e){
        System.out.println("Could not find file"); }}



    public void readFile(){
      String str;

      while(x.hasNext()){
        String a=x.next();

        System.out.println(a);}}

    public void closeFile(){
      x.close();}}

Seperate file which reads... 单独的文件读取...

    public class Prac33 {


    public static void main(String[] args) { 

     file r =new file();
        r.openFile();
        r.readFile();
        r.closeFile();
      }
    }

I am hoping to store these into an array which i can later use to sort the file alphabetically. 我希望将这些存储到一个数组中,以后可以用来按字母顺序对文件进行排序。

You could store the whole file into a string first, then split it: 您可以先将整个文件存储为一个字符串,然后将其拆分:

    ...
    String whole = "";
    while (x.hasNext()) {
        String a = x.next();
        whole = whole + " " + a;
    }
    String[] array = whole.split(" ");
    ...

Or you could use an ArrayList , which is a 'cleaner' solution: 或者,您可以使用ArrayList ,这是一个“清洁”的解决方案:

    ...
    ArrayList<String> words= new ArrayList<>();
    while (x.hasNext()) {
        String a = x.next();
        words.add(a);
    }
    //get an item from the arraylist like this:
    String val=words.get(index);
    ...

You can add to an ArrayList instead of your System.out.println(a); 您可以添加到ArrayList而不是System.out.println(a); .

Then, you can convert the ArrayList to a String array when you're done using: 然后,可以使用以下命令将ArrayList转换为String array

String[] array = list.toArray(new String[list.size()]);

Here is what you can do: 您可以执行以下操作:

import java.util.*;
import java.io.*;

public class file {

private Scanner x;

public void openFile() {
    try {
        x = new Scanner(new File("note3.txt"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public String[] readFile(String[] array) {
    long count = 0;
    while (x.hasNext()) {
        String a = x.next();
        array[(int) count] = a;
        System.out.println(a);
        count++;
    }
    return array;
}

public void closeFile() {
    x.close();
    }
}

Use 采用

new BufferedReader (new FileReader ("file name"));

With the object of bufferedReader iterate and read lines from the file. 使用bufferedReader的对象迭代并读取文件中的行。 Post that use StringTokenizer to tokenise based on " " empty space and store them to your array . 邮政使用StringTokenizer来tokenise基础上" "空的空间,并将其存储到您的array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM