I’m trying to read a file with buffered reader, but it skips first character in a line sometimes.
Here is the file, I’m reading:
http://files.moonmana.com/forums/Rectangle.h
here is the result, I’m getting:
LINE: #ifndef RECTANGLE_H
LINE: include "Shape.h"
LINE: lass Rectangle : public Shape {
LINE: rivate:
LINE: double _width;
LINE: std::vector<b2Vec2*>* _vertices;
LINE: ublic:
LINE: Rectangle(std::vector<b2Vec2*>* vertices) { _vertices = vertices;};
LINE: void createVertices();
LINE: bool isMimePoint(b2Vec2);
LINE: double getWidth(){return _width;};
LINE: void setWidth(double width);
LINE: void setHeights(double heights);
LINE: ShapeType getType();
LINE: void moveOn( b2Vec2 ,b2Vec2*);
LINE: virtual b2Vec2* getCenter();
LINE: ;
LINE: endif
Here is my source code:
String path = file.getPath();
BufferedReader _br;
try {
_br = new BufferedReader(new FileReader(path));
do {
System.out.println("LINE: " + _br.readLine());
lines.add(_br.readLine());
} while (_br.read() != -1);
_br.close();
} catch (Exception e) {
System.out.println("Error reading file: " + e.getMessage());
}
You are printing every second line and saving every second line and skipping the start of every second line. You read data in three places and use it different ways. Once you have _br.read() a character, it won’t read it again so it won’t appear in the line.
A better approach is to use
As you can see it reads in one place and that values is used consistently.