从书上拷贝下来的,怎么老是“can't find the file”,
netos 2004-07-20 11:58:20 import java.io.*;
class ReadFile
{
public static void main(String[] args)
{
System.out.println("Please input the directory name:");
char ch;
StringBuffer dirBuf = new StringBuffer();
try
{
while((ch= (char)System.in.read())!= '\n')
{
dirBuf.append(ch);
}
}
catch(IOException e)
{
System.err.println("Exception : " + e.getMessage());
}
File dir = new File(dirBuf.toString());
System.out.println("Please input the file name:");
StringBuffer fileBuf = new StringBuffer();
try
{
while((ch = (char)System.in.read())!='\n')
{
fileBuf.append(ch);
}
}
catch(IOException e)
{
System.err.println("Exception : " + e.getMessage());
}
File input = new File(dir,fileBuf.toString());
if (input.isFile()&&input.canRead()&&input.canWrite())
{
try
{
RandomAccessFile file = new RandomAccessFile(input,"rw");
System.out.println("File length:" + file.length());
System.out.println("Pointer pos:" + file.getFilePointer());
while (file.getFilePointer()< file.length())
{
System.out.println(file.readLine());
}
file.close();
}
catch(IOException e)
{
System.err.println("Exception : " + e.getMessage());
}
}
else
{
System.err.println("can't find the file");
}
}
}