錯誤太多了,改了一下,能編譯了
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();
}
}
}
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();
}
}
}
解决方案 »
- 关于日期与星期的问题
- [无事慢慢发点Java经典代码一,随便温习鸟语]
- 使用exe4j的问题???
- 一个诡异的笔试题关于String和StringBuffer
- 将JButton放在JTable列的右端
- 关于在IBM上看到的一个synchronized问题
- 我用jdk1.4.1,xp的,运行javac一直有"can not read.. 1 error"
- farawayzheng_necas(遥远)接分!
- Java初学,在线等待,高分相送,也许简单,HelloWold为何运行不起来?没事也可来看看啊*^o^*
- window XP 下装JDK1.4.0的问题
- 怎么生成随机6位号码啊,急,没分了,不好意思,帮个忙
- 关于java里的控制台输入问题
括号都错了。
关键是:
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);
}
}
}