好的,这是一个草稿,将此逻辑构建到您现有的代码中,编辑您的问题,我们可以帮助您完成它。
使用的关键方法是 FileReaders read() 方法来获取单个字符(如果没有更多,则为 -1)和 skip() 以忽略一定数量的字符(已经处理)
(抱歉我的格式有点匆忙)
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class SimpleReader {
File theSourceFile = new File("D:\\<yourPath>\\input.txt");
int nrOfCharsToRead = 10;
int nrOfCharsProcessed = 0;
public static void main(String[] args) {
new SimpleReader().process();
}
public void process(){
System.out.println(getNext(nrOfCharsToRead));
System.out.println(getNext(nrOfCharsToRead));
System.out.println(getNext(nrOfCharsToRead));
System.out.println(getNext(nrOfCharsToRead));
System.out.println(getNext(nrOfCharsToRead));
System.out.println(getNext(nrOfCharsToRead));
System.out.println(getNext(nrOfCharsToRead));
System.out.println(getNext(nrOfCharsToRead));
System.out.println(getNext(nrOfCharsToRead));
System.out.println(getNext(nrOfCharsToRead));
}
private String getNext(int nrOfCharsToRead){
try {
FileReader reader = new FileReader(theSourceFile);
StringBuffer b = new StringBuffer();
reader.skip(nrOfCharsProcessed);
char c;
int counter = 0;
StringBuffer sb = new StringBuffer();
while((c = (char) reader.read()) != -1 && counter < nrOfCharsToRead){
sb.append(c);
nrOfCharsProcessed++;
counter++;
}
reader.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}