I have to make a controller class and there I have to define get method to read a txt file which i have in my local storage.I made a controller and I don’t know if it is really a correct one
Advertisement
Answer
It looks like your text file has new lines. So can you try using streams?
@RestController
@RequestMapping("/api")
public class Controller {
@GetMapping("/file")
String getData(){
String strLine="";
try (BufferedReader br = new BufferedReader(new FileReader(new File("/path"))))
{
strLine= br.lines().collect(Collectors.joining("n")); //this way next line text can also be returned into existing string & strLine will looks like exactly as your text file content
} catch (IOException e) {
e.printStackTrace();
}
return strLine;
}
}