A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王双宝 初级黑马   /  2012-7-13 17:11  /  1461 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Test {

    /**
     * @param args
     * @throws UnknownHostException
     * @throws SocketException
     */
    public static void main(String[] args){
        
        try {
            File f=new File("abc.txt");
            
            FileOutputStream fos=new FileOutputStream(f);
            ObjectOutputStream out=new ObjectOutputStream(fos);
            
            out.writeObject(new Point(3,4));//怎么改成添加在文件尾?
            out.writeObject(new Point(2,3));//怎么改成添加在文件尾?
            
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        try {
            ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File("abc.txt")));
            int i=0;
            while(true){
                    try{
                        Point p=(Point)in.readObject();
                        System.out.println(p);
                    }
                    catch(EOFException e){
                        break;
                    }
                    Point p=(Point)in.readObject();
                    System.out.println(p);
               
               
               
               
               
            }
            in.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }   
}

class Point implements Serializable{
    private int x;
    private int y;
    public Point(){
        
    }
    public Point(int x,int y){
        this.x=x;
        this.y=y;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "x="+x+",y="+y;
    }
}
当程序第一次运行时,abc.txt写入了两个对象。可再次运行,打印出的还是两个对象。怎么写才是添加到文件中的?
如:
第一次运行结果:
x=3,y=4
x=2,y=3
第二次运行结果:
x=3,y=4
x=2,y=3
x=3,y=4
x=2,y=3


评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

5 个回复

倒序浏览
public class Test {

    /**
     * @param args
     * @throws UnknownHostException
     * @throws SocketException
     */
    public static void main(String[] args){
        
        try {
            File f=new File("abc.txt");
            
            FileOutputStream fos=new FileOutputStream(f,true);//改成这个就可以了
            ObjectOutputStream out=new ObjectOutputStream(fos);
            
            out.writeObject(new Point(3,4));//怎么改成添加在文件尾?
            out.writeObject(new Point(2,3));//怎么改成添加在文件尾?
            
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        try {
            ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File("abc.txt")));
            int i=0;
            while(true){
                    try{
                        Point p=(Point)in.readObject();
                        System.out.println(p);
                    }
                    catch(EOFException e){
                        break;
                    }
                    Point p=(Point)in.readObject();
                    System.out.println(p);
               
               
               
               
               
            }
            in.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }   
}

class Point implements Serializable{
    private int x;
    private int y;
    public Point(){
        
    }
    public Point(int x,int y){
        this.x=x;
        this.y=y;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "x="+x+",y="+y;
    }
}

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 黑马-王言龙 于 2012-7-13 17:38 编辑

FileOutputStream fos=new FileOutputStream(f, true);
回复 使用道具 举报
  1. public static void main(String[] args){
  2.         
  3.         try {
  4.             File f=new File("D:\\abc.txt");
  5.             
  6.             FileOutputStream fos=new FileOutputStream(f,true); // 如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
  7.             ObjectOutputStream out=new ObjectOutputStream(fos);
  8.             
  9.             out.writeObject(new Point(3,4));//怎么改成添加在文件尾?
  10.             out.writeObject(new Point(2,3));//怎么改成添加在文件尾?
  11.             
  12.             out.flush();
  13.             out.close();
  14.         } catch (FileNotFoundException e) {
  15.             // TODO Auto-generated catch block
  16.             e.printStackTrace();
  17.         } catch (IOException e) {
  18.             // TODO Auto-generated catch block
  19.             e.printStackTrace();
  20.         }
  21.         
  22.         try {
  23.             ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File("abc.txt")));
  24.             int i=0;
  25.             while(true){
  26.                     try{
  27.                         Point p=(Point)in.readObject();
  28.                         System.out.println(p);
  29.                     }
  30.                     catch(EOFException e){
  31.                         break;
  32.                     }
  33.                     Point p=(Point)in.readObject();
  34.                     System.out.println(p);
  35.                
  36.                
  37.                
  38.                
  39.                
  40.             }
  41.             in.close();
  42.         } catch (FileNotFoundException e) {
  43.             // TODO Auto-generated catch block
  44.             e.printStackTrace();
  45.         } catch (IOException e) {
  46.             // TODO Auto-generated catch block
  47.             e.printStackTrace();
  48.         } catch (ClassNotFoundException e) {
  49.             // TODO Auto-generated catch block
  50.             e.printStackTrace();
  51.         }
  52.         
  53.     }   
  54. }
复制代码
如果是FileOutputStream(File file)构造方法将会在文件开头写,覆盖原来写入的数据。改过代码后你可以看看abc.txt文件里多了些东西
回复 使用道具 举报



import java.io.EOFException;
import java.io.File;
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;
import java.net.SocketException;
import java.net.UnknownHostException;





public class Test {

        /**
         * @param args
         * @throws UnknownHostException
         * @throws SocketException
         */
        public static void main(String[] args){
               
                try {
                        File f=new File("abc.txt");
                        if(!f.exists()){
                                f.createNewFile();
                        }
                       
                        FileOutputStream fos=new FileOutputStream(f,true);//是可以到文件尾
                        ObjectOutputStream out=new ObjectOutputStream(fos);
                       
                        out.writeObject(new Point(3,4));//怎么改成添加在文件尾?
                        out.writeObject(new Point(2,3));//怎么改成添加在文件尾?
                       
                       
                        out.flush();
                        out.close();
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
               
                try {
                        ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File("abc.txt")));
                        int i=0;
                        while(true){
                                        try{
                                                Point p=(Point)in.readObject();   //这里读不出来写入的所有对象,还是第一次写入的内容,后面新写的内容读出不来,为什么?
                                                System.out.println(p);
                                        }catch(Exception e){
                                                System.out.println(e.toString());//此处抛出了java.io.StreamCorruptedException
                                                break;
                                        }                               
                        }
                        in.close();
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
               
                }
        }       
}

class Point implements Serializable{
        private int x;
        private int y;
        public Point(){
               
        }
        public Point(int x,int y){
                this.x=x;
                this.y=y;
        }
        public int getX() {
                return x;
        }
        public void setX(int x) {
                this.x = x;
        }
        public int getY() {
                return y;
        }
        public void setY(int y) {
                this.y = y;
        }
        @Override
        public String toString() {
                // TODO Auto-generated method stub
                return "x="+x+",y="+y;
        }
}


运行结果:
x=3,y=4
x=2,y=3
java.io.StreamCorruptedException: invalid type code: AC
求解
回复 使用道具 举报
FileOutputStream fos=new FileOutputStream(f,true);
//改成这个就可以了
//ture代表你可以在已存在的文件后面续写该文件内容。
//如果改为false,那么就会开始的时候一样,每次运行都会创建一个新的文件替代已存在的文件。
//默认情况下是false。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马