Bonsoir à tous,
Après tout ce que j'ai pu lire sur la manipulation des objets de java.io, je ne sais toujours pas dans quel cas utiliser write(b) au lieu de write(b, off, len).
Voici ce que dit la Javadoc :
public void write(byte[] b) throws IOException
Writes b.length bytes from the specified byte array to this output stream.
The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).
- Question 1 : Est-ce un problème de taille maximale acceptable par OutputStream qui conduit à utiliser plutôt write(b, 0, b.length) pour découper en petits paquets plus acceptables ?
- Question 2 : Si oui, alors existe-t'il une logique pour évaluer de façon rationnelle la taille optimale du tableau de bytes (dans les tutos je vois byte[8], mais aussi byte[2048]) ?
- Question 3 : Une fois choisi la méthode d'écriture dans le flux de sortie (pour moi il s'agit d'un Socket), j'imagine que pour l'objet InputStream qui reçoit le flux il faut un effet miroir...est-ce exact ?
Je me pose ces questions parceque mon code ci-dessous ne fonctionne qu'à moitié.
Selon la taille du tableau côté InputStream, je reçois ou pas le fichier.
Voici mon code qui écrit :
Voici mon code qui lit :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 private void doWriteData (String fName, OutputStream out){ byte [] bytes; BufferedInputStream in = null; File path = null; int length = 0; try { bytes = new byte[1024]; //Cuisine Android ! path = Environment.getExternalStorageDirectory(); in = new BufferedInputStream(new FileInputStream(new File(path + "/" + fName)), 8192); while ((length = in.read(bytes)) > 0) { out.write(bytes, 0, length); } out.flush(); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 private File doReadData(InputStream in){ byte[] bytes; BufferedOutputStream out = null; File path = null; File f = null; int length = 0; try { bytes = new byte[8000]; //Cuisine Android path = Environment.getExternalStorageDirectory(); f = new File(path.getAbsolutePath() + "/toto3.txt"); out = new BufferedOutputStream(new FileOutputStream(f), 8192); /******************** * CA NE MARCHE PAS * ********************/ /* while ((length = in.read(bytes)) > 0) { out.write(bytes, 0, length); } */ /************************* * CA MARCHE QU'A MOITIE * *************************/ length = in.read(bytes); out.write(bytes, 0, length); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return f; }
Partager