rickyreticent

FileInputStreamDemo

Feb 10th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. import java.io.*;
  2. public class FileInputStreamDemo
  3. {
  4. public static void main(String args[])
  5. {
  6. if (args.length != 0)
  7. {
  8. System.err.println ("Syntax - FileInputStreamDemo file");
  9. return;
  10. }
  11. try
  12. {
  13. // Membuat input stream yang membaca dr file
  14. InputStream fileInput = new FileInputStream ("mytext.txt");
  15.  
  16. int data = fileInput.read(); // Baca byte ke 1
  17.  
  18. while (data != -1) // ulangi : hingga end of file (EOF) dicapai
  19. {
  20. System.out.write ( data ); // menampilkan byte data ke console
  21. data = fileInput.read(); // baca byte berikutnya
  22. }
  23.  
  24. fileInput.close(); // Close the file
  25. }
  26. catch (IOException ioe)
  27. {
  28. System.err.println ("I/O error - " + ioe);
  29. }
  30. }}
  31.  
  32.  
  33.  
  34. ###Copy Lines
  35. import java.io.FileReader;
  36. import java.io.FileWriter;
  37. import java.io.BufferedReader;
  38. import java.io.PrintWriter;
  39. import java.io.IOException;
  40. public class CopyLines {
  41. public static void main(String[] args) throws IOException {
  42. BufferedReader inputStream = null;
  43. PrintWriter outputStream = null;
  44. try {
  45. inputStream = new BufferedReader(new
  46. FileReader("charStreamsText.txt"));
  47. outputStream = new PrintWriter(new
  48. FileWriter("characteroutput.txt"));
  49. String l;
  50. int cu = 0;
  51. while ((l = inputStream.readLine()) != null) {
  52. outputStream.println(l);
  53. System.out.println(cu);
  54. cu += 1;
  55. }
  56. } finally {
  57. if (inputStream != null) {
  58. inputStream.close();
  59. }
  60. if (outputStream != null) {
  61. outputStream.close();
  62. }
  63. }
  64. }
  65. }
  66.  
  67.  
  68.  
  69. ###CopyBytes
  70.  
  71. import java.io.FileInputStream;
  72. import java.io.FileOutputStream;
  73. import java.io.IOException;
  74. public class CopyBytes {
  75. public static void main(String[] args) throws IOException {
  76. FileInputStream in = null;
  77. FileOutputStream out = null;
  78. try {
  79. in = new FileInputStream("byteStreamsText.txt");
  80. out = new FileOutputStream("outagain.txt");
  81. int c;
  82. while ((c = in.read()) != -1) {
  83. out.write(c);
  84. }
  85. }
  86. finally {
  87. if (in != null) {
  88. in.close();
  89. }
  90. if (out != null) {
  91. out.close();
  92. }
  93. }
  94. }
  95. }
  96.  
  97.  
  98.  
  99. ####CopyCharacters
  100.  
  101. import java.io.FileReader;
  102. import java.io.FileWriter;
  103. import java.io.IOException;
  104. public class CopyCharacters {
  105. public static void main(String[] args) throws IOException {
  106. FileReader inputStream = null;
  107. FileWriter outputStream = null;
  108. try {
  109. inputStream = new FileReader("charStreamsText.txt");
  110. outputStream = new FileWriter("characteroutput.txt");
  111. int c;
  112. int cu = 0;
  113. while ((c = inputStream.read()) != -1) {
  114. outputStream.write(c);
  115. System.out.println(cu);
  116. cu += 1;
  117. }
  118. }
  119. finally {
  120. if (inputStream != null) {
  121. inputStream.close();
  122. }
  123. if (outputStream != null) {
  124. outputStream.close();
  125. }
  126. }
  127. }
  128. }
  129.  
  130. ###ScanSum
  131.  
  132. import java.io.FileReader;
  133. import java.io.BufferedReader;
  134. import java.io.IOException;
  135. import java.util.Scanner;
  136. import java.util.Locale;
  137.  
  138. public class ScanSum {
  139. public static void main(String[] args) throws IOException {
  140.  
  141. Scanner s = null;
  142. double sum = 0;
  143.  
  144. try {
  145. s = new Scanner(new BufferedReader(new FileReader("usnumbers.txt")));
  146. s.useLocale(Locale.US);
  147.  
  148. while (s.hasNext()) {
  149. if (s.hasNextDouble()) {
  150. sum += s.nextDouble();
  151. } else {
  152. s.next();
  153. }
  154. }
  155. } finally {
  156. s.close();
  157. }
  158.  
  159. System.out.println(sum);
  160. }
  161. }
  162.  
  163.  
  164.  
  165. ####FileOutputStreamDemo
  166.  
  167. import java.io.*;
  168. public class FileOutputStreamDemo
  169. {
  170. public static void main(String args[])
  171. {
  172. if (args.length != 0)
  173. {
  174. System.err.println ("Syntax - FileOutputStreamDemo src dest");
  175. return;
  176. }
  177. String source = "mytext.txt";
  178. String destination = "myout.txt";
  179. try
  180. {
  181. // Open source file for input
  182. InputStream input = new FileInputStream(source);
  183. System.out.println ("Opened " + source + " for reading.");
  184. OutputStream output = new FileOutputStream (destination); // Ouput output file for output
  185. PrintStream pout = new PrintStream(output);
  186. System.out.println ("Opened " + destination + " for writing.");
  187. int data = input.read();
  188. while ( data != -1)
  189. {
  190. // Write byte of data to our file
  191. output.write (data);
  192. // Read next byte
  193. data=input.read();
  194. }
  195. // Close both streams
  196. pout.println("aku disini");
  197. input.close();
  198. output.close();
  199. System.out.println ("I/O streams closed");
  200. }
  201. catch (IOException ioe)
  202. {
  203. System.err.println ("I/O error - " + ioe);
  204. }
  205. }
  206. }
Add Comment
Please, Sign In to add comment