Steven,
 
If you don't care how the data is physically stored, you can use the
ObjectOutputStream and ObjectInputStream classes to do what you want:
 
import java.io.*;
 
public class Test {
 
  public static void main(String[] args) throws Exception {
    byte[] bytes = {1, 2, 3, 4};
    String string = "Hello";
    Integer integer = new Integer(456);
 
    ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream("test.data"));
    oos.writeObject(bytes);
    oos.writeObject(bytes);
    oos.writeObject(string);
    oos.writeObject(string);
    oos.writeObject(integer);
    oos.close();
 
    ObjectInputStream ois = new ObjectInputStream(new
FileInputStream("test.data"));
    try {
      Object object;
      while ((object = ois.readObject()) != null) {
        if (object instanceof byte[]) {
          System.out.print("byte array: {");
 
          byte[] b = (byte[]) object;
 
          for (int i = 0 ; i < b.length ; i++) {
            System.out.print("" + b[i] + (i == b.length - 1 ? "" : ", "));
          }
          System.out.println("}");
        }
        else if (object instanceof String) {
          System.out.println("String: " + (String) object);
        }
        else {
          String className = object.getClass().getName();
          System.out.println(className + ": " + object.toString());
        }
      }
    }
    catch (EOFException eofex) { }
  }
}
 
Stan


-----Original Message-----
From: Steven [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 09, 2002 6:23 PM
To: JDJList
Subject: [jdjlist] Question about Reading byte[] and String to file
Importance: High


Hi All,
 
I am currently facing the problem by reading byte[] and String data from
file.
If i write the byte[] and String data alternatively to a file through a
loop...which is the best way I should use? (BufferedOutputStream?
FileWriter?...?). After i write it...how am I going to read all data and
store them back to byte[] and String variables accordingly? (I dont have the
fix sequence to write byte[] or String).
 
 
Please help...Thank you.
 
 
 
 
 
Steven
To change your membership options, refer to: 
http://www.sys-con.com/java/list.cfm 


To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

Reply via email to