JAVA io流筆記08 資料型別處理流
package FileText;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//資料型別(基本+String)處理流
//輸入流:DataInputStream
//輸出流:DataOutputStream
public class DataInputStream08 {
public static void main(String args[]){
try {
write();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//資料+型別輸出到檔案
public static void write() throws IOException{
String str = "資料型別";
long l= 10L;
double dou = 2.5;
File file = new File("D:/text/dd/DataText.txt");
try {
DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
output.writeUTF(str);
output.writeLong(l);
output.writeDouble(dou);
output.flush();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
read(file);
}
public static void read(File file) throws IOException{
DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
String str = input.readUTF(); //順序必須與上面的一致,否則資料會亂
Long l = input.readLong();
Double d = input.readDouble();
System.out.println(str);
System.out.println(l);
System.out.println(d);
}
}