Created
November 17, 2019 22:26
-
-
Save Ushiosan23/fc8b0dd817d249c0a99b300cf3f5c611 to your computer and use it in GitHub Desktop.
Write file with binary object data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package render; | |
| import java.io.Serializable; | |
| public class Pixel implements Serializable { | |
| private int X; | |
| private int Y; | |
| private short R; | |
| private short G; | |
| private short B; | |
| private short A; | |
| public Pixel(int x, int y, short r, short g, short b, short a) { | |
| X = x; | |
| Y = y; | |
| R = r; | |
| G = g; | |
| B = b; | |
| A = a; | |
| } | |
| @Override | |
| public String toString() { | |
| return String.format("[%d, %d] => {r: %d, g: %d, b: %d, a: %d}", X, Y, R, G, B, A); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import render.Pixel; | |
| import java.io.FileInputStream; | |
| import java.io.FileOutputStream; | |
| import java.io.ObjectInputStream; | |
| import java.io.ObjectOutputStream; | |
| import java.util.ArrayList; | |
| public class Program { | |
| public static void main(String[] args) throws Exception { | |
| ArrayList<Pixel> pixels = new ArrayList<>(); | |
| FileOutputStream outputStream = new FileOutputStream("data.pixel"); | |
| ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); | |
| int max = 20; | |
| for (int x = 0; x < max; x++) { | |
| for (int y = 0; y < max; y++) { | |
| short r = (short) Math.floor(Math.random() * 255); | |
| short g = (short) Math.floor(Math.random() * 255); | |
| short b = (short) Math.floor(Math.random() * 255); | |
| Pixel pixel = new Pixel(x, y, r, g, b, (short) 255); | |
| pixels.add(pixel); | |
| } | |
| } | |
| objectOutputStream.writeObject(pixels); | |
| objectOutputStream.close(); | |
| FileInputStream inputStream = new FileInputStream("data.pixel"); | |
| ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); | |
| Object dataP = objectInputStream.readObject(); | |
| if (dataP instanceof ArrayList) { | |
| for (Object x : (ArrayList) dataP) { | |
| System.out.println(x); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment