-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileToByteArray.java
56 lines (50 loc) · 1.6 KB
/
FileToByteArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Lesson2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*
* @author mich01
*/
public class FileToByteArray
{
int len;
int FLength;
byte [] FileBits;
public static void main(String [] args) throws IOException
{
File InFile = new File("E://robot.jpg");
FileToByteArray FileProcessor = new FileToByteArray();
FileProcessor.GetFileBin(InFile);
}
public int GetFileBin(File InputFile) throws FileNotFoundException, IOException
{
FileInputStream FileIn = new FileInputStream(InputFile);
byte data[] = new byte[(int) InputFile.length()];
FileIn.read(data);
FileIn.close();
System.out.println("File size: "+data.length);
for(int i=0;i<data.length;i++)
{
System.out.printf("%02X ",data[i]);
}
//Now to create a new file using the same byte retrieved
System.err.println("Writing file");
WriteFile(data, "E://new.jpg");
return 0;
}
public int WriteFile(byte [] File_Data, String FilePath) throws FileNotFoundException, IOException
{
FileOutputStream OutputFile = new FileOutputStream(FilePath);
OutputFile.write(File_Data);
System.out.println("\nFile Created Success");
return 0;
}
}