skip to Main Content

I am trying to extract a data from a raw text file in my raw directory by reading it and getting the string after the colon preceded by a category and storing it in a class variable. The raw text file is as follows:

category:AI
photo:ai4.png
title:'I will destroy humans': Humanoid AI robot Sophia gets Saudi citizenship
website:https://www.deccanchronicle.com/lifestyle/viral-and-trending/111117/i-will-destroy-humans-humanoid-ai-robot-sophia-gets-saudi-citizenship.html
date:11-11-2017
category:cybersecurity
photo:cyber3.png
title:WhatsApp and Telegram media files aren't so secure
website:https://www.theverge.com/2019/7/15/20692184/whatsapp-telegram-media-files-android-messaging-encryption
date:15-07-2019

I want to be able to read the first line and check what category it is, if the category is photo, I want to store the ai4.png in the class variable related to it. After it reaches date and the date data is stored in the date class variable. It will instantiate a new class and begin the process again with a new class.

The class is as follows:

public class Link{
    private String title;
    private String imageName;
    private String url;

    Link(String t,String i,String u){
        this.title=t;
        this.imageName=i;
        this.url=u;
    }
    public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getImageName() {
            return imageName;
        }

        public void setImageName(String imageName) {
            this.imageName = imageName;
        }
    public String getURL(){
        return url;
    }
    public void setURL(String url){
        this.url = url;
    }

}

So by reading the text file, if it is title preceded by colon, it will assign the title text after the colon to the title class variable.

Sorry, I am new to android and Java in general, any help is appreciated. Thanks for reading!

2

Answers


  1. Chosen as BEST ANSWER

    I have found a solution.

            InputStream input = getResources().openRawResource(R.raw.news_items);
            String _final="";
            Scanner scanner = new Scanner(input);
            while(scanner.hasNext()){
                String line = scanner.nextLine();
    
                String[] pieces = line.split(":",2);
                _final+=pieces[1]+"n";
            }
    

    First, I get the raw text file from the raw directory and store it under InputStream input variable.

    After that,I pass the InputStream variable into a Scanner variable to read it as a text file to my understanding, please correct me if I am wrong!

    In the while loop, while there it isn't end-of-file (EOF), get the line of text. Split it at the colon(:), and since I want to get Web URL's also, split at the first instance of colon hence the 2 in .split(":",2).

    Lastly, append it the _final text variable, this is purely for testing sake. I can modify it to pass these variables into class variables with some effort.


  2. As I see it, your problem consists of 2 parts.

    First one is to read the file from Android storage and
    Second is to read the text and parse into an array/list of class objects.

    For the first part, I would recommend looking at the following stackoverflow answer:
    How can I read a text file in Android?

    As for the second part, use the following code snippet:

        String fileContents = "category:AIn" +
                "photo:ai4.pngn" +
                "title:'I will destroy humans': Humanoid AI robot Sophia gets Saudi citizenshipn" +
                "website:https://www.deccanchronicle.com/lifestyle/viral-and-trending/111117/i-will-destroy-humans-humanoid-ai-robot-sophia-gets-saudi-citizenship.htmln" +
                "date:11-11-2017n" +
                "category:cybersecurityn" +
                "photo:cyber3.pngn" +
                "title:WhatsApp and Telegram media files aren't so securen" +
                "website:https://www.theverge.com/2019/7/15/20692184/whatsapp-telegram-media-files-android-messaging-encryptionn" +
                "date:15-07-2019";
        String[] arrayOfContents = fileContents.split("n");
        List<Link> arrayOfLinks = new ArrayList<>();
        boolean isFirstSection = true;
        Link link = new Link();
    
        for (String content : arrayOfContents) {
            String[] splitVal = content.split(":");
    
            switch (splitVal[0].toLowerCase()) {
                case "category":
                    if (isFirstSection) {
                        isFirstSection = false;
                    } else {
                        arrayOfLinks.add(link);
                    }
                    link = new Link();
                    link.setCategory(splitVal[1]);
                    break;
                case "date":
                    link.setDate(splitVal[1]);
                    break;
                case "photo":
                    link.setPhoto(splitVal[1]);
                    break;
                case "title":
                    link.setTitle(splitVal[1]);
                    break;
                case "website":
                    link.setWebsite(splitVal[1]);
                    break;
                default:
                    break;
            }
        }
        arrayOfLinks.add(link);
    

    fileContents variable will actually have the text file contents that you read from the storage.

    Also, below is the Class Link which will hold all the data in the end:

    public class Link {
    
    private String Category;
    private String Photo;
    private String Title;
    private String Website;
    private Date Date;
    
    public Link() {
    }
    
    public void setCategory(String category) {
        Category = category;
    }
    
    public void setPhoto(String photo) {
        Photo = photo;
    }
    
    public void setTitle(String title) {
        Title = title;
    }
    
    public void setWebsite(String website) {
        Website = website;
    }
    
    public void setDate(String date) {
        try {
            Date = new SimpleDateFormat("dd-MM-yyyy").parse(date);
        } catch (Exception e) {
            Date = null;
        }
    }}
    

    In the end, you will have the complete data in arrayOfLinks variable.

    Hope this solves your query.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search