程序如下:
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!
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!
解决方案 »
- 关于JComponent一个问题
- java 标准IO
- Java上机实现统计某一目录下每个文件中出现的字母个数、数字个数、空格个数及行数?
- 谁来告诉我一下具体的算法
- private,static,final,abstratr的困惑
- 用JAVA写的通讯类 服务器端接受不到客户端的消息 帮看看这么回事
- URLConnection的两个问题
- 在JTextArea里获得光标所在列号不对,高手请进!!!!!
- 怎样才能得到weblogic中的用户和组信息?
- 有没有人用java写过http服务,要有带get和post的参数分析的,谁有??
- import 导入其他盘的自己写的class文件,如何操作?
- 晕啊,老是遇到这样的错误,到底是为什么啊???
打印出这一行的值看看,到底是哪个在作怪,
另外建议用
String typeIn = java.io.BufferedReader(new InputStreamReader()).readLine();
来获取输入的东东~
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!"); }
}
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());这句话不行?? 区别在哪里啊?