Добавил:
nyan
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Александр Б. / lab2_java / src / lab2_java / FileWorker
.java package lab2_java;
import java.io.*;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public class FileWorker {
public static void write(String fileName, String text) {
File file = new File(fileName);
try {
if(!file.exists()) {
file.createNewFile();
System.out.println("Файл не найден. Создание нового файла.");
}
PrintWriter out = new PrintWriter(file.getAbsoluteFile());
try {
out.print(text);
} finally {
out.close();
}
}catch(IOException e) {
throw new RuntimeException(e);
}
}
public static ArrayList<String> read(String fileName) {
ArrayList<String> text = new ArrayList();
File file = new File(fileName);
if (!file.exists()) {
System.out.println("Файл не найден!");
}
try {
BufferedReader in = new BufferedReader(new FileReader(file.getAbsoluteFile()));
try {
String temp;
while((temp = in.readLine()) != null) {
text.add(temp);
}
} finally {
in.close();
}
} catch(IOException e) {
throw new RuntimeException(e);
}
return text;
}
public static ArrayList<Car> fileParseCar(ArrayList<String> text){
ArrayList<Car> cars = new ArrayList<Car>();
String model = new String();
int speed = 0;
String radio = new String();
boolean status = false;
int counter = 0;
for (String temp : text) {
if (temp.compareTo("[Truck]") == 0)
return cars;
if (temp.length() > 2) {
String[] line = new String[4];
line = temp.split(" ",4);
if (line[0].compareTo("Марка:") == 0) {
counter++;
model = line[1];
speed = Integer.parseInt(line[3]);
}
if (line[0].compareTo("Канал:") == 0) {
counter++;
radio = line[1];
if (line[3].compareTo("On") == 0) {
status = true;
}
}
}
if (counter == 2){
cars.add(new Car(model, speed, new Radio(status, radio)));
status = false;
counter = 0;
}
}
return cars;
}
public static ArrayList<Truck> fileParseTruck(ArrayList<String> text){
ArrayList<Truck> trucks = new ArrayList<Truck>();
int weight = 0;
int height = 0;
String model = new String();
int speed = 0;
String radio = new String();
boolean status = false;
int counter = 0;
boolean flag = false;
for (String temp : text) {
if (temp.compareTo("[Truck]") == 0)
flag = true;
if (temp.length() > 2 && flag) {
String[] line = new String[4];
line = temp.split(" ",4);
if (line[0].compareTo("Вес:") == 0) {
counter++;
weight = Integer.parseInt(line[1]);
height = Integer.parseInt(line[3]);
}
if(line[0].compareTo("Марка:") == 0) {
counter++;
model = line[1];
speed = Integer.parseInt(line[3]);
}
if (line[0].compareTo("Канал:") == 0) {
counter++;
radio = line[1];
if (line[3].compareTo("On") == 0) {
status = true;
}
}
}
if (counter == 3) {
trucks.add(new Truck(model, speed, new Radio(status, radio), weight, height));
status = false;
counter = 0;
}
}
return trucks;
}
public static Map<String, Integer> fileParseLog(ArrayList<String> text){
Map<String, Integer> map = new HashMap<String, Integer>();
String[] line = new String[3];
String res = new String();
boolean ArrayListCheck = true;
for (String temp : text) {
if (temp.compareTo("ArrayList") == 0){
ArrayListCheck = true;
continue;
}
if (temp.compareTo("LinkedList") == 0){
ArrayListCheck = false;
continue;
}
line = temp.split(" ",3);
if (line[0].compareTo("addTotalCount") == 0) {
res = line[2];
continue;
}
if ( line[0].compareTo("addTotalTime") == 0 ||
line[0].compareTo("addMedianTime") == 0 ){
if (ArrayListCheck)
res = res + " ArrayList " + line[0];
else
res = res + " LinkedList " + line[0];
map.put(res, Integer.parseInt(line[2]));
}
if (line[0].compareTo("removeTotalCount") == 0) {
res = line[2];
continue;
}
if ( line[0].compareTo("removeTotalTime") == 0 ||
line[0].compareTo("removeMedianTime") == 0 ){
if (ArrayListCheck)
res = res + " ArrayList " + line[0];
else
res = res + " LinkedList " + line[0];
map.put(res, Integer.parseInt(line[2]));
}
}
return map;
}
}
import java.io.*;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public class FileWorker {
public static void write(String fileName, String text) {
File file = new File(fileName);
try {
if(!file.exists()) {
file.createNewFile();
System.out.println("Файл не найден. Создание нового файла.");
}
PrintWriter out = new PrintWriter(file.getAbsoluteFile());
try {
out.print(text);
} finally {
out.close();
}
}catch(IOException e) {
throw new RuntimeException(e);
}
}
public static ArrayList<String> read(String fileName) {
ArrayList<String> text = new ArrayList();
File file = new File(fileName);
if (!file.exists()) {
System.out.println("Файл не найден!");
}
try {
BufferedReader in = new BufferedReader(new FileReader(file.getAbsoluteFile()));
try {
String temp;
while((temp = in.readLine()) != null) {
text.add(temp);
}
} finally {
in.close();
}
} catch(IOException e) {
throw new RuntimeException(e);
}
return text;
}
public static ArrayList<Car> fileParseCar(ArrayList<String> text){
ArrayList<Car> cars = new ArrayList<Car>();
String model = new String();
int speed = 0;
String radio = new String();
boolean status = false;
int counter = 0;
for (String temp : text) {
if (temp.compareTo("[Truck]") == 0)
return cars;
if (temp.length() > 2) {
String[] line = new String[4];
line = temp.split(" ",4);
if (line[0].compareTo("Марка:") == 0) {
counter++;
model = line[1];
speed = Integer.parseInt(line[3]);
}
if (line[0].compareTo("Канал:") == 0) {
counter++;
radio = line[1];
if (line[3].compareTo("On") == 0) {
status = true;
}
}
}
if (counter == 2){
cars.add(new Car(model, speed, new Radio(status, radio)));
status = false;
counter = 0;
}
}
return cars;
}
public static ArrayList<Truck> fileParseTruck(ArrayList<String> text){
ArrayList<Truck> trucks = new ArrayList<Truck>();
int weight = 0;
int height = 0;
String model = new String();
int speed = 0;
String radio = new String();
boolean status = false;
int counter = 0;
boolean flag = false;
for (String temp : text) {
if (temp.compareTo("[Truck]") == 0)
flag = true;
if (temp.length() > 2 && flag) {
String[] line = new String[4];
line = temp.split(" ",4);
if (line[0].compareTo("Вес:") == 0) {
counter++;
weight = Integer.parseInt(line[1]);
height = Integer.parseInt(line[3]);
}
if(line[0].compareTo("Марка:") == 0) {
counter++;
model = line[1];
speed = Integer.parseInt(line[3]);
}
if (line[0].compareTo("Канал:") == 0) {
counter++;
radio = line[1];
if (line[3].compareTo("On") == 0) {
status = true;
}
}
}
if (counter == 3) {
trucks.add(new Truck(model, speed, new Radio(status, radio), weight, height));
status = false;
counter = 0;
}
}
return trucks;
}
public static Map<String, Integer> fileParseLog(ArrayList<String> text){
Map<String, Integer> map = new HashMap<String, Integer>();
String[] line = new String[3];
String res = new String();
boolean ArrayListCheck = true;
for (String temp : text) {
if (temp.compareTo("ArrayList") == 0){
ArrayListCheck = true;
continue;
}
if (temp.compareTo("LinkedList") == 0){
ArrayListCheck = false;
continue;
}
line = temp.split(" ",3);
if (line[0].compareTo("addTotalCount") == 0) {
res = line[2];
continue;
}
if ( line[0].compareTo("addTotalTime") == 0 ||
line[0].compareTo("addMedianTime") == 0 ){
if (ArrayListCheck)
res = res + " ArrayList " + line[0];
else
res = res + " LinkedList " + line[0];
map.put(res, Integer.parseInt(line[2]));
}
if (line[0].compareTo("removeTotalCount") == 0) {
res = line[2];
continue;
}
if ( line[0].compareTo("removeTotalTime") == 0 ||
line[0].compareTo("removeMedianTime") == 0 ){
if (ArrayListCheck)
res = res + " ArrayList " + line[0];
else
res = res + " LinkedList " + line[0];
map.put(res, Integer.parseInt(line[2]));
}
}
return map;
}
}
Соседние файлы в папке lab2_java