简体   繁体   English

将单个字符串保存到数组中,文件文本java中用多个空格隔开

[英]save single string to array separate by multi space in file text java

I'm a java beginner and I'm doing a small project about dictionary, now I want to save word and translate mean in file, because my native language often have space like chicken will be con gà so, I must use other way, not by space, but I really don't know how to do that, a word and it translation in one line, separate by "tab", mean multi space like chicken con gà now I want to get 2 words and store it in my array of Words which I created before, so I want to do something like 我是一名Java初学者,我正在做一个有关字典的小项目,现在我想保存单词并在文件中翻译均值,因为我的母语经常有空间,例如chickencon gà所以,我必须使用其他方式,不是按空格排列的,但是我真的不知道该怎么做,一个单词并将其翻译成一行,并用“制表符”隔开,意思是像chicken con gà这样的多空格,现在我想得到2个单词并将其存储在我的计算机中我之前创建的单词数组,所以我想做类似的事情

w1=word1inline;
w2=word2inline;
Word(word1inline,word2inline);(this is a member of array);

please help me, thanks a lot, I just know how to read line from file text, and use split to get word but I am not sure how to read by multi space. 请帮助我,非常感谢,我只知道如何从文件文本中读取行,并使用split来获取单词,但是我不确定如何通过多空格读取。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class docfile {
    public static void main(String[] args)
    {
        String readLine;
        ArrayList<String>str=new ArrayList<>(String);
        try {
            File file = new File("text.txt");
            BufferedReader b = new BufferedReader(new FileReader(file));
            while ((readLine = b.readLine()) != null) {
                str.add()=readLine.split("  ");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

If you stick to using tabs as a separator, this should work: 如果您坚持使用制表符作为分隔符,则应该可以使用:

package Application;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Application {
    public static void main(String[] args) {
        String line;
        ArrayList<String> str = new ArrayList<>();
        try {
            File file = new File("text.txt");
            BufferedReader b = new BufferedReader(new FileReader(file));
            while ((line = b.readLine()) != null) {
                for (String s : line.split("\t")) {
                    str.add(s);
                }
            }

            str.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Why not just use a properties file? 为什么不只使用属性文件?

dict.properties : dict.properties

chicken=con gá

Dict.java : Dict.java

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

public class Dict {
    public static void main(String[] args) throws IOException {
        Properties dict = new Properties();
        dict.load(Files.newBufferedReader(Paths.get("dict.properties")));
        System.out.println(dict.getProperty("chicken"));
    }
}

Output: 输出:

con gá Congá

If your line is like this chicken con gà you can use indexof() method to find the first space in the string. 如果您的代码行像这chicken con gà ,则可以使用indexof()方法查找字符串中的第一个空格。

Then you can substring each word by using substring() method. 然后,您可以使用substring()方法对每个单词进行substring()

    readLine = b.readLine();
    ArrayList<String>str=new ArrayList<>();

    int i = readLine.indexOf(' ');
    String firstWord = readLine.substring(0, i);
    String secondWord = readLine.substring(i+1, readLine.length());

    str.add(firstWord);
    str.add(secondWord);

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

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