Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
Ambdós costats versió prèvia Revisió prèvia | Ultima revisió Ambdós costats nova versio | ||
java_lectura_escriptura [2022/11/24 16:56] albert_palacios_jimenez [Tipus d'arxius] |
java_lectura_escriptura [2022/11/24 17:00] albert_palacios_jimenez |
||
---|---|---|---|
Línia 85: | Línia 85: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | |||
+ | ==== DataOutputStream i DataInputStream ==== | ||
+ | |||
+ | Per guardar dades primitives de manera ordenada en arxius, hi ha les classes " | ||
+ | |||
+ | Exemple de guardar dades primitives amb " | ||
+ | |||
+ | <code java> | ||
+ | File file = new File(" | ||
+ | | ||
+ | FileOutputStream fileOutputStream = null; | ||
+ | DataOutputStream dataOutputStream = null; | ||
+ | try { | ||
+ | fileOutputStream=new FileOutputStream(file); | ||
+ | dataOutputStream=new DataOutputStream(fileOutputStream); | ||
+ | dataOutputStream.writeInt(50); | ||
+ | dataOutputStream.writeDouble(400.25); | ||
+ | dataOutputStream.writeUTF(“Hola”); | ||
+ | dataOutputStream.flush(); | ||
+ | } catch (IOException e) { | ||
+ | e.printStackTrace(); | ||
+ | } finally { | ||
+ | try { | ||
+ | if(fileOutputStream!=null){ fileOutputStream.close(); | ||
+ | if(dataOutputStream!=null){ dataOutputStream.close(); | ||
+ | } catch (Exception e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Exemple de llegir dades primitives amb " | ||
+ | (Es llegeixen en l' | ||
+ | |||
+ | <code java> | ||
+ | FileInputStream fileInputStream = null; | ||
+ | DataInputStream dataInputStream = null; | ||
+ | try { | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } catch (IOException e) { | ||
+ | | ||
+ | }finally { | ||
+ | try { | ||
+ | | ||
+ | | ||
+ | } catch (Exception e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Cal tenir en compte que els objectes no són dades primitives i per tant no es poden guardar i llegir amb els mètodes anteriors. | ||
+ | |||
==== Serialitzar ==== | ==== Serialitzar ==== | ||
Línia 183: | Línia 241: | ||
**Nota**: Cal tenir en compte, que els objectes serialitzats només es poden llegir en la mateixa versió de Java. | **Nota**: Cal tenir en compte, que els objectes serialitzats només es poden llegir en la mateixa versió de Java. | ||
- | |||
- | ==== DataOutputStream i DataInputStream ==== | ||
- | |||
- | Per guardar dades primitives de manera ordenada en arxius, hi ha les classes " | ||
- | |||
- | Exemple de guardar dades primitives amb " | ||
- | |||
- | <code java> | ||
- | File file = new File(" | ||
- | | ||
- | FileOutputStream fileOutputStream = null; | ||
- | DataOutputStream dataOutputStream = null; | ||
- | try { | ||
- | fileOutputStream=new FileOutputStream(file); | ||
- | dataOutputStream=new DataOutputStream(fileOutputStream); | ||
- | dataOutputStream.writeInt(50); | ||
- | dataOutputStream.writeDouble(400.25); | ||
- | dataOutputStream.writeUTF(“Hola”); | ||
- | dataOutputStream.flush(); | ||
- | } catch (IOException e) { | ||
- | e.printStackTrace(); | ||
- | } finally { | ||
- | try { | ||
- | if(fileOutputStream!=null){ fileOutputStream.close(); | ||
- | if(dataOutputStream!=null){ dataOutputStream.close(); | ||
- | } catch (Exception e) { | ||
- | e.printStackTrace(); | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Exemple de llegir dades primitives amb " | ||
- | (Es llegeixen en l' | ||
- | |||
- | <code java> | ||
- | FileInputStream fileInputStream = null; | ||
- | DataInputStream dataInputStream = null; | ||
- | try { | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | } catch (IOException e) { | ||
- | | ||
- | }finally { | ||
- | try { | ||
- | | ||
- | | ||
- | } catch (Exception e) { | ||
- | e.printStackTrace(); | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Cal tenir en compte que els objectes no són dades primitives i per tant no es poden guardar i llegir amb els mètodes anteriors. | ||
==== Escriptura i lectura d' | ==== Escriptura i lectura d' |