Dodany parser plikow

This commit is contained in:
Tomasz Piechucki 2011-03-27 14:28:25 +00:00
parent ee675a839c
commit 953c599d1f
2 changed files with 128 additions and 0 deletions

38
Winda/DemoDane.txt Normal file
View File

@ -0,0 +1,38 @@
1 2
1 3
1 4
1 5
1 7
2 6
8 3
12 2
12 1
10 5
5 3
3 4
4 3
4 3
4 3
4 3
4 3
4 3
11 10
10 11
6 3
7 4
2 7
3 2
3 5
5 3
1 5
2 6
3 7
4 8
5 9
6 10
7 11
8 12
9 1
10 2
11 3
12 4

View File

@ -0,0 +1,90 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package winda.logic;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Tomek
*/
public class Parser {
public Pasazer [] Wczytaj(String filename){
try {
Pasazer [] pasazerowie;
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String s;
String [] strings = new String[1000];
int count = 0;
while((s = br.readLine()) != null){
strings[count] = s;
count++;
}
pasazerowie = new Pasazer[count];
for(int i = 0; i < count; i++){
String [] st = null;
st = strings[i].split(" ");
pasazerowie[i] = new Pasazer(i+1, Integer.parseInt(st[0]), Integer.parseInt(st[1]));
}
fr.close();
return pasazerowie;
} catch (IOException ex) {
Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public void Zapisz(String filename, Pasazer [] pasazerowie){
FileWriter fw = null;
try {
fw = new FileWriter(filename);
for (Pasazer ps : pasazerowie) {
fw.write(ps.GetStart() + " " + ps.GetStop() + '\r'+'\n');
}
fw.close();
} catch (IOException ex) {
Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fw.close();
} catch (IOException ex) {
Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args) {
//Do testów
//plik odczytu z args
System.out.println("Wczytywanie pliku " + args[0]);
Parser p = new Parser();
Pasazer [] pasazerowie = null;
pasazerowie = p.Wczytaj(args[0]);
System.out.println(pasazerowie.length);
for(Pasazer ps: pasazerowie){
System.out.println("Pasazer " + ps.GetName() + ": " + ps.GetStart() + " " + ps.GetStop());
}
System.out.println("Zapisywanie do pliku test.txt");
p.Zapisz("test.txt", pasazerowie);
pasazerowie = p.Wczytaj("test.txt");
System.out.println(pasazerowie.length);
for(Pasazer ps: pasazerowie){
System.out.println("Pasazer " + ps.GetName() + ": " + ps.GetStart() + " " + ps.GetStop());
}
}
}