-2

I am developing an android app and i need it to read a text file. Once it has read the text file I need to save certain parts to a database. The text file contains the following:

Title - Hello
Date - 03/02/1982
Info - Information blablabla

Title - New title
Date - 04/05/1993
Info - New Info

I thought that I need to split the text file in two by using the blank line as a separator. Then I need to get the individual info like the Title and save it into the database as a title. Is there some way to do this? I know how to read all of the text file. I am using this to read the complete text file.

    TextView helloTxt = (TextView) findViewById(R.id.hellotxt);
    helloTxt.setText(readTxt());

}

private String readTxt() {

    InputStream inputStream = getResources().openRawResource(R.raw.hello);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i;
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return byteArrayOutputStream.toString();
}

I was just wondering about the splitting event of this. Thank you

Dion Segijn
  • 2,625
  • 4
  • 24
  • 41
S Lateffa
  • 49
  • 4
  • This is a project I am working on with friends, we are using txt files – S Lateffa Dec 17 '12 at 15:31
  • 2
    But you understand what user1810737 tried to tell you? You create more structure by using xml files, and there are enough library's that can be used to read those xml files and assign variables like: Title, Date and Info. – Dion Segijn Dec 17 '12 at 15:35
  • 1
    Hi, Yes I understand what they are saying, and I thank you both for your help. I need to read it from a txt file though. – S Lateffa Dec 17 '12 at 15:38
  • 2
    So basically, you want someone to tell you how to do something that there's a better way to do, which you haven't even shown an attempt at doing on your own. That's ... really not a question on SO. – Brian Roach Dec 17 '12 at 15:40

3 Answers3

2

So if i understand your question right, you seek for something that splits your text at the newline. So you can take both of the information fields and put them into a textview.

Look at this post: Split Java String by New Line This will show you how to split the text in lines.

And if you want to split the text without empty lines use:

String.split("[\\r\\n]+")

This was also described by an other user in the same thread.

Goodluck,
Daniel

Community
  • 1
  • 1
Dion Segijn
  • 2,625
  • 4
  • 24
  • 41
0

You can use regular expression with Pattern and Matcher :

    String inputString = "Title - Hello\n" //
        + "Date - 03/02/1982\n" //
        + "Info - Information blablabla\n" //
        + "Title - New title\n" //
        + "Date - 04/05/1993\n" //
        + "Info - New Info\n";

    Pattern myPattern = Pattern.compile("Title - (.*)\nDate - (.*)\nInfo - (.*)");
    Matcher m = myPattern.matcher(inputString);
    String title = "";
    String date = "";
    String info = "";

    while (m.find()) {
        title = m.group(1);
        date = m.group(2);
        info = m.group(3);
        System.out.println("Title : [" + title + "] Date : [" + date + "] Info : [" + info + "]");
    }

This code returns :

Title : [Hello] Date : [03/02/1982] Info : [Information blablabla]
Title : [New title] Date : [04/05/1993] Info : [New Info]

I'm sure you can find a better regex but I'm not an expert ;)

Baptiste
  • 449
  • 4
  • 7
0

try as using Pattern.compile :

    File sdcard = Environment.getExternalStorageDirectory();

    //Get the text file
    File file = new File(sdcard,"myaddress.txt");

    //Read text from file
    StringBuilder text = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) {
            text.append(line);
            text.append("####");
        }
    }
    catch (IOException e) {
        //You'll need to add proper error handling here
    }
System.out.println("text text text ====:: "+text.toString());

String your_string = text.toString();

Pattern pattern = Pattern.compile("####");
String[] strarray =pattern.split(your_string);

NOTE :

this is working on my end i have tested it .if you have any better solution like store your data in JSON or XML file is preferable my way as other suggestion in comments

Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213