package hotel1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import hotel.CloseStreamMethod;
public class ImportData{
public ArrayList roomsLists = new ArrayList();
public static void main(String[] args) {
ImportData ip = new ImportData();
ip.importRooms();
}
public String importData(){
File src = new File("/Users/handsomeboy/eclipse-workspace/Coursework/src/hotel1/rooms.txt");
BufferedReader br = null;
String lines = null;
try {
br = new BufferedReader(new FileReader(src));
String line = null;
while((line = br.readLine()) != null) {
lines += line;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
CloseStreamMethod csm = new CloseStreamMethod();
csm.close(br);
}
return lines;
}
public void importRooms() {
String[] content = importData().split("\n");
for (String row : content) {
if(row != null) {
String[] contents = row.split(",");
if (contents.length == 5) {
roomsLists.add(new Rooms(Integer.parseInt(contents[0]),
RoomType.valueOf(contents[1].toUpperCase()),
Double.parseDouble(contents[2]),
Integer.parseInt(contents[3]),contents[4]));
}
}
}
System.out.println(roomsLists);
}
}
输出内容:
[]
文本内容:
101,double,80.00,2,own bathroom
102,double,80.00,2,own bathroom
103,twin,70.00,2,shared bathroom
104,twin,70.00,2,shared bathroom
201,double,80.00,2,own bathroom
202,single,70.00,1,own bathroom
301,family,90.00,4,own bathroom
package hotel1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import hotel.CloseStreamMethod;
public class ImportData{
public ArrayList roomsLists = new ArrayList();
public static void main(String[] args) {
ImportData ip = new ImportData();
ip.importRooms();
System.out.println(ip.roomsLists);
}
public String importData(){
File src = new File("/Users/handsomeboy/eclipse-workspace/Coursework/src/hotel1/rooms.txt");
BufferedReader br = null;
String lines = "";
try {
br = new BufferedReader(new FileReader(src));
String line = "";
while((line = br.readLine()) != null) {
lines += line + "\n";
}
} catch (IOException e) {
e.printStackTrace();
} finally {
CloseStreamMethod csm = new CloseStreamMethod();
csm.close(br);
}
return lines;
}
public boolean importRooms() {
String[] content = importData().split("\n");
for (String row : content) {
String contents[] = row.split("\\,");
if (contents.length == 5) {
roomsLists.add(new Rooms(Integer.parseInt(contents[0]),
RoomType.valueOf(contents[1].toUpperCase()),
Double.parseDouble(contents[2]),
Integer.parseInt(contents[3]),contents[4]));
}
}
return true;
}
}
package hotel1;
public class Rooms {
private int roomNumber;
private RoomType roomType;
private double price;
private int capacity;
private String facilities;
public Rooms(int roomNumber, RoomType roomType, double price, int capacity, String facilities ) {
this.roomNumber = roomNumber;
this.roomType = roomType;
this.price = price;
this.capacity = capacity;
this.facilities = facilities;
}
public String toString(){
return "\n" + " RoomNumber:" + roomNumber + " Type:" + roomType + " Price:" + price +
" Capacity:" + capacity + " Facilities:" +facilities;
}
public int getRoomNumber() {
return roomNumber;
}
public void setRoomNumber(int roomNumber) {
this.roomNumber = roomNumber;
}
public RoomType getRoomType() {
return roomType;
}
public void setRoomType(RoomType roomType) {
this.roomType = roomType;
}
public double getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public String getFacilities() {
return facilities;
}
public void setFacilities(String facilities) {
this.facilities = facilities;
}
}
终于改好了