bufferedReader不打印文件

时间:2013-07-28 16:44:39

标签: printing split bufferedreader

我正在尝试从.rtf文件打印,但似乎我的if语句已被破坏。它似乎分裂不起作用'因为如果我放置打印状态输出刚出来作为第一个打印行,它告诉用户他们正在搜索哪首歌,然后是未格式化的歌词。除此之外,代码只是循环遍历while循环。它似乎在点击if(line.contains(song)){时找不到该歌曲。现在,我只是硬编码文件的位置,但是当我开始工作时,我会让方法使用用户输入。

任何帮助都将不胜感激。

public static void main(String[] args){
     lyricsSearch("/Users/adam/Documents/Final/BlackDahliaMurder/", "miasma.rtf");
}

public static void lyricsSearch(String artist, String song){
    try {
        String stringSearch = song;
        // Opens the album file as a buffered reader
        BufferedReader bf = new 
        BufferedReader(new FileReader("/Users/adam/Documents/Final/BlackDahliaMurder/miasma.rtf"));

        // Let the user know what we are searching for
        System.out.println("Searching for " + song + "...");

        // Loop through each line, parsing.
        String line;

        while (( line = bf.readLine()) != null){
            System.out.println(line);
            if (line.contains(song)){
                String[] songInfo = line.split("\\|");
                System.out.println(Arrays.toString(songInfo));
            }       
        }
        bf.close();
    }
    catch (IOException e) {
        System.out.println("IO Error Occurred: " + e.toString());
    }
}

2 个答案:

答案 0 :(得分:0)

lyricsSearch("/Users/adam/Documents/Final/BlackDahliaMurder/", "miasma.rtf");

查看lyricsSearch的签名,您似乎正在搜索名为“miasma.rtf”的歌曲 - 我想这不是您的意图。 :)

否则,代码看起来很好。

答案 1 :(得分:0)

文件组织必须正确。专辑中的所有歌曲都在一个文件中。歌曲必须以管道开始,每一行必须以另一根管道结束。关键是文件中没有任何换行符。

import java.util.Arrays;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Experiment {

private int artist;
private String album;
private String song;
private String aaa = "/Users/adam/Documents/Final/AngelsAndAirwaves/";
private String bdm = "/Users/adam/Documents/Final/BlackDahliaMurder/";
private String naf = "/Users/adam/Documents/Final/NakedAndFamous/";
private String awn = "/Users/adam/Documents/Final/AWOLNATION/";


    public static void lyricsSearch(int artist, String album, String song) {
        String userArtistChoice = null;
        album.toLowerCase();
        song.toLowerCase();
        try {

            if (artist == 0){
                userArtistChoice = "/Users/adam/Documents/Final/AngelsAndAirwaves/";
            }
            else if (artist == 1){
                userArtistChoice = "/Users/adam/Documents/Final/BlackDahliaMurder/";
            }
            else if (artist == 2){
                userArtistChoice = "/Users/adam/Documents/Final/NakedAndFamous/";
            }
            else if (artist == 3){
                userArtistChoice = "/Users/adam/Documents/Final/AWOLNATION/";
            }
            else
                System.out.println("that i'n't right....");

            String stringSearch = song;
            // Opens the album file as a buffered reader
            BufferedReader bf = 
                new BufferedReader(new FileReader(userArtistChoice + album + ".txt"));

            // Let the user know what we are searching for
            System.out.println("Searching for " + song + "...");

            String line;
            //loop through each line, parsing     
            while ((line = bf.readLine()) != null){
                //print the song.  each | delimits a string.  song stops at carriage return.
                if (line.toLowerCase().contains(song)){
                    String[] songInfo = line.split("\\|");
                    for(String el : songInfo) {
                    System.out.println(el);
                }

            }   
        }
        bf.close();
        }
        catch (IOException e) {
            System.out.println("IO Error Occurred: " + e.toString());
        }
    }
}