Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. @CrossOrigin
  2. @RequestMapping(value = "/fileRead", method = RequestMethod.GET)
  3. public @ResponseBody String fileRead() throws Exception {
  4.  
  5. File file = new ClassPathResource("countries.txt").getFile();
  6.  
  7. FileInputStream fis = null;
  8.  
  9. try {
  10. fis = new FileInputStream(file);
  11. System.out.println(file.getAbsolutePath());
  12. System.out.println(file.getPath());
  13. System.out.println("Total file size to read (in bytes) : " + fis.available());
  14.  
  15. int content;
  16. while ((content = fis.read()) != -1) {
  17. // convert to char and display it
  18. System.out.print((char) content);
  19. }
  20.  
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. } finally {
  24. try {
  25. if (fis != null)
  26. fis.close();
  27. } catch (IOException ex) {
  28. ex.printStackTrace();
  29. return ex.getMessage();
  30. }
  31.  
  32. }
  33.  
  34. return "done";
  35.  
  36. }
  37.  
  38. @CrossOrigin
  39. @RequestMapping(value = "/fileWrite", method = RequestMethod.GET)
  40. public @ResponseBody String fileWrite() throws Exception {
  41.  
  42.  
  43. try {
  44.  
  45. String content = "This is the content to write into file";
  46.  
  47. File file = new ClassPathResource("countries.txt").getFile();
  48.  
  49.  
  50. if (!file.exists()) {
  51. file.createNewFile();
  52. }
  53.  
  54. FileWriter fw = new FileWriter(file.getAbsoluteFile());
  55. BufferedWriter bw = new BufferedWriter(fw);
  56. bw.write(content);
  57. bw.close();
  58.  
  59. return "done";
  60.  
  61. } catch (IOException e) {
  62. return e.getMessage();
  63. }
  64.  
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement