Advertisement
luqman0611

i/o

Feb 21st, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6.  
  7. public class Main {
  8.  
  9. public static void main(String[] args) throws IOException {
  10. // write your code here
  11.  
  12. FileInputStream fileMasuk = null;
  13. FileOutputStream fileKeluar = null;
  14.  
  15. // membuka file
  16. fileMasuk = new FileInputStream("datain.txt");
  17.  
  18. //membaca file
  19. int data = fileMasuk.read();
  20.  
  21. while (data != -1){
  22. System.out.println((char)data);
  23. data = fileMasuk.read();
  24. }
  25.  
  26. //menutup file
  27. fileMasuk.close();
  28.  
  29. //
  30. try {
  31. //membuak file
  32. fileMasuk = new FileInputStream("datain.txt");
  33. fileKeluar = new FileOutputStream("datakeluar.txt");
  34.  
  35. //membaca file
  36. int data2 = fileMasuk.read();
  37.  
  38. while (data2 != -1) {
  39. fileKeluar.write(data2);
  40. data2 = fileMasuk.read();
  41. }
  42. }finally {
  43. if (fileMasuk != null) {
  44. fileMasuk.close();
  45. }
  46.  
  47. if (fileKeluar !=null){
  48. fileMasuk.close();
  49. }
  50. }
  51.  
  52. //
  53.  
  54. try ( FileInputStream in = new FileInputStream("datain.txt");
  55. FileOutputStream out = new FileOutputStream("dataout.txt")){
  56.  
  57. int data3 = in.read();
  58.  
  59. while (data3 != -1){
  60. out.write((char)data3);
  61. data3 = in.read();
  62. }
  63.  
  64. }
  65.  
  66.  
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement