程序如下:
import java.io.*;
public class randomRead 
{
   public static void main(String args []) throws Exception
  {
    System.out.println("Please enter a directory that the file located in:");
    StringBuffer stfDir = new StringBuffer();
    char ch;
    while((ch = (char)System.in.read())!= '\n')
   {
      stfDir.append(ch);
  }

  File dir = new File(stfDir.toString());
 System.out.println("Please enter a filename that want to read:");
 StringBuffer stfFilename = new StringBuffer();
 char c;
 while((c= (char)System.in.read())!='\n')
  {
stfFilename.append(ch);
  }
 File readFrom = new File(dir,stfFilename.toString());
 if(readFrom.isFile()&&readFrom.canRead()&&readFrom.canWrite())
 {
RandomAccessFile rafFile = new RandomAccessFile(readFrom,"rw");
while(rafFile.getFilePointer()<rafFile.length())
System.out.println(rafFile.readLine());
         rafFile.close();
 }

 else

  System.out.println("File cann't be read!");

}
}程序运行结果如下:
javacodePlease enter a directory that the file located in:
C:\javacode
Please enter a filename that want to read:
abc.txt
File cann't be read!我的问题是:我不管怎么运行,结果都会出现File cann't be read!
            我创建了个abc.txt已经在c:\javacode下面了,可为什么会出现File cann't be read!

解决方案 »

  1.   

    if(readFrom.isFile()&&readFrom.canRead()&&readFrom.canWrite())
    打印出这一行的值看看,到底是哪个在作怪,
    另外建议用 
    String typeIn = java.io.BufferedReader(new InputStreamReader()).readLine();
    来获取输入的东东~
      

  2.   

    给你改完了,结贴吧
    import java.io.*;public class randomRead
    {
        public static void main(String args[]) throws Exception
        {
            System.out.println("Please enter a directory that the file located in:");
            StringBuffer stfDir = new StringBuffer();
            char ch;
            while((ch = (char)System.in.read()) != '\n')
            {
                stfDir.append(ch);
            }        File dir = new File(stfDir.toString());
            
            System.out.println("Please enter a filename that want to read:");
            StringBuffer stfFilename = new StringBuffer();
            char c;
            while((c = (char)System.in.read()) != '\n')
            {
                stfFilename.append(c);
            }        String[] dirStr=dir.getPath().split("\r");
            
            String[] fileStr=stfFilename.toString().split("\r");  
            
            File readFrom = new File(dirStr[0], fileStr[0]);        if(readFrom.isFile() && readFrom.canRead() && readFrom.canWrite())
            {
                RandomAccessFile rafFile = new RandomAccessFile(readFrom, "rw");
                while(rafFile.getFilePointer() < rafFile.length())
                    System.out.println(rafFile.readLine());
                rafFile.close();
            }
            else
                System.out.println("File cann't be read!");    }
    }
      

  3.   

    土豆块:请问这下面几句话什么意思?
           String[] dirStr=dir.getPath().split("\r");
           String[] fileStr=stfFilename.toString().split("\r");  
           File readFrom = new File(dirStr[0], fileStr[0]);
    为什么
    File readFrom = new File(dir,stfFilename.toString());这句话不行?? 区别在哪里啊?   
      

  4.   

    split("\r")   这是干什么啊?