Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

java - Iterator over binary file

Hello everyone
I wanted to know how to iterate on a file. The iterator should support the options-

  1. Upload to the map<String, Car> .
  2. Allow removal of the last Car including its ID which is represented in String. Without using an auxiliary array or auxiliary file, i.e. tampering with the file itself only. Some of the ideas I came up with were to use random access file and seek to the last place, I mean it will work together with ObjectInputStream.

How to do Remove?

This is what I have so far, I would like to get guidance:


 public class MyFile implements Iterable<Car> {
    private Map<String,Car> allCars;
    final String fileName;
    

    public MyFile(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public Iterator<Car> iterator() {
        return new MyFileIterator();
    }

    class MyFileIterator implements Iterator<Car> {
        private ObjectOutputStream oOut;
        private ObjectInputStream oIn;
        //BufferedReader in;
        //String nextline;
        Car nextCar;
        String nexrID;
        int position;

        public MyFileIterator() {
            oOut = new ObjectOutputStream(new FileOutputStream(fileName, true)); 
            oIn = new ObjectInputStream(new FileInputStream(fileName));
        }

        // If the file isn't over
        public boolean hasNext() { 
          return oIn.available() != 0;
        }

        // Return the next Car
        public Car next() {
            nextID = oIn.readUTF();
            nextCar = (Car)oIn.readObject();
            
            return nextCar;
        }
//TODO:
        public void remove() { 
            
                    
            }
      }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...