当前位置: 技术问答>java相关
关于一个String与StringBuffer的非常奇怪的问题(50分求解):
来源: 互联网 发布时间:2017-03-11
本文导语: 请看以下代码: import java.io.*; class ReadFromFile{ public static void main(String args[])throws IOException{ System.out.println("Please enter a directory that the file located in:"); //构造待读文件的目录 StringBuffe...
请看以下代码:
import java.io.*;
class ReadFromFile{
public static void main(String args[])throws IOException{
System.out.println("Please enter a directory that the file located in:");
//构造待读文件的目录
StringBuffer stfDir=new StringBuffer();
//从键盘获取输入字符,存储进入字符缓冲区
char ch;
while((ch=(char)System.in.read())!='n'){
stfDir.append(ch);
}
//创建目录文件对象
String str;
//str=stfDir.toString();
File dir=new File("C:/Classes");
System.out.println(dir.exists());
System.out.println("Please enter a filename that want to read:");
//获取待读取的文件名
StringBuffer stfFilename=new StringBuffer();
//从键盘获取输入字符,存储进入字符缓冲区
while((ch=(char)System.in.read())!='n'){
stfFilename.append(ch);
}
//创建文件对象
****String str2=new String(stfFilename);
****System.out.println(str2);
****File readFrom=new File(dir,str2);
System.out.println(readFrom.exists());
//判断文件是否为目录,是否具有写权限,读权限
if(readFrom.isFile()&&readFrom.canRead()&&readFrom.canWrite()){
//创建RandomAccessFile对象
RandomAccessFile rafFile=new RandomAccessFile(readFrom,"rw");
//如果未读到文件尾,则继续读取
while(rafFile.getFilePointer()
import java.io.*;
class ReadFromFile{
public static void main(String args[])throws IOException{
System.out.println("Please enter a directory that the file located in:");
//构造待读文件的目录
StringBuffer stfDir=new StringBuffer();
//从键盘获取输入字符,存储进入字符缓冲区
char ch;
while((ch=(char)System.in.read())!='n'){
stfDir.append(ch);
}
//创建目录文件对象
String str;
//str=stfDir.toString();
File dir=new File("C:/Classes");
System.out.println(dir.exists());
System.out.println("Please enter a filename that want to read:");
//获取待读取的文件名
StringBuffer stfFilename=new StringBuffer();
//从键盘获取输入字符,存储进入字符缓冲区
while((ch=(char)System.in.read())!='n'){
stfFilename.append(ch);
}
//创建文件对象
****String str2=new String(stfFilename);
****System.out.println(str2);
****File readFrom=new File(dir,str2);
System.out.println(readFrom.exists());
//判断文件是否为目录,是否具有写权限,读权限
if(readFrom.isFile()&&readFrom.canRead()&&readFrom.canWrite()){
//创建RandomAccessFile对象
RandomAccessFile rafFile=new RandomAccessFile(readFrom,"rw");
//如果未读到文件尾,则继续读取
while(rafFile.getFilePointer()