我怎样才能阅读电影名称? [关闭]

问题描述 投票:2回答:6

我有这样的数据

1|Toy Story (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Toy%20Story%20(1995)|0|0|0|1|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0

2|GoldenEye (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?GoldenEye%20(1995)|0|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0

3|Four Rooms (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Four%20Rooms%20(1995)|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0

4|Get Shorty (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Get%20Shorty%20(1995)|0|1|0|0|0|1|0|0|1|0|0|0|0|0|0|0|0|0|0

5|Copycat (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Copycat%20(1995)|0|0|0|0|0|0|1|0|1|0|0|0|0|0|0|0|1|0|0

并假设链接部分与电影名称部分在同一行。我是

只对最左边部分的电影号码和电影名称感兴趣。

如何在Java中读取此文件并返回如下:

1|Toy Story

2|GoldenEye

感谢您提前帮助。

java java.util.scanner bufferedreader readfile
6个回答
1
投票

非常简单,只需拆分“(”并记住使用\\逃脱它。

public static void main(String[] args) {

        String result = movie("1|Toy Story (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Toy%20Story%20(1995)|0|0|0|1|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0");
        System.out.println(result);   //prints 1|Toy Story
    }

    public static String movie(String movieString){
        return movieString.split(" \\(")[0];
    }

1
投票

您可以使用正则表达式来提取所需的部件。假设电影标题仅包含单词字符或空格。

List<String> movieInfos = Arrays.asList(
        "1|Toy Story (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Toy%20Story%20(1995)|0|0|0|1|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0",
        "2|GoldenEye (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?GoldenEye%20(1995)|0|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0",
        "3|Four Rooms (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Four%20Rooms%20(1995)|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0",
        "4|Get Shorty (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Get%20Shorty%20(1995)|0|1|0|0|0|1|0|0|1|0|0|0|0|0|0|0|0|0|0",
        "5|Copycat (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Copycat%20(1995)|0|0|0|0|0|0|1|0|1|0|0|0|0|0|0|0|1|0|0"
);

Pattern pattern = Pattern.compile("^(\\d+)\\|([\\w\\s]+) \\(\\d{4}\\).*$");

for (String movieInfo : movieInfos) {

    Matcher matcher = pattern.matcher(movieInfo);
    if (matcher.matches()) {
        String id = matcher.group(1);
        String title = matcher.group(2);

        System.out.println(String.format("%s|%s", id, title));
    } else {
        System.out.println("Unexpected data");
    }

}

0
投票

仅当您具有所有格式化的行时,此方法才有效。

private static final String FILENAME =“pathToFile”;

public static void main(String[] args) {

    BufferedReader br = null;
    FileReader fr = null;
    ArrayList<String> output = new ArrayList<>();

    try {

        //br = new BufferedReader(new FileReader(FILENAME));
        fr = new FileReader(FILENAME);
        br = new BufferedReader(fr);

        String currentLine;

        while ((currentLine= br.readLine()) != null) {
            String movie = currentLine.split(" \\(")[0];
            output.add(movie);
        }

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        try {

            if (br != null)
                br.close();

            if (fr != null)
                fr.close();

        } catch (IOException ex) {

            ex.printStackTrace();

        }

    }

}

0
投票

考虑到文件格式与您给出的相同,逐行读取文件,对于每个读取行,将其拆分为“(”括号并打印分割操作后获得的结果数组中的第一个索引。

static void readMovieNamesFromFile(String fileName)  {
    try (BufferedReader br = new BufferedReader(new FileReader(new File(fileName)))) {
        String line;
        while( (line = br.readLine()) != null){
            System.out.println((line.split("\\(")[0]).trim());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

0
投票

假设您正在阅读t.txt

File file = new File("t.txt");    
        try {
            Scanner in = new Scanner(file);
            while(in.hasNextLine())
            {

                String arr[] = in.nextLine().split("\\|");
                if(arr.length > 1)
                {
                    System.out.println(arr[0] +"|"+arr[1].split("\\(")[0]);
                    System.out.println();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

会给你一个输出

1|Toy Story 

2|GoldenEye 

3|Four Rooms

4|Get Shorty 

5|Copycat

你需要注意两件事。 (这里我们假设我们正在阅读第一行)

  1. 拆分|。现在自|是一个必须用来逃避它的元字符。因此in.nextLine().split("\\|");
  2. 现在arr [0]将包含1,而arr [2]将包含玩具总动员(1995)。因此我们将arr [2]拆分为“(”。你需要第一次匹配,因此你可以把它写成arr[1].split("\\(")[0])(你再次必须将它作为“(”也是一个元字符)来逃避它。

PS:if(arr.length > 1)这一行是为了避免空白的新行,这样你就不会得到ArrayIndexOutOfBoundsException。


-1
投票

您可以将数据保存在String中

例如

String name =  //data of move

然后使用if with char

for(int i =0;i<name.lenght;i++)
   {
       if(name.charat(i).equals("(")  //will read when it catch ( after name it will stop 
            {Break;}
            Else 
               System.out.print("name.charat(i);
 }

你也可以通过其他方式修复

© www.soinside.com 2019 - 2024. All rights reserved.