浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

Read a file into a byte array

Contents

[hide]

[edit] Implementations

[edit] C

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. char* readFileBytes(const char *name)  
  5. {  
  6.     FILE *fl = fopen(name, "r");  
  7.     fseek(fl, 0, SEEK_END);  
  8.     long len = ftell(fl);  
  9.     char *ret = malloc(len);  
  10.     fseek(fl, 0, SEEK_SET);  
  11.     fread(ret, 1, len, fl);  
  12.     fclose(fl);  
  13.     return ret;  
  14. }  

[edit] C++

  1. #include <iostream>  
  2. #include <fstream>  
  3.   
  4. char* readFileBytes(const char *name)  
  5. {  
  6.     ifstream fl(name);  
  7.     fl.seekg( 0, ios::end );  
  8.     size_t len = fl.tellg();  
  9.     char *ret = new char[len];  
  10.     fl.seekg(0, ios::beg);   
  11.     fl.read(ret, len);  
  12.     fl.close();  
  13.     return ret;  
  14. }  

[edit] C#

  1. var bytes = System.IO.File.ReadAllBytes(name);  

[edit] Common Lisp

The following does not work with multibyte streams. The common lisp stream must be single-byte for this particular function.

; here's the function
(defun slurp-stream4 (stream)
  (let ((seq (make-string (file-length stream))))
    (read-sequence seq stream)
    seq))

;open up a stream:
(with-open-file (stream "filename.txt"))
  ;; call the function and return the string.
  (slurp-stream4 stream))

This is from [1].

[edit] Java

This method reads the entire content of a file into a byte array.

  1. // Returns the contents of the file in a byte array.  
  2. public static byte[] getBytesFromFile(File file) throws IOException {  
  3.     InputStream is = new FileInputStream(file);  
  4.     byte[] bytes;  
  5.           
  6.     try {  
  7.         // Get the size of the file  
  8.         long length = file.length();  
  9.           
  10.         // You cannot create an array using a long type.  
  11.         // It needs to be an int type.  
  12.         // Before converting to an int type, check  
  13.         // to ensure that file is not larger than Integer.MAX_VALUE.  
  14.         if (length > Integer.MAX_VALUE) {  
  15.             // File is too large (>2GB)  
  16.         }  
  17.       
  18.         // Create the byte array to hold the data  
  19.         bytes = new byte[(int)length];  
  20.       
  21.         // Read in the bytes  
  22.         int offset = 0;  
  23.         int numRead = 0;  
  24.         while (offset < bytes.length  
  25.                && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {  
  26.             offset += numRead;  
  27.         }  
  28.       
  29.         // Ensure all the bytes have been read in  
  30.         if (offset < bytes.length) {  
  31.             throw new IOException("Could not completely read file " + file.getName());  
  32.         }  
  33.     }  
  34.     finally {  
  35.         // Close the input stream and return bytes  
  36.         is.close();  
  37.     }  
  38.     return bytes;  
  39. }  

[edit] OCaml

# let get_bytes_from_file filename =
    let ch = open_in filename in
    let buf = Buffer.create 1024 in
    (try Buffer.add_channel buf ch max_int with _ -> ());
    close_in ch;
    buf;;
val get_bytes_from_file : string -> Buffer.t = <fun>

[edit] Perl

use File::Slurp (read_file);
my $slurped = read_file('filename');

[edit] Python

Python 2.x's "str" type acts as an (immutable) byte array (not a true char array), so the following suffices:

  1. def get_bytes_from_file(filename):  
  2.     return open(filename, "rb").read()  

[edit] Ruby

Since String objects 'hold and manipulate an arbitrary sequence of bytes' (Source), simply reading a file into a String will suffice.

  1. def get_bytes_from_file(filename)  
  2.   File.read(filename)  
  3. end  
posted on 2012-01-24 02:51  lexus  阅读(1214)  评论(0编辑  收藏  举报