下面这个程序是书上的例子 /ReadFromFile.java import java.io.*; 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 ch1; char ch2; while((ch1 = (char)System.in.read())!='\n') { stfDir.appendchar(ch1); } File dir = new File(stfDir.toString()); System.out.println("Please enter a filename that want to read:"); StringBuffer stfFilename = new StringBuffer(); while((ch2 = (char)System.in.read())!='\n') { stfFilename.append(ch2); } File readFrom = new File(dir,stfFilename.toString()); 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("File cann't be read!"); } }运行JAVAC ReadFromFile.java 系统显示stfDir.appendchar(ch1)有错。请问怎么修改?谢谢
什么书,这么垃圾,上面不是告诉你怎么改了么?import java.io.*; 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 ch1; char ch2; while((ch1 = (char)System.in.read())!='\n') { stfDir.append(ch1); } File dir = new File(stfDir.toString()); System.out.println("Please enter a filename that want to read:"); StringBuffer stfFilename = new StringBuffer(); while((ch2 = (char)System.in.read())!='\n') { stfFilename.append(ch2); } File readFrom = new File(dir,stfFilename.toString()); 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("File cann't be read!"); } }
bluesmile979(笑着) 你好: 感谢您多次的帮助。我刚才把上面这段程序运行了一遍。系统仍然显示以下错误: while((ch2 = (char)System.in.read())!='\n') ^ ReadFromFile.java:24: unreported exception java.io.FileNotFoundException; must b e caught or declared to be thrown RandomAccessFile rafFile =new RandomAccessFile(readFrom,"rw"); ^ ReadFromFile.java:25: unreported exception java.io.IOException; must be caught o r declared to be thrown while(rafFile.getFilePointer()<rafFile.length()) ^ ReadFromFile.java:25: unreported exception java.io.IOException; must be caught o r declared to be thrown while(rafFile.getFilePointer()<rafFile.length()) ^ ReadFromFile.java:26: unreported exception java.io.IOException; must be caught o r declared to be thrown System.out.println(rafFile.readLine()); ^ ReadFromFile.java:27: unreported exception java.io.IOException; must be caught o r declared to be thrown rafFile.close(); ^ 7 errors
import java.io.*; class ReadFromFile { public static void main(String args[]) { System.out.println("Please enter a directory that the file located in:"); StringBuffer stfDir = new StringBuffer(); try{ char ch1; char ch2; while((ch1 = (char)System.in.read())!='\n') { stfDir.append(ch1); } File dir = new File(stfDir.toString()); System.out.println("Please enter a filename that want to read:"); StringBuffer stfFilename = new StringBuffer(); while((ch2 = (char)System.in.read())!='\n') { stfFilename.append(ch2); } File readFrom = new File(dir,stfFilename.toString()); 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("File cann't be read!"); }catch(Exception e){ } } }
String没有appendchar的方法。
/ReadFromFile.java
import java.io.*;
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 ch1;
char ch2;
while((ch1 = (char)System.in.read())!='\n')
{
stfDir.appendchar(ch1);
}
File dir = new File(stfDir.toString()); System.out.println("Please enter a filename that want to read:");
StringBuffer stfFilename = new StringBuffer();
while((ch2 = (char)System.in.read())!='\n')
{
stfFilename.append(ch2);
}
File readFrom = new File(dir,stfFilename.toString());
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("File cann't be read!");
}
}运行JAVAC ReadFromFile.java
系统显示stfDir.appendchar(ch1)有错。请问怎么修改?谢谢
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 ch1;
char ch2;
while((ch1 = (char)System.in.read())!='\n')
{
stfDir.append(ch1);
}
File dir = new File(stfDir.toString()); System.out.println("Please enter a filename that want to read:");
StringBuffer stfFilename = new StringBuffer();
while((ch2 = (char)System.in.read())!='\n')
{
stfFilename.append(ch2);
}
File readFrom = new File(dir,stfFilename.toString());
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("File cann't be read!");
}
}
感谢您多次的帮助。我刚才把上面这段程序运行了一遍。系统仍然显示以下错误:
while((ch2 = (char)System.in.read())!='\n')
^
ReadFromFile.java:24: unreported exception java.io.FileNotFoundException; must b
e caught or declared to be thrown
RandomAccessFile rafFile =new RandomAccessFile(readFrom,"rw");
^
ReadFromFile.java:25: unreported exception java.io.IOException; must be caught o
r declared to be thrown
while(rafFile.getFilePointer()<rafFile.length())
^
ReadFromFile.java:25: unreported exception java.io.IOException; must be caught o
r declared to be thrown
while(rafFile.getFilePointer()<rafFile.length())
^
ReadFromFile.java:26: unreported exception java.io.IOException; must be caught o
r declared to be thrown
System.out.println(rafFile.readLine());
^
ReadFromFile.java:27: unreported exception java.io.IOException; must be caught o
r declared to be thrown
rafFile.close();
^
7 errors
class ReadFromFile
{
public static void main(String args[])
{
System.out.println("Please enter a directory that the file located in:"); StringBuffer stfDir = new StringBuffer();
try{
char ch1;
char ch2;
while((ch1 = (char)System.in.read())!='\n')
{
stfDir.append(ch1);
}
File dir = new File(stfDir.toString()); System.out.println("Please enter a filename that want to read:");
StringBuffer stfFilename = new StringBuffer();
while((ch2 = (char)System.in.read())!='\n')
{
stfFilename.append(ch2);
}
File readFrom = new File(dir,stfFilename.toString());
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("File cann't be read!");
}catch(Exception e){
}
}
}