Created
July 12, 2016 11:43
-
-
Save sunmeat/f14ca2b672dc59d5d6a3a1ecee2d1d49 to your computer and use it in GitHub Desktop.
data output stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package files; | |
import java.io.*; | |
public class Program { | |
static String path = "C:\\1\\data.txt"; | |
public static void main(String[] args) { | |
// A data output stream lets an application write primitive Java data | |
// types to an output stream in a portable way. An application can then | |
// use a data input stream to read the data back in. | |
try (DataOutputStream outer = new DataOutputStream( | |
new FileOutputStream(path));) { | |
outer.writeInt(2); | |
outer.writeInt(3); | |
outer.writeDouble(3.5); | |
outer.writeDouble(1.5); | |
outer.writeFloat(3.5f); | |
outer.writeFloat(3.237f); | |
outer.writeBoolean(true); | |
System.out.println("Writing executed"); | |
} catch (Exception e) { | |
System.out.println("Error in writing"); | |
} | |
try (DataInputStream inner = new DataInputStream(new FileInputStream( | |
path))) { | |
int a = inner.readInt(); | |
int b = inner.readInt(); | |
int c = a + b; | |
System.out.println("Reading executed. Result of Int = " + c); | |
double a2 = inner.readDouble(); | |
double b2 = inner.readDouble(); | |
double c2 = a2 * b2; | |
System.out.println("Reading executed. Result of Double = " + c2); | |
float a3 = inner.readFloat(); | |
float b3 = inner.readFloat(); | |
float c3 = a3 * b3; | |
System.out.println("Reading executed. Result of Float = " + c3); | |
System.out.println(!inner.readBoolean()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.out.println("Error in reading"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment