Java程序使用或覆盖已弃用的API

Java程序使用或覆盖已弃用的API

本文介绍了Java程序使用或覆盖已弃用的API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个程序,编译并运行在Netbeans和Eclipse没有任何问题。但是当我尝试在命令行中编译它:

I have written a program that is compiled and run in Netbeans and Eclipse without any problem. But when I try to compile it in command line via:

javac -classpath .:lucene-core-3.4.0.jar Indexer.java

我得到错误:

Note: Indexer.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.



当我尝试使用-Xlint:deprecation选项进行编译时,我得到:

When I try to compile with -Xlint:deprecation option I get:

javac -classpath .:lucene-core-3.4.0.jar Indexer.java -Xlint:deprecation
Indexer.java:14: warning: [deprecation] org.apache.lucene.index.IndexWriter.MaxFieldLength in org.apache.lucene.index.IndexWriter has been deprecated
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
                                          ^
1 warning

我的源代码似乎是纯的,没有任何问题!任何人都可以帮助我在命令行中编译和运行它。

My source code seems to be pure and without any problem! can anyone help me compile and run it in command line?

代码

import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Document;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.lucene.index.TermFreqVector;

public class Indexer {

    public static void main(String[] args) throws Exception {
        // TODO code application logic here

        //Indexing begins
            String indexDir;
            String dataDir;
            if (args.length != 2) {
                    dataDir = new String("/home/norc/Ranking/Hamshahri/Data/");
                    indexDir = new String("/media/5E62D9BD62D99A5B/indexes/");
            //throw new IllegalArgumentException("Usage: java " + Indexer.class.getName() + " <index dir> <data dir>");
        }
                else {
                    dataDir = args[0];
                    indexDir = args[1];
                }
        long start = System.currentTimeMillis();
        Indexer indexer = new Indexer(indexDir);
        int numIndexed;
        try {
            numIndexed = indexer.index(dataDir, new TextFilesFilter());
        } finally {
            indexer.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("Indexing " + numIndexed + " files took " + (end - start) + " milliseconds");

    }

    private IndexWriter writer;

    @SuppressWarnings("deprecation")
    public Indexer(String indexDir) throws IOException {
        Directory dir = FSDirectory.open(new File(indexDir));
        writer = new IndexWriter(dir,
             new StopAnalyzer(Version.LUCENE_34, new File("/home/norc/Ranking/Hamshahri/stopwords.txt")),
             true,
             IndexWriter.MaxFieldLength.UNLIMITED);
    }

    public void close() throws IOException {
        writer.close();
    }

    public int index(String dataDir, FileFilter filter) throws Exception {
        File[] dires = new File(dataDir).listFiles();
        for (File d: dires) {
            if (d.isDirectory()) {
            File[] files = new File(d.getAbsolutePath()).listFiles();
            for (File f: files) {
                if (!f.isDirectory() &&
                !f.isHidden() &&
                f.exists() &&
                f.canRead() &&
                (filter == null || filter.accept(f))) {
                    indexFile(f);
                }
            }
            }
        }
        return writer.numDocs();
    }

    private static class TextFilesFilter implements FileFilter {
        public boolean accept(File path) {
            return path.getName().toLowerCase().endsWith(".txt");
        }
    }


    protected Document getDocument(File f) throws Exception {
        Document doc = new Document();
        if (f.exists()) {
        doc.add(new Field("contents", new FileReader(f), Field.TermVector.YES));
        doc.add(new Field("path", f.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
        String cat = "WIR";
        cat = f.getAbsolutePath().substring(0, f.getAbsolutePath().length()-f.getName().length()-1);
        cat = cat.substring(cat.lastIndexOf('/')+1, cat.length());
        //doc.add(new Field("category", cat.subSequence(0, cat.length()), Field.Store.YES));
        //System.out.println(cat.subSequence(0, cat.length()));
        }
        return doc;
    }

    private void indexFile(File f) throws Exception {
        System.out.println("Indexing " + f.getAbsolutePath());
        Document doc = getDocument(f);
        writer.addDocument(doc);
    }

}


推荐答案

org.apache.lucene.index.IndexWriter.MaxFieldLength 已弃用,不应再使用,请参阅

org.apache.lucene.index.IndexWriter.MaxFieldLength is deprecated and should not be used any more, see https://lucene.apache.org/core/old_versioned_docs/versions/3_4_0/api/all/org/apache/lucene/index/IndexWriter.MaxFieldLength.html#IndexWriter.MaxFieldLength%28int%29

这只是一个警告。您可以使用它,但不建议这样做。

It's just a warning. You may use it, but it's not recommended.

这篇关于Java程序使用或覆盖已弃用的API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 11:57