gpt4 book ai didi

Java-使用类并读取文件

转载 作者:行者123 更新时间:2023-12-01 13:14:51 24 4
gpt4 key购买 nike

我正在编写一个程序,它有四个类:Circle、Rectangle、GeometricObject 和我的主要类 TestGeometricObject。 我不明白如何使用 TestGeometricObject 从另一个文件(例如记事本)读取?

我不需要帮助阅读类(class),我现在只需要帮助,如何连接我必须读取的另一个文件(例如记事本上的文件),不仅仅是这个程序,而是我要编写的任何程序?我希望这是有道理的...如果没有,请告诉我。

 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();
}

关于Java-使用类并读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22548936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com