Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中处理文本文件中的每五个单词?_Java_Text Files - Fatal编程技术网

如何在Java中处理文本文件中的每五个单词?

如何在Java中处理文本文件中的每五个单词?,java,text-files,Java,Text Files,大家好, 我有一个文本文件,上面写着“test.txt”,我只想对每5个单词进行处理 例如,如果test.txt包含: 在“插入”选项卡上,图库包含与文档整体外观协调的项目。 我想记下前五个字:在插入选项卡上,对它们执行一些功能。接下来的五个单词库中包含了在文件结束前属于、执行函数等的项目 我想用java实现这一点。有什么想法吗?因此,以下伪代码: 读文件 把单词列成一个单子 while(保留未处理的项目) 五分钟 处理它们 重复 可以沿着这条路线实施 String fileConten

大家好,

我有一个文本文件,上面写着“test.txt”,我只想对每5个单词进行处理

例如,如果test.txt包含:

在“插入”选项卡上,图库包含与文档整体外观协调的项目。

我想记下前五个字:
在插入选项卡上
,对它们执行一些功能。接下来的五个单词
库中包含了在文件结束前属于
、执行函数等的项目

我想用java实现这一点。有什么想法吗?

因此,以下伪代码:

  • 读文件
  • 把单词列成一个单子
  • while(保留未处理的项目)
    • 五分钟
    • 处理它们
  • 重复
可以沿着这条路线实施

String fileContent = readFile("test.txt");
List<String> words = splitWordsIntoList( fileContent );
int n = 0;
List<String> five = new ArrayList<String>();
for( String word : words ) { 
  if( n++ < 5 ) { 
     five.add( word );
  } else { 
      n = 0 ;
      process( five );
  }
}
stringfilecontent=readFile(“test.txt”);
列表字=拆分字到列表(文件内容);
int n=0;
列表五=新的ArrayList();
对于(字符串字:字){
如果(n++<5){
五、加(字);
}否则{
n=0;
过程(五);
}
}
所以这个伪代码:

  • 读文件
  • 把单词列成一个单子
  • while(保留未处理的项目)
    • 五分钟
    • 处理它们
  • 重复
可以沿着这条路线实施

String fileContent = readFile("test.txt");
List<String> words = splitWordsIntoList( fileContent );
int n = 0;
List<String> five = new ArrayList<String>();
for( String word : words ) { 
  if( n++ < 5 ) { 
     five.add( word );
  } else { 
      n = 0 ;
      process( five );
  }
}
stringfilecontent=readFile(“test.txt”);
列表字=拆分字到列表(文件内容);
int n=0;
列表五=新的ArrayList();
对于(字符串字:字){
如果(n++<5){
五、加(字);
}否则{
n=0;
过程(五);
}
}

检查SDK中的String.split()方法。可能会为您提供一个很好的方向。

查看SDK中的String.split()方法。你可以将整个文本文件读入一个字符串,并使用字符串标记器创建一个单词数组,只要你感兴趣的单词总是以空格分隔。

你可以将整个文本文件读入一个字符串,并使用字符串标记器创建一个单词数组只要你感兴趣的单词总是用空格隔开。

5个单词组,然后循环找到的匹配项

Pattern p = Pattern.compile("(\\w*\\s?){5}");
String s = "On the Insert tab the galleries include items that are designed to coordinate with the overall look of your document.";
Matcher m = p.matcher(s);
while (m.find()) {
   String words_group = m.group();
   System.out.println(words_group);
}
要拆分单词组,您可以:

words_group.split(" "); // returns String[]

词组为5,然后循环查找到的匹配项

Pattern p = Pattern.compile("(\\w*\\s?){5}");
String s = "On the Insert tab the galleries include items that are designed to coordinate with the overall look of your document.";
Matcher m = p.matcher(s);
while (m.find()) {
   String words_group = m.group();
   System.out.println(words_group);
}
要拆分单词组,您可以:

words_group.split(" "); // returns String[]

到目前为止你有什么?到目前为止你有什么?你不应该在
process(five)之后调用else块中的
five.removeAll()
?感谢您的回复,但您能稍微澄清一下您的代码吗。@用户未知:确实如此@黛西,几乎没有。我想这已经很清楚了,因为我不是在试图为你做你的工作。你必须向我们展示你目前所拥有的以及你需要帮助的方面。这不是我做作业的网站。对不起,我知道这不是“做我的作业网站”。感谢您的友好回复。您不应该在
过程(五)之后在else块中调用
five.removeAll()
?感谢您的回复,但您能稍微澄清一下您的代码吗。@用户未知:确实如此@黛西,几乎没有。我想这已经很清楚了,因为我不是在试图为你做你的工作。你必须向我们展示你目前所拥有的以及你需要帮助的方面。这不是我做作业的网站。对不起,我知道这不是“做我的作业网站”。感谢您的友好回复。感谢您的回复。我如何实现它以循环每组5个单词。而
将循环每个匹配的组。每组将是从工作字符串中剪下的5个单词组成的字符串。如果您需要循环5个分组单词的字符串,您可以在空白处拆分。感谢您的回复。我如何实现它以循环每个5个单词的组。
while
将循环每个匹配的组。每组将是从工作字符串中剪下的5个单词组成的字符串。如果需要循环5个分组单词组成的字符串,可以使用空格分隔。