DataInputStream vs InputStreamReader trying to conceptually understand two

As I first understand it at the moment:

DataInputStreamis a subclass InputStream, so it reads and writes bytes. If you read bytes, and know that they will all be intor some other primitive data types, you can read them bytedirectly in the primitive using DataInputStream.

  • Question: Would you need to know the type (int, string, etc.) of the content that will be read before it is read? And should the whole file consist of one primitive type?

The question I have is : Why not use InputStreamReaderwrapped around data bytes InputStream? With this approach, you still read the bytes and then convert them to integers that represent characters. Which integers represent which characters depend on a given character set, for example, "UTF-8".

  • Question: In which case InputStreamReaderit will not be able to work, where will it work DataInputStream?

My answer to the question . If speed is really important, and you can do it, converting byte data InputStreamdirectly to primitive through DataInputStreamwill be a way? This avoids the Readerneed to "distinguish" byte data before int; and he will not rely on providing a character set to interpret the character represented by the returned integer. I suppose this means that people mean DataInputStreamthat it allows you to read raw data that is machine independent.

  • Simplification: DataInputStreamcan convert bytes directly to primitives.

The question that spurred all this : I read the following tutorial code:

    FileInputStream fis = openFileInput("myFileText");

    BufferedReader reader = new BufferedReader( new InputStreamReader( new DataInputStream(fis)));

    EditText editText = (EditText)findViewById(R.id.edit_text);

    String line;

    while(  (line = reader.readline()) != null){

        editText.append(line);
        editText.append("\n");
    }

... , new DataInputStream(fis), , - ?

  • - ?

.

+4
6

InputStreamReader DataInputStream .

DataInputStream - InputStream, .

, InputStream , DataInputStream , Java. - .

: (int, string ..) , ? ?

DataInputStream , DataOutputStream. , DataInputStream "" , , . , DataOutputStream .

, ( ):

public void exit() {
    //...
    DataOutputStream dout = new DataOutputStream(new FileOutputStream(STATE_FILE));
    dout.write(somefloat);
    dout.write(someInt);
    dout.write(someDouble);
}

public void startup() {
    DataInputStream dout = new DataInputStream(new FileInputStream(STATE_FILE));
    //exactly the same order, otherwise it going to return weird data
    dout.read(somefloat);
    dout.read(someInt);
    dout.read(someDouble);
}

DataInputStream DataOutputStream: .

InputStreamReader - . InputStreamReader "" Java. ( ) Java- InputStreamReader.

, , . , , , "UTF-8".

- , . , , ​​ . , UTF-8 UTF-16 , InputStreamReader , UTF-8 UTF-16. aabb, ​​ un UTF-8 ('a', 'a', 'b', 'b'), . . , , , .

InputStreamReader ( DataInputStream), .

: InputStreamReader , DataInputStream ?

. , . InputStreamReader not , DataInputStream .

, DataInputStream:

BufferedReader reader = new BufferedReader( new InputStreamReader(fis));

DataInputStream , InputStream, FileInputStream ( ).

+5

.

API DataInputStream: " Java - ". Java v 1.0, , , HTML, XML JSON, . , / int s, long s, float s .. - Java - .

. FileReader BufferedReader, , .

+1

InputStreamReader , , . , . DataInputStream - , , , . DataInputStream InputStreamReader, , . , , , . InputStreamReader , , XML HTTP.

0

DataInputStream , . ints, Strings, objects .. ( ASN.1, , DataInputStream, .) InputStreamReader .

, :

FileInputStream fis = ...;
BufferedReader reader = new BufferedReader(
    new InputStreamReader( 
        new DataInputStream(fis)
    )
);

:

FileInputStream fis = ...;
BufferedReader reader = new BufferedReader(
    new InputStreamReader(fis)
);

, , .

0

DataInputStream , , 32- . InputStream.

A InputStreamReader a InputStream, (Reader) . .

, DataInputStream InputStream.

0

, , . -, DataInputStream DataOutputStream. : , . , , DataOutputStream , , , .

package StackoverflowQuestion25511536;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Earthlings writes a collection of Earthling Peeps to
* a file using DataInputStream, then reads the collection. 
* 
* The intent of this class is to demonstrate that
* DataInputStream & DataOutputStream require knowledge
* of the data (types and order) that is written to a file so 
* that it can be meaningfully interpreted when read.
* 
* Using object serialization and ObjectInputStream
* and ObjectOutputStream should be considered.
* 
* Detection of end of file (EOF)
* is determined using technique taught in "Introduction to Java 
* Programming", 9th ed, by Y. Danial Liang.  
* 
* @author Ross Studtman
*/
public class Earthlings {   

List<Peeps> peepCollection = new ArrayList<Peeps>();

public static void main(String[] args){
    new Earthlings().run(); 
}

public void run(){
    makePeeps();
    writeAndRead();
}

public void makePeeps(){    
    peepCollection.add(new Peeps("Ross", 45, 6.9));
    peepCollection.add(new Peeps("Lebowski", 42, 7.8));
    peepCollection.add(new Peeps("Whedon", 50, 8.8));       
}

// When end of file reached an EOFException is thrown.
public void writeAndRead(){

    DataOutputStream output = null;
    DataInputStream input = null;

    try{
        // Create DataOutputStream
        output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("peeps.oddPeople")));

        // Iterate over collection
        for(Peeps peep : peepCollection){

            // Assign instance fields to output types.
            output.writeUTF(peep.getName());
            output.writeInt(peep.getAge());
            output.writeDouble(peep.getOddness());
        }

        // flush buffer to ensure everything is written.
        output.flush();

        // Close output
        output.close();

        // Create DataInputStream
        File theSavedFile = new File("peeps.oddPeople");            
        input = new DataInputStream(new BufferedInputStream(new FileInputStream(theSavedFile)));

        // How many bytes are in this file? Used in for-loop as upper iteration limit.
        long bytes = theSavedFile.length();

        // Reconstitute objects & print             
        for(long counter = 0 ; counter < bytes; counter++ ){  // EOFException thrown before 'counter' ever equals 'bytes'.

            String name = input.readUTF();
            int age = input.readInt();
            double oddity = input.readDouble();

            // Create and print new Peep object.
            System.out.println(new Peeps(name, age, oddity));
        }


    }catch(EOFException e){
        System.out.println("All data read from file.");         
    }catch(IOException e){
        e.printStackTrace();
    }finally{
        if(input != null){
            try {
                input.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }           
    }
}   

/**
 * Simple class to demonstrate with.
 */
class Peeps{        
    // Simple Peep info
    private String name;
    private int age;
    private double oddness;

    // Constructor
    public Peeps(String name, int age, double oddness) {
        super();
        this.name = name;
        this.age = age;
        this.oddness = oddness;
    }

    // Getters
    public String getName() { return name;}
    public int getAge() { return age;}
    public double getOddness() { return oddness;}

    @Override
    public String toString() {
        return "Peeps [name=" + name + ", age=" + age + ", oddness=" + oddness + "]";
    }           
}

}

0

All Articles