0

我有一个名为 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());

    }
}
4

2 回答 2

0

看看我前一段时间发布的我自己的问题。我最终使用了 Apache Lucene 的分词器。

这是你如何使用它(从这里复制):

    TokenStream tokenStream = analyzer.tokenStream(fieldName, reader);
    OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);
    CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);

    while (tokenStream.incrementToken()) {
        int startOffset = offsetAttribute.startOffset();
        int endOffset = offsetAttribute.endOffset();
        String term = charTermAttribute.toString();
    }
于 2013-04-17T05:38:51.177 回答
0

仅当文件为 doc 时,此代码才会返回 true。

    StringTokenizer st2 = new StringTokenizer(name, ".");

    if(st2.nextToken() == ".txt" || st2.nextToken() == ".doc")

如果文件名 file.txt 那么发生了什么

      (st2.nextToken() == ".txt") means ("file" == "txt") false
      (st2.nextToken() == ".doc") means ("txt" == "txt") false

第一个令牌将给出文件名第二个令牌将给出分机。

正确的代码是

     StringTokenizer st2 = new StringTokenizer(name, ".");
     String filename = st2.nextToken();
     String ext = st2.nextToken();
     if(ext.equalsIgnoreCase(".txt") || ext.equalsIgnoreCase(".txt"))

使用 always equals 比较字符串 not ==

于 2013-04-17T05:40:39.807 回答