我有一个名为 File Location 的类,它存储文件的大小、名称、驱动器和目录。
该类应该将扩展名与文件名(“java”与“test.java”)分开,然后使用equals方法将其与另一个文件进行比较。尽管由于某种原因,它每次都返回错误。知道有什么问题吗?
类文件
import java.util.*;
public class FileLocation
{
private String name;
private char drive;
private String directory;
private int size;
public FileLocation()
{
drive = 'X';
directory = "OOProgramming\\Practicals\\";
name = "test";
size = 2;
}
public FileLocation(char driveIn, String dirIn, String nameIn, int sizeIn)
{
drive = driveIn;
directory = dirIn;
name = nameIn;
size = sizeIn;
}
public String getFullPath()
{
return drive + ":\\" + directory + name;
}
public String getFileType()
{
StringTokenizer st1 = new StringTokenizer(name, ".");
return "File type is " + st1.nextToken();
}
public String getSizeAsString()
{
StringBuilder data = new StringBuilder();
if(size > 1048575)
{
data.append("gb");
}
else if(size > 1024)
{
data.append("mb");
}
else
{
data.append("kb");
}
return size + " " + data;
}
public boolean isTextFile()
{
StringTokenizer st2 = new StringTokenizer(name, ".");
if(st2.nextToken() == ".txt" || st2.nextToken() == ".doc")
{
return true;
}
else
{
return false;
}
}
public void appendDrive()
{
StringBuilder st1 = new StringBuilder(drive);
StringBuilder st2 = new StringBuilder(directory);
StringBuilder combineSb = st1.append(st2);
}
public int countDirectories()
{
StringTokenizer stDir =new StringTokenizer(directory, "//");
return stDir.countTokens();
}
public String toString()
{
return "Drive: " + drive + " Directory: " + directory + " Name: " + name + " Size: " + size;
}
public boolean equals(FileLocation f)
{
return drive == f.drive && directory == f.directory && name == f.name && size == f.size;
}
}
测试程序
import java.util.*;
public class FileLocationTest
{
public static void main(String [] args)
{
Scanner keyboardIn = new Scanner(System.in);
FileLocation javaAssign = new FileLocation('X', "Programming\\Assignment\\", "Loan.txt", 1);
int selector = 0;
System.out.print(javaAssign.isTextFile());
}
}