1.小说分为两种,文字类型、数字类型,根据类型选择代码
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class readtxt {
public static void main(String[] args) throws FileNotFoundException {
String filePath = "C:\\Users\\1508.txt";//文件位置
Scanner scanner = new Scanner(System.in);
System.out.print("请输入起始章节号:");
int startChapter = scanner.nextInt();
int number=20;
scanner.close();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
boolean printing = false;
int currentChapter = startChapter;
/**
* 如果章节是汉字的话,则需要用这个
*/
// numberToChinese number = new numberToChinese();
// String s = number.numberToChinese(startChapter);
// String s1 = number.numberToChinese(startChapter+20);
// String node = "第" + s + "章";
// String tail = "第" + s1 + "章";
/**
* 如果章节是数字的话,则需要用这个
*/
String node = "第" + startChapter + "章";
String tail = "第" + (startChapter+number) + "章";
while ((line = reader.readLine()) != null) {
if (!line.trim().isEmpty()) {
if (line.startsWith(node)) { // 假设章节信息以"Chapter"开头
currentChapter++;//当前章节
if (currentChapter >= startChapter && currentChapter <= startChapter + number) {
printing = true;
}
}
if (line.startsWith(tail)) break;
if (printing) {
System.out.println(line);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、控制台输入你起始章节,如果需要转成汉字的话,2000读则转换成两
public class numberToChinese {
private static final String[] UNITS = { "一", "二", "三", "四", "五", "六", "七", "八", "九","十"};
private static final String[] UNITSK = {"十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九"};
private static final String[] TEENS = {"一十", "二十", "三十", "四十", "五十", "六十", "七十", "八十", "九十"};
public String numberToChinese(int num) {
if (num<10&&num>0){
return UNITS[num-1];
}
if (num == 0) {
return "零";
}
if (num>=10&&num<=19){
int result=num%10;
return UNITSK[result];
}
StringBuilder result = new StringBuilder();
if (num >= 10000) {
throw new IllegalArgumentException("超出范围(只支持0-9999)");
}
int thousands = num / 1000;
if (thousands > 0) {
if (thousands==2){
result.append("两千");
}else {
result.append(UNITS[thousands-1]).append("千");
}
num %= 1000;
// 处理千位之后的零,如2005应为两千零五,而非两千五百
if (num != 0) {
result.append(num < 100 ? "零" : "");
}
}
int hundreds = num / 100;
if (hundreds > 0) {
// if (hundreds==2){
// result.append("两百");
// }else {
result.append(UNITS[hundreds-1]).append("百");
num %= 100;
}
Boolean flag=false;
if (num >= 10) {
int tensIndex = num / 10 - 1; // 转换为TEENS数组的索引
result.append(TEENS[tensIndex]);
num %= 10;
flag=true;
}
if (num > 0) {
if (flag){
result.append(UNITS[num-1]);
}else {
result.append("零").append(UNITS[num-1]);
}
}
// 处理连续的零和一的情况
String strResult = result.toString();
return strResult.isEmpty() ? "零" : strResult.trim();
}
public static void main(String[] args) {
numberToChinese number = new numberToChinese();
System.out.println(number.numberToChinese(201));
}
}
public static void main(String[] args) {
numberToChinese number = new numberToChinese();
System.out.println(number.numberToChinese(201));
}
}
3、效果演示,用上下一行一行阅读