Java – Appending objects to binary files

Java is tricky, you cannot add objects to already existent files with default classes.

Here is the solution:

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class Test{
    private static String filename = "test";

    public static void main(String[] args) {
        writeToBinary (filename, "a", true);
        writeToBinary (filename, "b", true);
        writeToBinary (filename, "c", true);
        readFromBinaryFile (filename);
    }

    public static void writeToBinary (String filename, Object obj, boolean append){
        File file = new File (filename);
        ObjectOutputStream out = null;

        try{
            if (!file.exists () || !append) out = new ObjectOutputStream (new FileOutputStream (filename));
            else out = new AppendableObjectOutputStream (new FileOutputStream (filename, append));
            out.writeObject(obj);
            out.flush ();
        }catch (Exception e){
            e.printStackTrace ();
        }finally{
            try{
                if (out != null) out.close ();
            }catch (Exception e){
                e.printStackTrace ();
            }
        }
    }

    public static void readFromBinaryFile (String filename){
        File file = new File (filename);

        if (file.exists ()){
            ObjectInputStream ois = null;
            try{
                ois = new ObjectInputStream (new FileInputStream (filename));
                while (true){
                    String s = (String)ois.readObject ();
                    System.out.println (s);
                }
            }catch (EOFException e){

            }catch (Exception e){
                e.printStackTrace ();
            }finally{
                try{
                    if (ois != null) ois.close();
                }catch (IOException e){
                    e.printStackTrace ();
                }
            }
        }
    }

    private static class AppendableObjectOutputStream extends ObjectOutputStream {
          public AppendableObjectOutputStream(OutputStream out) throws IOException {
            super(out);
          }

          @Override
          protected void writeStreamHeader() throws IOException {}
    }
}

 

References:

https://stackoverflow.com/questions/4646272/appending-objects-to-a-binary-file

Android Developer Nanodegree

Official links:

https://developer.android.com/training

https://www.udacity.com/course/android-developer-nanodegree-by-google–nd801

Direct links to the free seminars:

 

Level Group Course Length Projects
Beginner Core UI Design   ☆¹ 4 Weeks Birthday Card, Business Info
Beginner Core User Input   ☆¹ 4 Weeks Coffee Ordering App, Score Tracker, Quiz App
Beginner Core Multi-Screen Apps   ☆¹ 8 Weeks Language Learning App, Tour Guide App
Beginner Core Networking   ☆¹ 5 Weeks Book List, News Reader
Beginner Core Data Storage   ☆¹ 8 Weeks Pet Database
Beginner Core UI Components 4 Weeks Build Your Own App
Beginner Core UX Design 6 Weeks Build a User-Targeted App
Intermediate Core App Development Ⅰ   ☆² 10 Weeks Popular Movies, Weather, Build Your Own App
Intermediate Core Material Design   ☆² 4 Weeks Apply Material Design to News Reader
Intermediate Services Sign-In & Profiles 2 Weeks
Intermediate Services Maps 2 Weeks
Intermediate Services Location & Context 2 Weeks
Intermediate Services Analytics 2 Weeks
Intermediate Services Ads 2 Weeks
Intermediate Games Game Design Ⅰ 8 Weeks Breakout Clone
Intermediate Games Game Design Ⅱ 8 Weeks MegaMan Clone
Advanced Core App Development Ⅱ   ☆² 6 Weeks Stock Tracker
Advanced Core Optimizing App Performance 4 Weeks
Advanced Core Firebase 8 Weeks
Advanced Core Gradle   ☆² 6 Weeks Joke Teller
Advanced Core Developing Scalable Apps 8 Weeks
Advanced Platforms Android TV & Google Cast 1 Week
Advanced Platforms Android Wear   ☆² 2 Weeks Create Wear Interface for Weather App
Advanced Platforms Android Auto 1 Week

 

 

References:

https://github.com/Enteleform/-RES-/blob/master/%5BLinks%5D/%5BAndroid%5D%20Udacity%20Curriculum.md#how-do-the-nanodegree-programs-work

 

 

Lock/Unlock homepage

Here are two small REGISTRY files which lock and unlock your homepage

lock:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Internet Explorer\Control Panel]
"HomePage"=dword:00000001

[HKEY_CURRENT_USER\Software\Policies\Microsoft\Internet Explorer\Control Panel]
"HomePage"=dword:00000001


[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]
"NoSaveSettings"=dword:00000000

unlock:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Internet Explorer\Control Panel]
"HomePage"=dword:00000000

[HKEY_CURRENT_USER\Software\Policies\Microsoft\Internet Explorer\Control Panel]
"HomePage"=dword:00000000

[-HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects\LocalUser\Software\Policies\Microsoft\Internet Explorer\Control Panel]
"HomePage"=dword:00000001

[-HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects\LocalUser\Software\Policies\Microsoft\Internet Explorer\Control Panel]
"**del.HomePage"=" "

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]
"NoSaveSettings"=dword:00000000

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects\LocalUser\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]
"NoSaveSettings"=dword:00000000

Show hidden files

Here is a small REGISTRY file which enables the option “Show hidden files” to the explorer.

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced]
"Hidden"=dword:00000001

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced]
"HideFileExt"=dword:00000000

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced]
"ShowSuperHidden"=dword:00000001

Personal portal to the Internet