哎,何止一个错啊!!
import java.io.*;
class WriteToFile
{
public static void main(String args[]) throws IOException 
{
System.out.println("Please enter a directory that the file located in:");
StringBuffer stfDir=new StringBuffer();
//while ((char ch = (char)System.in.read())!='\n')
char ch=(char)System.in.read();
while(ch!='\n') {
stfDir.append(ch);
ch=(char)System.in.read();
}
File dir=new File(stfDir.toString());
System.out.println("Please enter a filename that want to read:");
StringBuffer stfFilename=new StringBuffer();

//While ((char ch=(char)System.in.read())!='\n')
         ch=(char)System.in.read();
                while(ch!='\n')
{
stfFilename.append(ch);
ch=(char)System.in.read();
}
File readFrom=new File(dir,stfFilename.toString());
if (readFrom.isFile()&&readFrom.canWrite()&&readFrom.canRead())
{
//创建RandomAccessFile对象
RandomAccessFile rafFile=new RandomAccessFile(readFrom,"rw");
//如果未读到文件尾,则继续读取
int ch1=0;
StringBuffer stfContent=null;
while(ch1!=-1)
{
//字符缓冲区清空
stfContent.setLength(0);
//接收键盘输入,构造字符
while((ch1=System.in.read())!=-1)
stfContent.append(ch);
//附加Dos格式行结束标志
stfContent.append("\r\n");
//将字符缓冲区的内容写出
rafFile.writeBytes(stfContent.toString());
}
rafFile.close();
}
else
System.out.println("File cann't be write!");
}
}这样应该没有问题了

解决方案 »

  1.   

    哎,何止一个错啊!!
    import java.io.*;
    class WriteToFile
    {
    public static void main(String args[]) throws IOException 
    {
    System.out.println("Please enter a directory that the file located in:");
    StringBuffer stfDir=new StringBuffer();
    //while ((char ch = (char)System.in.read())!='\n')
    char ch=(char)System.in.read();
    while(ch!='\n') {
    stfDir.append(ch);
    ch=(char)System.in.read();
    }
    File dir=new File(stfDir.toString());
    System.out.println("Please enter a filename that want to read:");
    StringBuffer stfFilename=new StringBuffer();

    //While ((char ch=(char)System.in.read())!='\n')
             ch=(char)System.in.read();
                    while(ch!='\n')
    {
    stfFilename.append(ch);
    ch=(char)System.in.read();
    }
    File readFrom=new File(dir,stfFilename.toString());
    if (readFrom.isFile()&&readFrom.canWrite()&&readFrom.canRead())
    {
    //创建RandomAccessFile对象
    RandomAccessFile rafFile=new RandomAccessFile(readFrom,"rw");
    //如果未读到文件尾,则继续读取
    int ch1=0;
    StringBuffer stfContent=null;
    while(ch1!=-1)
    {
    //字符缓冲区清空
    stfContent.setLength(0);
    //接收键盘输入,构造字符
    while((ch1=System.in.read())!=-1)
    stfContent.append(ch);
    //附加Dos格式行结束标志
    stfContent.append("\r\n");
    //将字符缓冲区的内容写出
    rafFile.writeBytes(stfContent.toString());
    }
    rafFile.close();
    }
    else
    System.out.println("File cann't be write!");
    }
    }这样应该没有问题了