令牌不分离字符串? (JAVA)

时间:2013-04-17 05:32:15

标签: java string

我有一个名为文件的课程 存储文件大小,名称,驱动器和目录的位置。

该类应该将扩展名与文件名(“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());

    }
}

2 个答案:

答案 0 :(得分:0)

看看my own question我发布了一段时间。我最终使用了Apache Lucene的tokenizer。

以下是您使用它的方式(从here复制):

    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();
    }

答案 1 :(得分:0)

只有当文件是doc时,此代码才会生效。

    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

第一个令牌会给出文件名,第二个令牌会给出ext。

正确的代码是

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

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