Java~IO输入输出流练习题~车库管理(停车、离开、查找、显示车库车辆信息)
程序员文章站
2022-04-03 17:52:54
...
IO流有很多种,按照操作数据的不同,可以分为字节流和字符流,按照数据传输方向的不同又可分为输入流和输出流,程序从输入流中读取数据,向输出流中写入数据。在IO包中,字节流的输入输出流分别用java.io.InputStream和java.io.OutputStream表示,字符流的输入输出流分别用java.io.Reader和java.io.Writer表示,具体分类如图所示。
字节流:
在JDK中,提供了两个抽象类InputStream和OutputStream,它们是字节流的*父类,所有的字节输入流都继承自InputStream,所有的字节输出流都继承自OutputStream。
字符流:
InputStream类和OutputStream类在读写文件时操作的都是字节,如果希望在程序中操作字符,使用这两个类就不太方便,为此JDK提供了字符流。同字节流一样,字符流也有两个抽象的*父类,分别是Reader和Writer。其中Reader是字符输入流,用于从某个源设备读取字符,Writer是字符输出流,用于向某个目标设备写入字符。
File类:
File类中提供了一系列方法,用于操作其内部封装的路径指向的文件或者目录,例如判断文件/目录是否存在、创建、删除文件/目录等。接下来介绍一下File类中的常用方法,如表所示。
练习题展示:
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
//车的类
class Car {
public String carNum;
public String carBrand;
public String carModel;
public Car(String carNum, String carBrand, String carModel) {
this.carNum = carNum;
this.carBrand = carBrand;
this.carModel = carModel;
}
@Override
public String toString() {
return carNum + "," + carBrand + "," + carModel;
}
}
//管理我的车库
class ManagePark {
//通过车牌号判断车库里是否有这辆车
public Boolean findCarByCarNum(Car car) throws IOException {
if(car == null) {
return false;
}
FileReader fileReader = new FileReader("e:\\MyCarPark.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String findCar = bufferedReader.readLine();
while (findCar != null) {
String[] strings = findCar.split(",");
if(strings[0].equals(car.carNum)) {
return true;
}
findCar = bufferedReader.readLine();
}
bufferedReader.close();
fileReader.close();
return false;
}
//停车入库 在构建FileWriter是加上true表示不覆盖 bufferedWriter.newline表是加在新的一行中
public void stopAtPark(Car car) throws IOException {
if(car == null) {
return;
}
if(findCarByCarNum(car)) {
return;
}
FileWriter fileWriter = new FileWriter("e:\\MyCarPark.txt", true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
String carString = car.toString();
bufferedWriter.newLine();
bufferedWriter.write(carString);
bufferedWriter.close();
fileWriter.close();
}
//离开车库 将除要离开的车全部插入链表 然后进行覆盖式的输入到文本中
public void LeavePark(Car car) throws IOException {
if( ! findCarByCarNum(car)) {
return;
}
List<Car> listCar = new LinkedList<>();
FileReader fileReader = new FileReader("e:\\MyCarPark.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String findCar = bufferedReader.readLine();
while (findCar != null) {
String[] strings = findCar.split(",");
if(strings.length > 1) {
if (!strings[0].equals(car.carNum)) {
listCar.add(new Car(strings[0], strings[1], strings[2]));
}
}
findCar = bufferedReader.readLine();
}
bufferedReader.close();
fileReader.close();
FileWriter fileWriter = new FileWriter("e:\\MyCarPark.txt");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (Car c: listCar
) {
String sCar = c.toString();
bufferedWriter.newLine();
bufferedWriter.write(sCar);
}
bufferedWriter.close();
fileWriter.close();
}
//通过车品牌Brand得到车库所有该品牌的车的对象
public Car[] getCarByBrand(String brand) throws IOException {
ArrayList<Car> carList = new ArrayList<>();
FileReader fileReader = new FileReader("e:\\MyCarPark.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String findCar = bufferedReader.readLine();
while (findCar != null) {
String[] strings = findCar.split(",");
if(strings.length > 1) {
if (strings[1].equals(brand)) {
carList.add(new Car(strings[0], strings[1], strings[2]));
}
}
findCar = bufferedReader.readLine();
}
bufferedReader.close();
fileReader.close();
return carList.toArray(new Car[0]);
}
//输出现在车库的所有车辆信息
public void displayMyParkCar() throws IOException {
FileReader fileReader = new FileReader("e:\\MyCarPark.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String findCar = bufferedReader.readLine();
while (findCar != null) {
System.out.println(findCar);
findCar = bufferedReader.readLine();
}
System.out.println();
bufferedReader.close();
fileReader.close();
}
}
public class MyCarParkTest {
public static void main(String[] args) {
try {
Car car = new Car("陕A33333", "宝马", "宝马X6");
Car car1 = new Car("陕A66666", "奔驰", "黑色G500");
ManagePark managePark = new ManagePark();
System.out.println("===初始车库===");
managePark.displayMyParkCar();
System.out.println("===找是否有陕A33333===");
System.out.println(managePark.findCarByCarNum(car));
System.out.println("===找是否有陕A66666===");
System.out.println(managePark.findCarByCarNum(car1));
System.out.println("===找车库中所有劳斯莱斯的车===");
Car[] cars = managePark.getCarByBrand("劳斯莱斯");
System.out.println(Arrays.toString(cars));
System.out.println();
System.out.println("===陕A66666离开车库陕A33333进入车库===");
managePark.LeavePark(car1);
managePark.stopAtPark(car);
System.out.println("===最终车库===");
managePark.displayMyParkCar();
}catch (IOException e) {
e.getStackTrace();
}
}
}
运行展示:
===初始车库===
陕A66666,奔驰,黑色G500
陕A88888,劳斯莱斯,劳斯莱斯幻影
陕A00000,迈巴赫,迈巴赫62
陕A11111,保时捷,保时捷911
陕A99999,劳斯莱斯,劳斯莱斯古斯特
===找是否有陕A33333===
false
===找是否有陕A66666===
true
===找车库中所有劳斯莱斯的车===
[陕A88888,劳斯莱斯,劳斯莱斯幻影, 陕A99999,劳斯莱斯,劳斯莱斯古斯特]
===陕A66666离开车库陕A33333进入车库===
===最终车库===
陕A88888,劳斯莱斯,劳斯莱斯幻影
陕A00000,迈巴赫,迈巴赫62
陕A11111,保时捷,保时捷911
陕A99999,劳斯莱斯,劳斯莱斯古斯特
陕A33333,宝马,宝马X6
文本展示:
一开始:
最后:
上一篇: TCL折叠屏探秘:自主研发屏幕、铰链
下一篇: Python数据相关性分析