Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void Zad3(){
- String line;
- try{
- RandomAccessFile raf = new RandomAccessFile("C:\\Users\\TalarPC\\IdeaProjects\\Powturka\\baza.txt", "rw");
- raf.seek(0);
- System.out.println(raf.getFilePointer() + ", " + raf.length());
- while(raf.getFilePointer() < raf.length()){
- long fp = raf.getFilePointer();
- line = raf.readLine();
- long fp2 = raf.getFilePointer();
- System.out.println(raf.getFilePointer() + " - " + line);
- if(line.equals("cos")){
- raf.seek(fp);
- raf.writeUTF("XDXDXD");
- raf.seek(fp2);
- }
- }
- //odczyt
- raf.seek(0);
- System.out.println(raf.getFilePointer() + ", " + raf.length());
- while(raf.getFilePointer() < raf.length()){
- System.out.println(raf.readLine());
- }
- raf.close();
- raf.close();
- }catch(Exception x) { System.out.print("Błont " + x); }
- }
- public static String Zad2(){
- String imie;
- String nazwisko;
- int wiek;
- Double pensja;
- String plec;
- String result = "";
- int min = Integer.MAX_VALUE;
- try{
- RandomAccessFile raf = new RandomAccessFile("C:\\Users\\TalarPC\\IdeaProjects\\Powturka\\baza.bin", "rw");
- //zapis
- raf.writeUTF("Adrian"); raf.writeUTF("Malina"); raf.writeDouble(2137); raf.writeInt(55); raf.writeUTF("Nieznana");
- raf.writeUTF("Martyna"); raf.writeUTF("Buc"); raf.writeDouble(200); raf.writeInt(13); raf.writeUTF("Kobieta");
- raf.writeUTF("Grażyna"); raf.writeUTF("Buc"); raf.writeDouble(3500); raf.writeInt(65); raf.writeUTF("Kobieta");
- raf.seek(0);
- System.out.println(raf.getFilePointer() + ", " + raf.length());
- while(raf.getFilePointer() < raf.length()){
- imie = raf.readUTF();
- nazwisko = raf.readUTF();
- long pt = raf.getFilePointer();
- pensja = raf.readDouble();
- wiek = raf.readInt();
- plec = raf.readUTF();
- long pt2 = raf.getFilePointer();
- if(wiek>50 && !plec.equals("Kobieta")){
- pensja *= 1.10;
- raf.seek(pt);
- raf.writeDouble(pensja);
- }
- if(plec.equals("Kobieta")){
- if(wiek<min){
- min = wiek;
- result = (imie + " " + nazwisko);
- }
- }
- raf.seek(pt2);
- }
- //odczyt
- raf.seek(0);
- System.out.println(raf.getFilePointer() + ", " + raf.length());
- while(raf.getFilePointer() < raf.length()){
- System.out.print(raf.readUTF() + ", ");
- System.out.print(raf.readUTF() + ", ");
- System.out.print(raf.readDouble() + ", ");
- System.out.print(raf.readInt() + ", ");
- System.out.print(raf.readUTF() + ", \n");
- }
- raf.close();
- }catch(Exception x) { System.out.print("Błont " + x); }
- return result;
- }
- public static void zad1(){
- String x = "123";
- System.out.println("Parsowanie: " + Integer.parseInt(x));
- System.out.println("CharAt: " + x.charAt(0));
- System.out.println("ASCII: " + Character.getNumericValue(x.charAt(1)));
- }
- public static void plik() throws FileNotFoundException{
- File plik = new File("C:\\Users\\TalarPC\\IdeaProjects\\Powturka\\baza.txt");
- Scanner in = new Scanner(plik);
- String linia;
- String [] pos;
- String [] imiona = new String[liczLinijki()];
- int i = 0;
- while(true){
- try{
- linia = in.nextLine();
- pos = linia.split("\\+");
- System.out.println(pos[0] + ", " + pos[1] + ", " + pos[2]);
- imiona[i] = pos[0];
- i++;
- }catch(NoSuchElementException e) { break; }
- }
- System.out.println("Tablica imion:");
- for(int j = 0; j < imiona.length; j++){
- System.out.println("Imie: " +imiona[j]);
- }
- }
- public static int liczLinijki() throws FileNotFoundException{
- File plik = new File("C:\\Users\\TalarPC\\IdeaProjects\\Powturka\\baza.txt");
- Scanner in = new Scanner(plik);
- String linia;
- int result = 0;
- while(true){
- try{
- linia = in.nextLine();
- result++;
- }catch(NoSuchElementException e) { break; }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement