randomdemo.java
来自「Java面向对象编程(随书配套源代码) 阐述了面向对象编程的思想」· Java 代码 · 共 39 行
JAVA
39 行
package chapter13;
import java.io.*;
public class RandomDemo
{
public static void main(String args[]) throws IOException
{
int lineNo;
long fp;
InputStreamReader stdin=new InputStreamReader(System.in);
BufferedReader bufin=new BufferedReader(stdin);
RandomAccessFile file=new RandomAccessFile("random.txt","rw");
System.out.println("请输入6个字符串:\n");
int len[]=new int[6];
String strInput[]=new String[6];
for(int i=0;i<6;i++)
{
System.out.print("行号"+(i+1)+":");
strInput[i]=bufin.readLine();
len[i]=strInput[i].length();
file.write((strInput[i]+"\n").getBytes());
}
while(true)
{
fp=0;
file.seek(0);
System.out.print("你要显示第几行?"+"(1--6) ");
lineNo=Integer.parseInt(bufin.readLine());
for(int i=1;i<lineNo;i++)
fp=fp+(long)len[i-1]+1;
file.seek(fp);
System.out.println("第"+lineNo+"行"+":"+file.readLine());
System.out.println("继续吗?"+"(y/n)");
if((bufin.readLine().equals("n")))
break;
}
file.close();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?