📄 readfromfile.java
字号:
//【代码7-3-4】
//ReadFromFile.java
import java.io.*;
class ReadFromFile
{
public static void main(String args[])
{
System.out.println("Please enter a directory that the file located in:");
//构造待读取文件的目录
StringBuffer stfDir = new StringBuffer();
//从键盘获取输入字符,存储进入字符缓冲区
While((char ch = (char)System.in.read())!='\n')
{
stfDir.appendChar(ch);
}
//创建目录文件对象
File dir = new File(stfDir.toString());
System.out.println("Please enter a filename that want to read:");
//获取待读取的文件名
StringBuffer stfFilename = new StringBuffer();
//从键盘获取输入字符,存储进入字符缓冲区
While((char ch = (char)System.in.read())!='\n')
{
stfFilename.appendChar(ch);
}
//创建文件对象
File readFrom = new File(dir,stfFilename.toString());
//判断文件是否为目录、是否具有写权限、读权限
if(readFrom.isFile() && reafFrom.canWrite() && reafFrom.canRead())
{
//创建RandomAccessFile对象
RandomAccessFile rafFile =
new RandomAccessFile(readFrom,"rw");
//如果未读到文件尾,则继续读取
while(rafFile.getFilePointer()<rafFile.length())
System.out.println(file.readLine());
//文件关闭
rafFile.close();
}
else
System.out.println("File cann't be read!");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -