Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python
C++

JAVA


PHP
SQL
Alice

TextVersusBinary


Frame1.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;

//----------------------------------------------------------------------------
public class Frame1 extends JFrame {
  JPanel contentPane;
  BorderLayout borderLayout1 = new BorderLayout();

  JLabel title=new JLabel("How integer arrays are stored in Binary and Text Files");

  Box mainBox=Box.createVerticalBox();
  Box buttonBox1=Box.createHorizontalBox();
  JButton S2AButton = new JButton("String2Array");
  JLabel spacer1=new JLabel("          ");
  JButton A2SButton = new JButton("Array2String");

  Box buttonBox2=Box.createHorizontalBox();
  JButton A2BButton = new JButton("Array2Binary");
  JLabel spacer2=new JLabel("          ");
  JButton B2AButton = new JButton("Binary2Array");

  JLabel arrayLabel=new JLabel("A=");
  JTextField[] element;
  int[] A;

  JTextArea SText= new JTextArea();
  Box AText=Box.createHorizontalBox();
  JTextArea BText= new JTextArea();

//----------------------------------------------------------------------------
  /**Construct the frame*/
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
//----------------------------------------------------------------------------
  /**Overridden so we can exit when window is closed*/
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }
//----------------------------------------------------------------------------
  /**Component initialization*/
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(borderLayout1);
    this.setTitle("Strings and Arrays and Binary Files");
    this.setSize(new Dimension(600,175));

    buttonBox1.add(S2AButton);
    buttonBox1.add(spacer1);
    buttonBox1.add(A2SButton);
    buttonBox2.add(A2BButton);
    buttonBox2.add(spacer2);
    buttonBox2.add(B2AButton);

    AText.add(arrayLabel);

    mainBox.add(SText);
    mainBox.add(buttonBox1);
    mainBox.add(AText);
    mainBox.add(buttonBox2);
    mainBox.add(BText);
    BText.setEditable(false);

    element=null;

    this.getContentPane().add(mainBox, BorderLayout.CENTER);
    this.getContentPane().add(title, BorderLayout.NORTH);

    S2AButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String2Array();
        validate();
      } });
    A2SButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Array2String();
        validate();
      } });
    A2BButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Array2Binary();
        validate();
      } });
    B2AButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Binary2Array();
        validate();
      } });
  }
//----------------------------------------------------------------------------
  public void Array2String() {
        SText.setText("");
        if(element!=null) {
          for(int i=0;i<element.length;i++) {
              A[i]=Integer.parseInt(element[i].getText());
          }
        SText.setText(IntArrayToString(A));
        }
  }
//----------------------------------------------------------------------------
  public void String2Array() {
       if(element!=null) {
           for(int i=0;i<element.length;i++) {
               AText.remove(element[i]);
           }
       }
       A=StringToIntArray(SText.getText());
       element=new JTextField[A.length];
       for(int i=0;i<A.length;i++) {
           element[i]=new JTextField(A[i]+"");
           AText.add(element[i]);
       }
  }
//----------------------------------------------------------------------------
  public void Array2Binary() {
    try {
       BText.setText("");
       FileOutputStream FS=new FileOutputStream("blah");
       DataOutputStream DS=new DataOutputStream(FS);
       if(element!=null) {
           for(int i=0;i<element.length;i++) {
               A[i]=Integer.parseInt(element[i].getText());
               DS.writeInt(A[i]);
           }
       }
       DS.close();
       FileInputStream IFS=new FileInputStream("blah");
       DataInputStream IDS=new DataInputStream(IFS);
       while(true) {
            BText.append(""+(char)IDS.readByte());
       }
    }
    catch(EOFException e) {
    }
    catch(IOException e) {
    }
  }
//----------------------------------------------------------------------------
  public void Binary2Array() {
     try {
        if(element!=null) {
            for(int i=0;i<element.length;i++) {
                AText.remove(element[i]);
            }
        }
        A=new int[BText.getText().length()/4];
        element=new JTextField[A.length];
   
        FileInputStream FS=new FileInputStream("blah");
        DataInputStream DS=new DataInputStream(FS);
        for(int i=0;i<A.length;i++) {
            A[i]=DS.readInt();
            element[i]=new JTextField(A[i]+"");
            AText.add(element[i]);
        }
    }
    catch(IOException e) {
    }
  }
//----------------------------------------------------------------------------
  public int[] StringToIntArray(String S) {
       StringTokenizer T=new StringTokenizer(S," ");
       int[] ans=new int[T.countTokens()];
       for(int i=0;i<ans.length;i++) {
          ans[i]=Integer.parseInt(T.nextToken());
       }
       return ans;
  }
//----------------------------------------------------------------------------
  public String IntArrayToString(int []A) {
       StringBuffer S=new StringBuffer();
       for(int i=0;i<A.length;i++) {
          S.append(A[i]+" ");
       }
       return new String(S);
  }
}
//----------------------------------------------------------------------------