帮初学者看一个郁闷的问题!!!!

tiandiqing 2004-11-05 11:10:32
我的这个文件为什么有错呀,我是照着书写的




import java.io.*;

class ReadFromFile
{
public static void main(String args[])
{
System.out.println("请输入目录:");

/*构造带读取文件目录*/
StringBuffer stfDir = new StringBuffer();

/*从键盘获取数字*/
while ((char aa = (char)System.in.read())!='\n')
{
stfDir.apppendChar(aa);

}

File dir = new File(stfDir.toString());

System.out.println("请输入文件名称:");

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() && readFrom.canWrite() && readFrom.canRead())
{
RandomAccessFile rafFile = new RandomAccessFile(readFrom,"rw");

while(rafFile.getFilePointer()<refFile.length())
System.out.println(file.readLine());
rafFile.close();
}else{
System.out.println("文件关闭");

}
}
}

//就是这句话出错!!!
char aa = (char)System.in.read())!='\n'
...全文
149 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sboom 2004-11-05
  • 打赏
  • 举报
回复
没错,嘻嘻,我看错了。。。
sboom 2004-11-05
  • 打赏
  • 举报
回复
char aa = (char)System.in.read())!='\n'
括号都错了。
tiandiqing 2004-11-05
  • 打赏
  • 举报
回复
但是执行不了了,执行有错呀!!
llp1217 2004-11-05
  • 打赏
  • 举报
回复
錯誤太多了,改了一下,能編譯了
import java.io.*;

class ReadFromFile {
public static void main(String args[]) throws FileNotFoundException {
System.out.println("请输入目录:");

/* 构造带读取文件目录 */
StringBuffer stfDir = new StringBuffer();

/* 从键盘获取数字 */
try {
char aa = (char) System.in.read();
while (aa != '\n') {
stfDir.append(aa);

}
} catch (IOException ioe) {
ioe.printStackTrace();
}

File dir = new File(stfDir.toString());

System.out.println("请输入文件名称:");

StringBuffer stfFileName = new StringBuffer();

try {
char ch = (char) System.in.read();
while (ch != '\n') {
stfFileName.append(ch);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}

/* 创建文件对象 */
File readFrom = new File(dir, stfFileName.toString());

/* 判断文件是否为目录,具有的权限 */
try {
if (readFrom.isFile() && readFrom.canWrite() && readFrom.canRead()) {
RandomAccessFile rafFile = new RandomAccessFile(readFrom, "rw");

while (rafFile.getFilePointer() < rafFile.length())
System.out.println(rafFile.readLine());
rafFile.close();
} else {
System.out.println("文件关闭");

}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
febchen 2004-11-05
  • 打赏
  • 举报
回复
这样可以了
关键是:
File dir = new File(stfDir.toString().trim());
要去掉空格,否则显示不出来


import java.io.*;

class ReadFromFile
{
public static void main(String args[])
{
char aa;
System.out.println("ÇëÊäÈëĿ¼£º");

/*¹¹Ôì´ø¶ÁÈ¡ÎļþĿ¼*/
StringBuffer stfDir = new StringBuffer();

/*´Ó¼üÅÌ»ñÈ¡Êý×Ö*/
try{
while ((aa=(char)System.in.read())!='\n')
{
stfDir.append(aa);

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

File dir = new File(stfDir.toString().trim());

System.out.println("ÇëÊäÈëÎļþÃû³Æ£º");

StringBuffer stfFileName = new StringBuffer();


char ch;

try{
while((ch=(char)System.in.read())!='\n')
{
stfFileName.append(ch);
}
}catch(Exception e){
System.out.println(e);
}

/*´´½¨Îļþ¶ÔÏó*/
File readFrom = new File(dir,stfFileName.toString().trim());


/*ÅжÏÎļþÊÇ·ñΪĿ¼£¬¾ßÓеÄȨÏÞ*/
try{
if(readFrom.isFile() && readFrom.canWrite() && readFrom.canRead())
{
RandomAccessFile rafFile = new RandomAccessFile(readFrom,"rw");

while(rafFile.getFilePointer()<rafFile.length())
System.out.println(rafFile.readLine());
rafFile.close();
}else{
System.out.println("Îļþ¹Ø±Õ");

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

62,623

社区成员

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

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