import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) throws IOException {
//对三国演义.txt的读取
RandomAccessFile file =new RandomAccessFile("三国演义.txt", "rw");
//先将回数hs定义为0,后续在函数中再查找
int hs=0;
while(hs!=999) {
//输入想要查看的回数,返回给sc
System.out.println("输入您想查看的回数");
Scanner sc=new Scanner(System.in);
//定义一个查询上下回的输入值,上回为1,下回为2
Scanner hh=new Scanner(System.in);
//获得要查看的回数
int hs1=sc.nextInt();
//调用查找函数
Demo.xh(file,hs1);
//询问是否要查看下一回,或者上一回
System.out.println("是否需要查看上一回或者下一回");
System.out.println("如果是上一回请输入1,下一回请输入2,不查看请输入999");
int h1=hh.nextInt();
//如果是第一回则不能查看上一回
if(h1==1) {
if(hs1==1) {System.out.println("您现在位于第一回不能查看上一回");}
if(hs1!=1) {Demo.xh(file, hs1-1);}
}
//如果是最后则不能查看下一回
if(h1==2) {
if(hs1==120) {System.out.println("您现在位于最后不能查看下一回");}
if(hs1!=120) {Demo.xh(file, hs1+1);}
}
}
}
public static void xh(RandomAccessFile file,int sc) throws IOException {
//循环遍历查找所需要的回数
String str="";
int i=0;
//------------------------------------------------------------------
//如果回数小于10则调用小于10的循环
if(sc<10)
{
while((str=file.readLine())!=null) {
System.out.println(str);
}
}
//------------------------------------------------------------------
//如果回数大于10小于100则调用大于10小于100的循环
if(sc>10&&sc<100)
{
while((str=file.readLine())!=null) {
if(str.startsWith("第0"+sc+"")) {
}
}
}
//------------------------------------------------------------------
//如果回数大于100则调用回数大于100的循环
if(sc>100)
{
while((str=file.readLine())!=null) {
if(str.startsWith("第"+sc+"")) {
}
}
}
}
}
其中上面对于读取循环我没有写完,但是发现读取进来的str.readLine()全是乱码
两个文件的解码格式我设置的都是UTF-8,我不是很明白为啥读取进来的全是乱码,求解!!