Home » Core Java » io (page 10)

io

Read file in String with BufferedInputStream

In this example we shall show you how to read a File in String with the BufferedInputStream. To read a File in String with the BufferedInputStream one should perform the following steps: Create a new File instance by converting the given pathname string into an abstract pathname. Create a FileInputStream by opening a connection to an actual file, the file ...

Read More »

Read file with BufferedInputStream

With this example we are going to demonstrate how to read a file with a BufferedInputStream. In short, to read a file with a BufferedInputStream you should: Let’s take a look at the code snippet that follows: Create a new File instance by converting the given pathname string into an abstract pathname. Create a FileInputStream by opening a connection to ...

Read More »

Externalize arbitrary objects

public static byte[][] serializeObject(Externalizable object) throws Exception { ByteArrayOutputStream baos = null; ObjectOutputStream oos = null; byte[][] res = new byte[2][]; try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); object.writeExternal(oos); oos.flush(); res[0] = object.getClass().getName().getBytes(); res[1] = baos.toByteArray(); } catch (Exception ex) { throw ex; } finally { try { if(oos != null) oos.close(); } catch (Exception e) { ...

Read More »

Serialize arbitrary objects

public static byte[] serializeObject(Serializable object) throws Exception { ByteArrayOutputStream baos = null; ObjectOutputStream oos = null; byte[] res = null; try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); oos.flush(); res = baos.toByteArray(); } catch (Exception ex) { throw ex; } finally { try { if(oos != null) oos.close(); } catch (Exception e) { e.printStackTrace(); } } return ...

Read More »