-1

I need to read a txt file using Springboot. This txt file consist of several words and need to assign those words in to a String Array. How can I do that?

2 Answers2

3

Since your question is too vague, it becomes nearly impossible to know what you are expecting. I'll assume that you want to read a file from the classpath. If you were expecting something else, please clarify it clearly.

You can use ClassPathResource to get the file name. And then you can handle files with FileReader or BufferedReader as you would normally handle files with Java.io. In a Spring Boot Application src/main/resources is the default classpath. So the String argument in the ClassPathResource constructor call must be relative to the classpath.

Resource resource = new ClassPathResource("test.txt");
File file = resource.getFile();
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

String[] array = bufferedReader.lines().collect(Collectors.joining()).split(",");
System.out.println(Arrays.asList(array));
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
0

here I give you a full solution for this. you can find the problem and solution here.

problem is ..

  1. you have to process a file of more than 1 million customers
  2. have to store customers in the database table
  3. You have to filter duplicate, and invalid customers based on their phone number and email, only valid customers will be stored in the table.
  4. You have to store all invalid customers in another separate table.
  5. After storing those data in the table, you have to export the invalid and valid customers into separate files.
  6. After exporting those you have to share those exported files with us.
  7. For valid customers, you have to export those files in a batch of files including 100k customers with each file.
  8. Have to share the total process execution time for exporting files.
  9. Project deployment documentation
  10. implements service and algorithm for file data processing and storing those data and exporting the files with customer data.
  11. You must have to implement a multithread process for your full process.

and you can find the full solution here

it was a Spring boot restful api project.