top of page

Read lines from text file in Android

  • agulevski10
  • Feb 27, 2020
  • 4 min read

In this example will be shown how to parse text file in to data objects and use the data where needed. The example is separated in two classes MainActivity and Library data transfer object (DTO) to map that line.

This was the parser that was used in solving the example of HashCode 2020 qualification round.

This is the txt file that we will use in our example. It must go in assets folder under app/src/main/assets. If you don`t see this folder, simply create it by clicking right click on main and then New->Directory and name it assets (use low letter). There paste the text file a_example.txt.


This example shows how to read two lines simultaneously from text.

Java

private void readLines() { List<Library> libraries = new ArrayList<Library>(); try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(getAssets().open("a_example.txt")))) { String line1 = ""; String line2 = ""; int i = 0; // We read two lines at same time // If you need only one line of text per iteration, just do while ((line1 = bufferedReader.readLine())) { ..code } while ((line1 = bufferedReader.readLine()) != null && (line2 = bufferedReader.readLine()) != null) { Library library = new Library(); if (i == 0) { String[] parts = line1.split(" "); String numberOfBooks = ""; String numberOfLibraries = ""; String numberOfDaysForScanning = ""; String scoresOfTheBooks = ""; numberOfBooks = parts[0]; numberOfLibraries = parts[1]; numberOfDaysForScanning = parts[2]; scoresOfTheBooks = line2; Log.i("--->", "Books: " + numberOfBooks + " Libraries: " + numberOfLibraries + " Days for scanning: " + numberOfDaysForScanning); Log.i("--->", "Scores of the books: " + scoresOfTheBooks); } if (i > 0) { library.setBooks(line2); String[] parts = line1.split(" "); if (parts.length > 0) { library.setLibraryId(String.valueOf(i - 1)); library.setNumberOfBooks(parts[0]); if (parts.length > 1) { library.setSignUpProcess(parts[1]); if (parts.length > 2) { library.setShippingPerDay(parts[2]); } } } libraries.add(library); } i++; } } catch (IOException e) { if (e.getMessage() != null) { Log.d("--->", e.getMessage()); } else { Log.d("--->", "Something went wrong"); } } for (int j = 0; j < libraries.size(); j++) { Log.i("--->", "Library ID is: " + libraries.get(j).getLibraryId() + " Library NumBooks is: " + libraries.get(j).getNumberOfBooks() + " Library Sign Up Process is: " + libraries.get(j).getSignUpProcess() + " Library Can Ship per day: " + libraries.get(j).getShippingPerDay() + " Library Books: " + libraries.get(j).getBooks()); } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

private void readLines() {

List<Library> libraries = new ArrayList<Library>();

try (BufferedReader bufferedReader = new BufferedReader(new

InputStreamReader(getAssets().open("a_example.txt")))) {

String line1 = "";

String line2 = "";

int i = 0;

// We read two lines at same time

// If you need only one line of text per iteration, just do while ((line1 = bufferedReader.readLine())) { ..code }

while ((line1 = bufferedReader.readLine()) != null && (line2 = bufferedReader.readLine()) != null) {

Library library = new Library();

if (i == 0) {

String[] parts = line1.split(" ");

String numberOfBooks = "";

String numberOfLibraries = "";

String numberOfDaysForScanning = "";

String scoresOfTheBooks = "";

numberOfBooks = parts[0];

numberOfLibraries = parts[1];

numberOfDaysForScanning = parts[2];

scoresOfTheBooks = line2;

Log.i("--->", "Books: " + numberOfBooks + " Libraries: " + numberOfLibraries + " Days for scanning: " + numberOfDaysForScanning);

Log.i("--->", "Scores of the books: " + scoresOfTheBooks);

}

if (i > 0) {

library.setBooks(line2);

String[] parts = line1.split(" ");

if (parts.length > 0) {

library.setLibraryId(String.valueOf(i - 1));

library.setNumberOfBooks(parts[0]);

if (parts.length > 1) {

library.setSignUpProcess(parts[1]);

if (parts.length > 2) {

library.setShippingPerDay(parts[2]);

}

}

}

libraries.add(library);

}

i++;

}

} catch (IOException e) {

if (e.getMessage() != null) {

Log.d("--->", e.getMessage());

} else {

Log.d("--->", "Something went wrong");

}

}

for (int j = 0; j < libraries.size(); j++) {

Log.i("--->", "Library ID is: " + libraries.get(j).getLibraryId()

+ " Library NumBooks is: " + libraries.get(j).getNumberOfBooks()

+ " Library Sign Up Process is: " + libraries.get(j).getSignUpProcess()

+ " Library Can Ship per day: " + libraries.get(j).getShippingPerDay()

+ " Library Books: " + libraries.get(j).getBooks());

}

}

And here is the Library Model for transferring data:

Java

ublic class Library { public String libraryId; public String numberOfBooks; public String signUpProcess; public String shippingPerDay; public String books; public String getLibraryId() { return libraryId; } public void setLibraryId(String libraryId) { this.libraryId = libraryId; } public String getNumberOfBooks() { return numberOfBooks; } public void setNumberOfBooks(String numberOfBooks) { this.numberOfBooks = numberOfBooks; } public String getSignUpProcess() { return signUpProcess; } public void setSignUpProcess(String signUpProcess) { this.signUpProcess = signUpProcess; } public String getShippingPerDay() { return shippingPerDay; } public void setShippingPerDay(String shippingPerDay) { this.shippingPerDay = shippingPerDay; } public String getBooks() { return books; } public void setBooks(String books) { this.books = books; } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

ublic class Library {

public String libraryId;

public String numberOfBooks;

public String signUpProcess;

public String shippingPerDay;

public String books;

public String getLibraryId() {

return libraryId;

}

public void setLibraryId(String libraryId) {

this.libraryId = libraryId;

}

public String getNumberOfBooks() {

return numberOfBooks;

}

public void setNumberOfBooks(String numberOfBooks) {

this.numberOfBooks = numberOfBooks;

}

public String getSignUpProcess() {

return signUpProcess;

}

public void setSignUpProcess(String signUpProcess) {

this.signUpProcess = signUpProcess;

}

public String getShippingPerDay() {

return shippingPerDay;

}

public void setShippingPerDay(String shippingPerDay) {

this.shippingPerDay = shippingPerDay;

}

public String getBooks() {

return books;

}

public void setBooks(String books) {

this.books = books;

}

}

Comments


bottom of page