Advertisement
kriteshpokharel

Java file read/write

Dec 28th, 2021
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1.  
  2. public void onClick(View v) {
  3. String hID = id.getText().toString();
  4. String hName = name.getText().toString();
  5. String hLocation = location.getText().toString();
  6. String details = hID + " " + hName + " " + " " +hLocation+"\n";
  7. System.out.println(details);
  8. try {
  9. FileOutputStream fos = openFileOutput("file.txt",MODE_APPEND);
  10. fos.write(details.getBytes(StandardCharsets.UTF_8));
  11. System.out.println("Success");
  12. fos.close();
  13. } catch (FileNotFoundException e) {
  14. e.printStackTrace();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18.  
  19.  
  20. public void onClick(View v) {
  21. try {
  22. FileInputStream fis = openFileInput("file.txt");
  23. InputStreamReader isr = new InputStreamReader(fis);
  24. BufferedReader br = new BufferedReader(isr);
  25. StringBuilder sb = new StringBuilder();
  26. String text;
  27. while((text = br.readLine())!=null)
  28. {
  29. sb.append(text);
  30. sb.append("\n");
  31. System.out.println(text+"Asd");
  32. }
  33. op.setText(sb.toString());
  34. } catch (FileNotFoundException e) {
  35. e.printStackTrace();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement