Java使用类并读取文件
我正在编写一个程序,它有四个类:圆形、矩形、GeometricObject和我的主类TestGeometricObject。 我不明白如何使用TestGeometricObject读取另一个文件,如记事本 我不需要阅读课程的帮助我现在只需要帮助我如何连接我必须阅读的其他文件,比如记事本上的文件,不仅仅是这个程序,而是我要写的任何程序?我希望这是有意义的…如果没有,请告诉我Java使用类并读取文件,java,Java,我正在编写一个程序,它有四个类:圆形、矩形、GeometricObject和我的主类TestGeometricObject。 我不明白如何使用TestGeometricObject读取另一个文件,如记事本 我不需要阅读课程的帮助我现在只需要帮助我如何连接我必须阅读的其他文件,比如记事本上的文件,不仅仅是这个程序,而是我要写的任何程序?我希望这是有意义的…如果没有,请告诉我 public class TestGeometricObject { public static voi
public class TestGeometricObject {
public static void main(String[] args) {
GeometricObject geoObject1 = new Circle();
GeometricObject geoObject2 = new Rectangle();
System.out.println("The two objects have the same area? " +
equalArea(geoObject1, geoObject2));
// Display circle
displayGeometricObject(geoObject1);
// Display rectangle
displayGeometricObject(geoObject2);
}
public static boolean equalArea(GeometricObject object1,GeometricObject object2) {
return object1.getArea() == object2.getArea();
}
public static void displayGeometricObject(GeometricObject object) {
System.out.println();
System.out.println("The area is " + object.getArea());
System.out.println("The perimeter is " + object.getPerimeter());
}
}//end main
public class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/* Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}//end
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
/** Return area */
public double getArea() {
return width * height;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
}//end
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}//end
要读取文本文件,通常使用
BufferedReader
包装器和FileReader
try {
BufferedReader br = new BufferedReader(new FileReader(new File("yourFile.txt")));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
那个么,你们正在尝试读取txt文件吗?你们能粘贴你们用来读取文件的代码吗?如果我理解正确,这就是你所关心的问题。另外,
TestGeometricObject
中的读取器也会有所帮助,主要是如何初始化它们对我们来说并不重要。是的,我想从txt文件中读取@imtheman@user3444609今后,请提供您所要求的相关代码。操纵形状变换并获取有关它们的数学数据与读取文件无关。