【问题标题】:How can I modify my script to adding each line to an ArrayLList如何修改我的脚本以将每一行添加到 ArrayList
【发布时间】:2023-03-23 21:55:01
【问题描述】:

这是我的代码,我需要将我的 file.txt 的每一行添加到一个 ArrayList 中,稍后我将通过使用本体(本体已在脚本中加载)进行注释,因此当我尝试简单的短语时它可以工作,例如:

//ar.add("军事攻击");

但是当我想使用文件中的行时,我正在伤害一堵墙?请你 指导我

package javaapplication1;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.*;
import org.semanticweb.owlapi.io.*;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.model.OWLClass; 
import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; 
import org.semanticweb.owlapi.util.SimpleIRIMapper;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * @author ekelodjo
 */
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */

    public static String getWord(String annotation)
    { 

        int index = annotation.indexOf("*");

        return annotation.substring(0,index); 

    }

        public static String getConcept(String annotation)
    { 

        int index = annotation.indexOf("*");
        int lIndex = annotation.lastIndexOf("*");             
        return annotation.substring(index+1, lIndex); 

    }

      public static String getType(String annotation)
    { 
        int index = annotation.lastIndexOf("*");             
        return annotation.substring(index+1, annotation.length()); 

    }


    public static void main(String[] args) {
        // TODO code application logic here
        //lecture de fichier 

            int compteur = 0;
            BufferedReader br;
            String line;

            try {
            br = new BufferedReader(new FileReader(new 
            File("D:/ekelodjo/OntoTest/CorpusTexte.txt")));
            while ((line = br.readLine()) != null) {
            //line = br.readLine();
            System.out.println("line :"+line);
            compteur++;
            }
            br.close();
            }catch(FileNotFoundException e){
             e.printStackTrace();
            }catch(IOException e){
             e.printStackTrace();
            }     
            // Test annotator class 
            Annot annot = new Annot(); 
            annot.loadOnto("D:/ekelodjo/OntoTest/2EME_ONTO.owl");
            //annot.loadOnto("D:/ekelodjo/OntoTest/Test1.owl"); 
            //Annot annot =  new Annot();

            OWLOntology onto = annot.getOnto();   
            //OWLOntology onto2 = annot2.getOnto();  
            ArrayList<String> ar= new ArrayList<String>(24);

            //ar.add("vehicle");
            //ar.add("military");
            //ar.add(" Vehicles autos person bus car1");
            //ar.add("car car1 vehicle military civilian person");
            //ar.add("possibly car1 vehicle military civilian person");
            //ar.add("vehicle military civilian person");
            //ar.add(" military  attack");
            //ar.add("normally ");
            //ar.add("Although Nothing Yet but");


            ar.add(line);
            ArrayList<String> list = annot.getAnnot(ar, onto);
            System.out.println("liste des annotations :" +list.toString());
            for (int i = 0; i < list.size(); i++) {
            String ann = (String) list.get(i);
             String word = getWord(ann);
            String concept = getConcept(ann);
            String type = getType(ann);
            System.out.println("word = " +word);
            System.out.println("concept = " +concept);
            System.out.println("type = " +type);
            System.out.println("  ");
            }                          
            }
            }

【问题讨论】:

  • "...我正在伤害 [撞] 墙?"是对问题的可怕描述。您的具体问题是什么?
  • 可怜的墙... ;(
  • 太棒了,谢谢你的提示!
  • @Carcigenicate 我尝试了多种方法将我的 file.txt 行添加到我的 arrayList 以便我以后可以使用它来注释它

标签: java string arraylist ontology


【解决方案1】:

如果您只需要将文本文件加载到您的 ArrayList 对象中,那么您几乎已经完成了,只需将 ArrayList ar 定义移动到您的 try/catch 块之前,然后在您的 while 循环中添加该行阅读它。

还将br.close 移动到finally

ArrayList<String> ar = new ArrayList<String>();

try {
    br = new BufferedReader(new FileReader(new File("D:/ekelodjo/OntoTest/CorpusTexte.txt")));
    while ((line = br.readLine()) != null) {
        ar.add(line);
    }
} catch (FileNotFoundException e) {
} catch (IOException e) {
} finally {
    try {
        br.close();
    } catch (IOException e) {
    }
}

【讨论】:

    【解决方案2】:

    你可以试试这个

        ArrayList<String> ar = new ArrayList<String>();
    
        try {
            br = new BufferedReader(new FileReader(new File(
                    "D:/ekelodjo/OntoTest/CorpusTexte.txt")));
            while ((line = br.readLine()) != null) {
                // line = br.readLine();
                System.out.println("line :" + line);
                ar.add(line);
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 2011-10-28
      相关资源
      最近更新 更多