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
I have found a solution.
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.
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:
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:
In the end, you will have the complete data in arrayOfLinks variable.
Hope this solves your query.