关于对象的问题
import java.io.*;
public class FileOperation
{
public static void main(String []args)
{
/** InputStreamReader is a class from change bye stream to char stream */
/** Read text and deposit character in person who buffer in order to be able to offer from
character introduction character , array , line segment high-efficient to read in flowing */
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String sdir;
String sfile;
System.out.print("please input a path:");
sdir=in.readLine();
File Fdir=new File(sdir);//use the absolutely path to create Object of File;
while(sdir!="#")
{
//File Fdir=new File(sdir);//use the absolutely path to create Object of File;
if(Fdir.exists() && Fdir.isDirectory())
{
System.out.println("You enter right,go--go--go");
System.out.println("There is a directory:"+sdir+" exists");
for(int i=0;i<Fdir.list().length;i++)
{
System.out.println(Fdir.list()[i]);
}
File Fdirother= new File(sdir+"\\books");
if(!Fdirother.exists()) //if the not exists,create it
{
Fdirother.mkdir();
System.out.println();
System.out.println("Now the new list after create a dir,please check the list.");
}
else
{
System.out.println("The catalogue that you input has already been established!");
}
for(int i=0;i<Fdir.list().length;i++)
{
System.out.println(Fdir.list()[i]);
}
}
else
{
System.out.println("Not Found the Directory!");
System.out.println("You Enter the Path is:"+Fdir.getPath()+"please check it");
System.out.print("Please input the path aggin:");
sdir=in.readLine();
File Fdir=new File(sdir);
//System.out.print("please input a path:");
//System.out.println("In Path:"+Fdirother.getPath());
}
}
sfile=in.readLine();
// File Ffile= new File(Fdir,sfile);
while(sfile!="#")
{
File Ffile= new File(Fdir,sfile);
if(Ffile.isFile())
{
System.out.println("File:"+Ffile.getName()+"\nIn Path:"+Ffile.getPath()+"\nFile length is:"+Ffile.length());
System.exit(0);
}
else
{
System.out.println("Not Found the file!");
sfile=in.readLine();
}
}
}
/** end try */
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
当第一次出现路径输入错误后,第2次在重新输入,创建的对象,从新获取参数,但我不知道怎么弄,就 在else 中加了2条语句:
sdir=in.readLine();
File Fdir=new File(sdir);
但编译的时候报出Fdir已经定义过的错误
我想实现的功能就是输入的路径字符串判断是不是结束符,在判断文件或目录是否存在,如果不存在,在重新输入路径目录。直到是真确的路径。在执行后面的步骤。