繁体   English   中英

从文本文件读取到字符串数组

[英]reading from text file to string array

所以我可以在我的文本文件中搜索一个字符串,但是,我想在这个ArrayList中对数据进行排序并实现一个算法。 是否可以从文本文件中读取文本文件中的[Strings]值存储在String []数组中。

也可以将字符串分开吗? 而不是我的数组有:

[Alice was beginning to get very tired of sitting by her sister on the, bank, and of having nothing to do:]

是否可以将数组作为:

["Alice", "was" "beginning" "to" "get"...]

    public static void main(String[]args) throws IOException
    {
        Scanner scan = new Scanner(System.in);
        String stringSearch = scan.nextLine();

        BufferedReader reader = new BufferedReader(new FileReader("File1.txt"));
        List<String> words = new ArrayList<String>();

        String line;
        while ((line = reader.readLine()) != null) {                
            words.add(line);
        }

        for(String sLine : words) 
        {
            if (sLine.contains(stringSearch)) 
            {
                int index = words.indexOf(sLine);
                System.out.println("Got a match at line " + index);

            }
         }

        //Collections.sort(words);
        //for (String str: words)
        //      System.out.println(str);

        int size = words.size();
        System.out.println("There are " + size + " Lines of text in this text file.");
        reader.close();

        System.out.println(words);

    }

也可以将字符串分开吗? 是的,您可以使用此分隔字符串作为空格。

 String[] strSplit;
 String str = "This is test for split";
 strSplit = str.split("[\\s,;!?\"]+");

请参见String API

此外,您还可以逐字阅读文本文件。

 Scanner scan = null;
 try {
     scan = new Scanner(new BufferedReader(new FileReader("Your File Path")));
 } catch (FileNotFoundException e) {
     e.printStackTrace();
 }

 while(scan.hasNext()){
     System.out.println( scan.next() ); 
 }

请参阅扫描仪API

要将一行拆分为一个单词数组,请使用:

String words = sentence.split("[^\\w']+");

正则表达式[^\\w']表示“不是单词char或撇号”

这将捕获带有“can can”等嵌入式撇号的单词,并跳过所有标点符号。

编辑:

注释已经提出了解析报字的边缘情况下,如'this'this
这是解决方案 - 您必须先删除包装引号:

String[] words = input.replaceAll("(^|\\s)'([\\w']+)'(\\s|$)", "$1$2$3").split("[^\\w']+");

这是一些边缘和角落情况的测试代码:

public static void main(String[] args) throws Exception {
    String input = "'I', ie \"me\", can't extract 'can't' or 'can't'";
    String[] words = input.replaceAll("(^|[^\\w'])'([\\w']+)'([^\\w']|$)", "$1$2$3").split("[^\\w']+");
    System.out.println(Arrays.toString(words));
}

输出:

[I, ie, me, can't, extract, can't, or, can't]

暂无
暂无

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

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