接口IterableText。
- working
- not working
- random output
- non-random output
- the expected output
- unexpected output
- no output
- any output
- crashing at random
- crashing always
- not crashing at all
- corruption of data
- different behaviour, when executed on another system
- , when compiled with another compiler
- , on tuesday
- , only when you are not looking
- same behaviour in any or all of the above cases
- anything else within the power of the computer (hopefully limited by the OS)
FileContent-负责读取文本文件的类。通过方法package main;
import java.util.Iterator;
public interface IterableText {
Iterator<String> charIterator();
Iterator<String> wordIterator();
}
和charIterator()
提供的内容。
我在FileCOntent类中创建了main静态函数,因为我想要测试程序并检查是否全部正常...
wordIterator()
CharIterator遍历文本字母。通过Iterator接口实现next()和hasNext()。
package main;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ListIterator;
public class FileContent implements IterableText{
public String pathToFile;
public ArrayList<String> splittedLetters;
public ArrayList<String> splittedWords;
public FileContent(String pathToFile){
this.pathToFile = pathToFile;
this.splittedLetters = new ArrayList<String>();
this.splittedWords = new ArrayList<String>();
}
public Iterator<String> charIterator(){
try (BufferedReader br = new BufferedReader(new FileReader(this.pathToFile))) {
String line;
while ((line = br.readLine()) != null) {
String[] splittedText = line.split("");
for(String letter: splittedText){
if(!letter.equals(" ")){
splittedLetters.add(letter);
}
}
Iterator<String> itr = splittedLetters.iterator();
while(itr.hasNext()){
return itr;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public Iterator<String> wordIterator(){
try (BufferedReader br = new BufferedReader(new FileReader(this.pathToFile))) {
String line;
while ((line = br.readLine()) != null) {
String[] splittedText = line.split(" ");
System.out.println(Arrays.toString(splittedText));
for(String word: splittedText){
if(!word.equals(" ")){
splittedWords.add(word);
}
}
Iterator<String> itr = splittedWords.iterator();
while(itr.hasNext()){
return itr;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String getFilename(String[] args){
return args[2];
}
public static void main(String[] args){
FileContent fileContent = new FileContent("/home/hubert/IdeaProjects/TextAnalysis/src/main/test.txt");
CharIterator charIterator = new CharIterator(fileContent);
Iterator<String> itr = fileContent.charIterator();
while(itr.hasNext()){
String element = itr.next().toLowerCase();
System.out.print(element + ", ");
}
}
}
WordIterator遍历文本的单词。通过Iterator接口实现next()和hasNext()。
package main;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class CharIterator {
public FileContent fileContent = new FileContent("/home/hubert/IdeaProjects/TextAnalysis/src/main/test.txt");
public int currentIndex = 0;
Iterator<String> itr_string = fileContent.charIterator();
public List<String> ListLetters = StreamSupport.stream(Spliterators.spliteratorUnknownSize(itr_string, Spliterator.ORDERED), false).collect(Collectors.<String> toList());
public String[] arrayLetters = ListLetters.toArray(new String[ListLetters.size()]);
public int currentSize = arrayLetters.length;
public CharIterator(FileContent fileContent){
this.fileContent = new FileContent(fileContent.pathToFile);
}
public boolean hasNext(){
return currentIndex < currentSize && arrayLetters[currentIndex] != null;
}
public String next(){
return arrayLetters[currentIndex++];
}
}
您能告诉我为什么迭代时我只得到文本文件的第一行吗? 每行最后都有符号:\ n