- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.Serializable;
- <span style="color: rgb(51, 153, 102);">/**
- * 对 象的写入和读取
- * @author ec06cumt
- *
- */</span>
- public class TestObjectStream {
- <span style="color: rgb(51, 153, 102);">/**
- * @param args
- */</span>
- public static void main(String[] args) {
- T t=new T();
- t.k=10;
- try {
- FileOutputStream fos=new FileOutputStream("D:/testObjectIo.bak");
- ObjectOutputStream oos=new ObjectOutputStream(fos);
- oos.writeObject(t);
- oos.flush();
- oos.close();
- FileInputStream fis=new FileInputStream("D:/testObjectIo.bak");
- ObjectInputStream bis=new ObjectInputStream(fis);
- T tReader= (T) bis.readObject();
- System.out.println(tReader.i + " " +tReader.j + " "+tReader.d + " " + tReader.k);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch(IOException e1){
- e1.printStackTrace();
- }catch(ClassNotFoundException e2){
- e2.printStackTrace();
- }
- }
- }
- class T implements Serializable{
- int i=2;
- int j=4;
- double d=2.5;
- transient int k=15;
- // int k=15;
- }
例六:文件的目录复制
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- public class JavaIoCopyFile {
- <span style="color: rgb(51, 153, 102);">/**
- * Author:liugao ec06-2 cumt
- * @param args
- */</span>
- public static void main(String[] args) {
- File sourceFile=new File("D:/test");
- File targetFile=new File("F:/jstl");
- copy(sourceFile,targetFile);
- }
- private static void copy(File sourceFile, File targetFile) {
- File tarpath=new File(targetFile,sourceFile.getName());
- if(sourceFile.isDirectory()){
- tarpath.mkdir();
- File[] dir=sourceFile.listFiles();
- for(int i=0;i<dir.length;i++){
- copy(dir[i],tarpath);
- }
- }else{
- try {
- InputStream is=new FileInputStream(sourceFile);
- OutputStream os=new FileOutputStream(tarpath);
- byte[] buf=new byte[1024];
- int len=0;
- while((len= is.read(buf))!=-1){
- os.write(buf);
- }
- System.out.println("复制完成");
- is.close();
- os.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch(IOException e1){
- e1.printStackTrace();
- }
- }
- }
- }
常用的也就是这些方法吧。。。。
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; /** * * @author ec06cumt * */ public class TestDataStream { /** * @param args */ public static void main(String[] args) { ByteArrayOutputStream bos=new ByteArrayOutputStream(); DataOutputStream dos=new DataOutputStream(bos); try { // dos.writeFloat((float) Math.random()); // dos.writeLong((long) Math.random()); // dos.writeDouble((double) Math.random()); // dos.writeChar((char) Math.random()); dos.writeShort((short) Math.random()); dos.writeBoolean(true); //注意ByteArrayInputStream的构造方法是参数要是一个数组 ByteArrayInputStream bais=new ByteArrayInputStream(bos.toByteArray()); DataInputStream dis=new DataInputStream(bais); System.out.println(dis.available()); //System.out.println(dis.readDouble()); // System.out.println(dis.readInt()); // System.out.println(dis.readFloat()); // System.out.println(dis.readDouble()); // System.out.println(dis.readChar()); System.out.println(dis.readShort()); System.out.println(dis.readBoolean()); bos.close(); bais.close(); } catch (IOException e) { e.printStackTrace(); } } }