importjava.io.*;publicclassDataStreamDemo{publicstaticvoidmain(String[] args){//向文件中写入数据try(FileOutputStream output =newFileOutputStream("data.dat");//使用文件名创建文件输出流对象//将文件输出流对象包装成缓冲输出流,最后包装成数据输出流对象DataOutputStream dataOutputStream =newDataOutputStream(newBufferedOutputStream(output));){
dataOutputStream.writeDouble(123.456);//写入double型数据
dataOutputStream.writeInt(100);//写入整形数据
dataOutputStream.writeChar('s');//写入字符型数据
dataOutputStream.writeBoolean(true);//写入布尔型数据
dataOutputStream.writeUTF("Java语言");//将字符串按UTF-8格式输入}catch(IOException e){System.out.println(e.toString());}//从文件读出数据try(FileInputStream input =newFileInputStream("data.dat");DataInputStream dataInputStream =newDataInputStream(newBufferedInputStream(input));){//只要文件中有数据,则循环while(dataInputStream.available()>0){//必须依次读入double d = dataInputStream.readDouble();int i = dataInputStream.readInt();char c = dataInputStream.readChar();boolean b = dataInputStream.readBoolean();String s = dataInputStream.readUTF();System.out.println("d = "+ d +" i = "+ i +" c = "+ c +" b = "+ b +" s = "+ s);}}catch(IOException e){System.out.println(e.toString());}}}
importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;publicclassFileCopyDemo{publicstaticvoidmain(String[] args){File inputFile =newFile("input.txt");File outputFile =newFile("output.txt");try(FileReader in =newFileReader("inputFile");FileWriter out =newFileWriter("outputFile.txt");){int c = in.read();while(c !=-1){
out.write(c);
c = in.read();}}catch(IOException e){System.out.println(e.toString());}}}