我有这个问题而且我不太清楚如何回答它 - 我能够阅读文件但不确定如何只显示超过10个字符的单词
答案 0 :(得分:0)
这应该有效:
private static void readFile(File fin) throws IOException {
FileInputStream fis = new FileInputStream(fin);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = br.readLine()) != null) {
if(line.length()>10) System.out.println(line);
}
br.close();
}
答案 1 :(得分:0)
在String
变量中使用名为word:
if (word.length() > 10)
System.out.println(word);
PS:google之前询问!!
答案 2 :(得分:0)
这里有一些伪代码可以帮助你入门
create empty list //where we'll add all the >10char words
read file(split per newline) //see the apache commons api
for each line
split on space
for each word in splitted sentence
if wordLength > 10 add to empty list
print each entry in your filled list
答案 3 :(得分:0)
虽然其他答案是正确的,但我建议使用scanner类来读取文件,因为它可以安全地检测文件结束条件并具有更简单/更有用的实用方法: -
Scanner input = new Scanner(new File("file.txt"));
while(input.hasNextLine())
{
String word = input.nextLine();
if(word.length()>10){
System.out.println(word)
}
}
答案 4 :(得分:0)
这是你的逻辑。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream(new File("your.txt")); // path for the text file
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = br.readLine()) != null) {
String st[] = line.split(" ");
for(int i=0; i<st.length; i++){
if(st[i].length()>10) System.out.println(st[i]);
}
}
}
}