Guest User

Untitled

a guest
Apr 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. /**
  4. * Created by IntelliJ IDEA.
  5. * User: User
  6. * Date: 8/12/11
  7. * Time: 13:30
  8. * To change this template use File | Settings | File Templates.
  9. */
  10. public class RunConversion {
  11. public static void main(String[] args) {
  12. try {
  13. byte[] bytes = getBytesFromFile(new File("C:\\Users\\User\\Desktop\\school\\jaar3\\legacy\\week3\\conversion\\src\\inputfile.dat"));
  14.  
  15. for (byte aByte : bytes) {
  16. String print = new String(new byte[] {aByte});
  17. System.out.println(print);
  18. }
  19.  
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24.  
  25. public static byte[] getBytesFromFile(File file) throws IOException {
  26. InputStream is = new FileInputStream(file);
  27.  
  28. // Get the size of the file
  29. long length = file.length();
  30.  
  31. // You cannot create an array using a long type.
  32. // It needs to be an int type.
  33. // Before converting to an int type, check
  34. // to ensure that file is not larger than Integer.MAX_VALUE.
  35. if (length > Integer.MAX_VALUE) {
  36. // File is too large
  37. }
  38.  
  39. // Create the byte array to hold the data
  40. byte[] bytes = new byte[(int) length];
  41.  
  42. // Read in the bytes
  43. int offset = 0;
  44. int numRead = 0;
  45. while (offset < bytes.length
  46. && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
  47. offset += numRead;
  48. }
  49.  
  50. // Ensure all the bytes have been read in
  51. if (offset < bytes.length) {
  52. throw new IOException("Could not completely read file " + file.getName());
  53. }
  54.  
  55. // Close the input stream and return bytes
  56. is.close();
  57. return bytes;
  58. }
  59. }
Add Comment
Please, Sign In to add comment