簡體   English   中英

拆分多行分隔符

[英]Split multi line delimiter

嘗試拆分包含有關具有不同格式的專輯的信息的文本文件。 每張專輯都由虛線分隔,每個部分的第一行包含有關專輯的信息,用冒號分隔,后面的行是專輯中的曲目。

1:Whatever People Say I Am That's What I'm Not:Arctic Monkeys:2006:1.95M
The View from the Afternoon (3:38)
I Bet You Look Good on the Dancefloor (2:53)
----------------------------------------------------------------------------------
2:Different Class:Pulp:1996:1.33M
Mis-Shapes (3:46)
Pencil Skirt (3:11)
----------------------------------------------------------------------------------
20:Konnichiwa:Skepta:2016:207K
Konnichiwa (3:16)
Lyrics (2:36)
----------------------------------------------------------------------------------

我需要幫助將其拆分為專輯 object。 我制作了專輯 class:

    //Album attributes
    private String salesRanking;
    private String title;
    private String artistName;
    private String yearRelease;
    private String sales;
    private String [] tracks; //Array of string to store each track in album

    //Album object constructor
    public Album (String salesRanking, String title, String artistName, String yearRelease, String sales, String [] tracks){
        this.salesRanking = salesRanking;
        this.title = title;
        this.artistName = artistName;
        this.yearRelease = yearRelease;
        this.sales = sales;
        this.tracks = tracks;
    }

拆分文本文件並將內容放入專輯 object 是我遇到問題的地方。 我試過了

    //Store album objects
    ArrayList <String []> bulkAlbum = new ArrayList<>();


        try{
            //Read file
            FileReader freader = new FileReader(albumData);
            BufferedReader breader = new BufferedReader(freader);
            
            String line;
            
            while((line=breader.readLine()) != null ){
                //Removes empty array
                if(line.startsWith("-")){
                    continue;
                }

                //Split each album entry
                String [] albumDetail = line.split("-");
                bulkAlbum.add(albumDetail);
                line = "";

            }
            breader.close();
            freader.close();
        
        } catch(IOException ex) {
            System.out.println(ex);
        }

當我運行代碼時,output 在 arrayList 中將每一行顯示為它自己的數組,我意識到我犯的錯誤是因為它只能逐行讀取,但我不知道 Z34D1F91FB2E514B86BZFAB 文件從哪里到這里被整體讀取,然后通過虛線拆分或拆分為塊以創建專輯 object。

由於當時的冒號,連接行然后使用“:”拆分不會起作用。 我一直在思考邏輯,但不知道還能嘗試什么。


編輯 - 最后借助以下方法解決了這個問題: 如何將文本文件拆分為對象 java

你可以試試這個——

假設您將負責解析和讀取文件,則實現它的邏輯部分。

    static class Block {
        String firstLine;
        List<String> tracks;

        public Block(String firstLine, List<String> tracks) {
            this.firstLine = firstLine;
            this.tracks = tracks;
        }
    }

    public static void main(String[] args) {
        List<Block> blocks = new ArrayList<>();
        try{
            //Read file
            FileReader freader = new FileReader("../");
            BufferedReader breader = new BufferedReader(freader);
            List<String> lines = (List<String>) breader.lines();
            int i = 0;
            while(i < lines.size()){
                List<String> addLines = new ArrayList<>();
                if(lines.get(i).startsWith("-")){
                    i++;
                } else {
                    while(!lines.get(i).startsWith("-")){
                        addLines.add(lines.get(i));
                        i++;
                    }
                    String firstLine = addLines.get(0);
                    addLines.remove(0);
                    Block block = new Block(firstLine,addLines);
                    blocks.add(block);
                }
            }
            breader.close();
            freader.close();

        } catch(IOException ex) {
            System.out.println(ex);
        }

        List<Album> albums = new ArrayList<>();

        for (Block block: blocks) {
            //Implement these Methods by yourself
            parseFirstLine(block.firstLine);
            parseListOfTracks(block.tracks);
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM