classe Java DataInputStream
Un flusso di input di dati consente un modo indipendente dalla macchina applicazione per leggere i tipi di dati primitivi Java da un flusso di input sottostante.
Il seguente costruttore viene utilizzato per creare un flusso di oggetto di immissione dei dati.
DataInputStream dis = DataInputStream(InputStream in);
Un altro modo per creare è quello di ricevere un array di byte, e due variabile intera off, len, rappresenta il via la prima lettura di byte, len indica la lunghezza di byte letti.
No. | metodo Descrizione |
---|---|
1 | int lettura finale pubblica (byte [] r, int off, int len) throws IOException flusso di input contiene da len byte letti in un array di byte. Se len è -1, restituisce il numero di byte letti. |
2 | public int lettura finale (byte [] b) throws IOException Leggi qualche numero di byte dal flusso di input contiene e li memorizza nella matrice del buffer b in. |
3 |
|
4 | public String readLine () throws IOException Legge la prossima riga di testo dal flusso di input. |
Esempi
L'esempio che segue illustra la DataInputStream e DataOutputStream utilizzare questo esempio di leggere da un file di testo test.txt 5 linee e convertito in lettere maiuscole, l'ultimo salvato in un altro file test1.txt in.
import java.io.*; public class Test{ public static void main(String args[])throws IOException{ DataInputStream d = new DataInputStream(new FileInputStream("test.txt")); DataOutputStream out = new DataOutputStream(new FileOutputStream("test1.txt")); String count; while((count = d.readLine()) != null){ String u = count.toUpperCase(); System.out.println(u); out.writeBytes(u + " ,"); } d.close(); out.close(); } }
Gli esempi sopra risultati elaborati sono i seguenti:
THIS IS TEST 1 , THIS IS TEST 2 , THIS IS TEST 3 , THIS IS TEST 4 , THIS IS TEST 5 ,