public void ioTest() throws IOException
{
System.out.println("Please enter a directory that the file located in:");
StringBuffer strDir = new StringBuffer();
char ch;
while((ch=(char)System.in.read())!='\n')
{
strDir.append(ch);
}
String strDirString = strDir.toString();
System.out.println(strDirString);
File dir = new File(strDirString);
System.out.println("Please enter a filename that want to read:");
StringBuffer stfFileName = new StringBuffer();
char c;
while((c=(char)System.in.read())!='\n')
{
stfFileName.append(c);
}
String stfStr =stfFileName.toString();
System.out.println(stfStr);
File readFrom = new File(dir,stfStr);//为什么这个文件创建不成功?
System.out.println("readFrom "+readFrom.isFile());//此处返回false 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("The file can not read");
}import java.io.*;
public class Test {
public static void main(String[] args) {
File fdir = new File("D:\\Test");
File f = new File(fdir,"E.txt");
System.out.println(f.isFile());
}
}
但是这样就可以创建成功
其中在dos窗口输入的字符串同样是"D:\\Test"和"E.txt"还是不行
if(!dir.isDirectory()){
dir.mkdir();//or dir.mkdirs();
}2,file
if(!file.exists()){
file.createNewFile();
}3,File Constructor Only Create File instance,not a real file!