java:buffered_io

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
java:buffered_io [2017/02/07 12:16]
gthanos [Buffered I/O]
java:buffered_io [2017/02/07 12:19]
gthanos [Διαβάζοντας και γράφοντας περισσότερα από ένα bytes σε δυαδικό αρχείο]
Line 11: Line 11:
  
  
-===== Διαβάζοντας και γράφοντας περισσότερα από ένα bytes σε δυαδικό αρχείο ===== 
- 
-Παρακάτω θα ξαναγράψουμε το πρόγραμμα CopyBytes μόνο που αυτή τη φορά το διάβασμα των bytes δεν θα γίνει ένα-ένα (unbuffered), αλλά θα διαβάζονται και θα γράφονται ακολουθίες των 512 bytes με την βοήθεια των κλάσεων [[http://docs.oracle.com/javase/7/docs/api/java/io/BufferedInputStream.html|BufferedInputStream]] και [[http://docs.oracle.com/javase/7/docs/api/java/io/BufferedOutputStream.html|BufferedOutputStream]]. 
- 
-<code java CopyBufferedBytes.java> 
-import java.io.*; 
- 
-public class CopyBufferedBytes { 
-  public static void main(String[] args) throws IOException { 
- 
-    BufferedInputStream in = null; 
-    BufferedOutputStream out = null; 
-    final int buffer_size = 256; 
-    byte []buffer = new byte[buffer_size]; 
- 
-    try { 
-      String filename; 
-      if(args.length > 0) 
-        filename = args[0]; 
-      else 
-        filename = "lena.jpg"; 
-       
-      in = new BufferedInputStream(new FileInputStream(filename)); 
-      out = new BufferedOutputStream(new FileOutputStream("__"+filename)); 
-      int size; 
- 
-      while ((size = in.read(buffer, 0, buffer_size)) != -1) { 
-        out.write(buffer, 0, size); 
-      } 
-    } finally { 
-      if (in != null) { 
-        in.close(); 
-      } 
-      if (out != null) { 
-        out.close(); 
-      } 
-    } 
-  } 
-} 
-</code> 
  
 ===== Διαβάζοντας και γράφοντας γραμμή-γραμμή σε αρχείο κειμένου ===== ===== Διαβάζοντας και γράφοντας γραμμή-γραμμή σε αρχείο κειμένου =====
java/buffered_io.txt · Last modified: 2017/02/07 12:30 (external edit)