Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- public class FileArray
- {
- public static void writeArray()
- {
- String fileName = "file.bin";
- int[] array = {10, 20, 30};
- try
- {
- FileOutputStream fileOs = new FileOutputStream(fileName);
- ObjectOutputStream os = new ObjectOutputStream(fileOs);
- for (int i = 0; i < 3; i++)
- os.writeInt(array[i]);
- os.close();
- fileOs.close();
- }
- catch (FileNotFoundException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void readArray()
- {
- String fileName = "file.bin";
- int[] array1;
- try
- {
- FileInputStream fileIs = new FileInputStream(fileName);
- ObjectInputStream is = new ObjectInputStream(fileIs);
- System.out.println(is.readInt());
- is.close();
- fileIs.close();
- }
- catch (FileNotFoundException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement