我需要能够比较文件的条目 - Java

时间:2015-06-05 11:04:54

标签: java file compare

`import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;

公共课Dokimi {

private static String line;


public static void  main (String[] args) throws IOException


{

    int x = 0;
    BufferedReader br = new BufferedReader(new FileReader("src/film.txt"));
    line = br.readLine();

    String[] filmline = new String [1000];

    while (line != null) {
        line = br.readLine();
        filmline[x] = line;
        x++;

    }
    br.close();

    for (int i = 0; i<x; i++) // after many tries the last change I made is this. This is the testing class.
    {

        String [] arr = filmline[i].split(": ");


        if ( i == x-1) // I know it isn't the best, maybe not even good but I tried many things and had nothing to lose.

        {

            for ( String ss : arr) {

                   String test = ss;
                    if (test.equals("Dancing With The Dogs "))
                    {
                        System.out.println("gotcha!");

                    }

                }

        }
    }






}

}`所以,我有一个带有一些电影属性的文本文件。例如:

"film id :  1  film title :   Pirates Of Hawai  film category :   action ,      comedy   film description :  A pirate from Hawai drinks rum and goes on an adventure to find more rum." 

(一行中的每个条目),每次用户尝试添加新条目时,我必须确保电影尚未存在于文件中。我尝试了slpit方法(通过使用":"并删除“电影ID”等)和StringTokenizer但它只能在ONE上工作并由我指定行,而不是在循环中以便它可以读取整个文件。

2 个答案:

答案 0 :(得分:0)

不要使用它的遗产StringTokenizer,应该支持维护原因,但不能在新代码中实现。

考虑到令牌每次都不同,你可能想要遍历String并在这里和那里使用子串,也就是说,假设每一行包含相同的令牌。

或者,在以下位置更改您的输入:

  

&#34; 1 *海盗夏威夷动作,喜剧来自夏威夷的海盗喝朗姆酒和   寻找更多朗姆酒的冒险经历。&#34;

这样,所有令牌都是相同的,您将能够使用拆分方法。

答案 1 :(得分:0)

根据here

稍微改变你的行以添加&#34;:&#34;:

"film id :  1 : film title :   Pirates Of Hawai : film category :   action ,      comedy :  film description :  A pirate from Hawai drinks rum and goes on an adventure to find more rum." 

您可以尝试这种方法并与您的方法进行比较:(在添加之前使用existsfilm验证它是否已存在)

 public void showAllFilms(){
    ArrayList<String[]> films = getFilms();
    for(String[] film : films){
        System.out.println("id "+film[0]+"\ntitle "+film[1]);
    }
 }

 public existsFilm(String filmName){
     ArrayList<String[]> films = getFilms();
     for(String[] film : films){
         if(film[1].equals(filmName)){
           return true;
         }
     }
     return false;
 }

 public ArrayList<String[]> getFilms(){
    ArrayList<String[]> filmList = new ArrayList();
    int lineRead = 0;
    try{
        File file = new File("yourfile.txt");
        BufferedReader br = new BufferedReader(new FileReader(file)));
        String line;
        while ((line = br.readLine()) != null) {
           String[] data = line.split(":");
           if(data.length > 0){
               filmList.add(new String[]{data[1],data[3],data[5],data[7]});
           }
           lineRead++;
        }
    }catch(Exception ex){
        System.out.println("Error reading line "+lineRead);
        ex.printStackTrace(); //very ugly using this (common is logging it)
    }
    return filmList;
 }