请教牛X人士,关于File类的问题!

onefish 2003-07-30 05:01:02
File readForm = new File(dir, stfFilename.toString());
参数的值为
dir : E:\\
sftFilename.toString() : user.txt
用System.out.print输出readForm,结果为\user.txt
可是将语句改为File readForm = new File("E:\\user.txt");
结果正确!这是为什么?编译用的是JDK build 1.4.2-b28.同样的程序在JBuilder9 IDE环境中结果正确!这个跟环境变量有关吗?还是JDK的毛病?希望能给出解决方法!



...全文
26 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
onefish 2003-08-07
  • 打赏
  • 举报
回复
已经解决!问题在System.in.read哪!Window下JBuilder对回车换行处理成为一个字符,而在JDK下,对回车换行处理为两个字符0xD、0xA,由于JDK中字符串只处理了一个0xD,造成错误!
最终问题还是在http://www.it315.org/解决了。CSDN上混分的人太多了!基本上已经解决不了什么实质性的问题了。我在CSDN上问过两个JAVA问题,都是初学者在学习中所遇到的基本问题,基本没有人能解决。真让人心寒。

原码如下希望能对以后遇到类似问题的人起点做用:
import java.io.*;

public class ReadFromFile {
public static void main(String[] args) {
System.out.println("Please enter a directory that the file located in:");
StringBuffer stfDir = new StringBuffer();
char charet;
boolean EnterMask = true;
try {

while ( (charet = (char) System.in.read()) != '\n') {
if (charet != '\r') {
stfDir.append(charet);
}
}
}
catch (IOException e) {
System.out.println("Read file error!");
}

File DirectFile = new File(stfDir.toString());
System.out.println("Please enter a filename that want to read:");
StringBuffer stfFilename = new StringBuffer();

try {
while ( (charet = (char) System.in.read()) != '\n') {
if (charet != '\r') {
stfFilename.append(charet);
}
}
}
catch (IOException e) {
System.out.println("Read char error");
}
System.out.println("Directory: " + DirectFile);
System.out.println("FileName: " + stfFilename.toString());
File readForm = new File(DirectFile, stfFilename.toString());
System.out.println("Path: " + stfFilename.toString());

if (readForm.isFile() && readForm.canWrite() && readForm.canRead()) {
try {
RandomAccessFile rafFile = new RandomAccessFile(readForm, "rw");
while (rafFile.getFilePointer() < rafFile.length()) {
System.out.println(rafFile.readLine());
}
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Read file info error!");
}
}
}
}

onefish 2003-08-06
  • 打赏
  • 举报
回复
有没有人能解决?
onefish 2003-08-04
  • 打赏
  • 举报
回复
最主要的是结果!!!结果用JDK编译运行是错的!
Hodex 2003-08-01
  • 打赏
  • 举报
回复
是啥,它的构造函数随你选择嘛
zez 2003-08-01
  • 打赏
  • 举报
回复
File(File parent, String child)
Creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname.
File(String parent, String child)
Creates a new File instance from a parent pathname string and a child pathname string.
File(URI uri)
Creates a new File instance by converting the given file: URI into an abstract pathname.


呵呵,是四个

------------------------------------------------------
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
zez 2003-08-01
  • 打赏
  • 举报
回复
File(File parent, String child)
Creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname.
File(String parent, String child)
Creates a new File instance from a parent pathname string and a child pathname string.
File(URI uri)
Creates a new File instance by converting the given file: URI into an abstract pathname.


呵呵,是四个

------------------------------------------------------
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
zez 2003-08-01
  • 打赏
  • 举报
回复
怎么了?
File有三个构造函数,你用的是其中两个...


------------------------------------------------------
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
onefish 2003-08-01
  • 打赏
  • 举报
回复
本人编制了如下代码!基本与上面的一样!在JBuilder和JDK下编译都能过!这是为什么呀?有没有人能帮我?
import java.io.*;

public class FileClass {
public static void main(String[] args) {
File dir = new File ("e:\\");
File fFile = new File(dir, "user.txt");
System.out.println(fFile);
System.out.println(fFile.isFile());
try {
RandomAccessFile RandFile = new
RandomAccessFile(fFile, "rw");
while (RandFile.getFilePointer() <
RandFile.length()) {
System.out.println(RandFile.readLine());
}

}
catch (Exception e) {
System.out.println(e.toString());
}

}
}
jigsaw 2003-08-01
  • 打赏
  • 举报
回复
拜托 到底是谁没有敬业精神啊。。。 学会看api再来发言不迟
onefish 2003-08-01
  • 打赏
  • 举报
回复
楼上两位!能给个解决方法吗??我知道File有三个构造函数!也知道用了其中两个!!能不能有点敬业精神!
onefish 2003-07-30
  • 打赏
  • 举报
回复
源程序如下:
import java.io.*;

public class ReadFromFile {
public static void main(String[] args) {
File TestFile = new File("e:\\test.txt");

System.out.println("Please enter a directory that the file located in:");
StringBuffer stfDir = new StringBuffer();
char charet;
try {
while ( (charet = (char) System.in.read()) != '\n') {
stfDir.append(charet);
}
}
catch (IOException e) {
System.out.println("Read file error!");
}
File dir = new File(stfDir.toString());

System.out.println("Please enter a filename that want to read:");
StringBuffer stfFilename = new StringBuffer();
try {
while ( (charet = (char) System.in.read()) != '\n') {
stfFilename.append(charet);
}
}
catch (IOException e) {
System.out.println("Read char error");
}
System.out.println(dir);
System.out.println(stfFilename.toString() );
File readForm = new File(dir, stfFilename.toString());
//File readForm = new File("e:\\user.txt");
System.out.println(readForm);
if (readForm.isFile() && readForm.canWrite() && readForm.canRead()) {
try {
RandomAccessFile rafFile = new RandomAccessFile(readForm, "rw");
while (rafFile.getFilePointer() < rafFile.length()) {
System.out.println(rafFile.readLine());
}
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Read file info error!");
}
}
}

}

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧