Gestionarea sistemului de fisiere Flashcards

1
Q

Scrieti si cititi dintr-un fisier folosind fluxul de octeti ( byte streams )

A
//write
 try (FileOutputStream fos = new FileOutputStream("myFile.txt");) {
            fos.write(content.getBytes());
    } catch (IOException e) {
    }
//read
 try (FileInputStream fos = new FileInputStream("myFile.txt");) {
            int c = fos.read();
        while (c != -1) {
            fos.read();
        }

    } catch (IOException e) {
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Scrieti si cititi dintr-un fisier folosind fluxul de caractere

A
//write
try (FileWriter fw = new FileWriter("myFile.txt");) {
            fw.write(content);
    } catch (IOException e) {
    }
/read
try (FileReader fw = new FileReader("myFile.txt");) {
            int c;
        while ((c = fw.read()) != -1) {
            System.out.print((char)c);
        }

    } catch (IOException e) {
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Exista un mod mai eficient de scriere si citire dintr-un fisier caracter cu caracter, care este acesta?

Exemple de citire si scriere

A

Fluxul Buffered care scrie si citeste linie cu linie

//scriere
   String content = "BufferedWriter second";
        try (BufferedWriter br = new BufferedWriter(new FileWriter("myFile.txt"));) {
           br.write(content);
    } catch (IOException e) {
    }
//citire
 try (BufferedReader br = new BufferedReader(new FileReader("myFile.txt"));) {
            String line;
        while ((line = br.readLine()) != null) {
            System.out.print(line);
        }

    } catch (IOException e) {
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Scrieti intr-un fisier 3 tipuri primitive si cititi-le apoi din fisier.

A

String content = “DataOutputStream”;
int c = 10;
double d = 10.10;

//write
 try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("primitive.txt"))) {
        dos. writeUTF(content);
        dos. writeInt(c);
        dos. writeDouble(d);

    } catch (IOException e) {
    }
//read
        try (DataInputStream dos = new DataInputStream(new FileInputStream("primitive.txt"))) {
        System.out.println(dos.readUTF());
        System.out.println(dos.readInt());
        System.out.println(dos.readDouble());

    } catch (IOException e) {
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Cum se pot citi date din consola ?

Daca numarul scris in consola = 1 atunci sa se afiseze mesajul Succes, daca este orice alt numar sa se afiseze mesajul: You must enter 1 value

A

Scanner sc = new Scanner(System.in);

    while (sc.hasNext()) {
        if (sc.nextInt() == 1) {
            System.out.println("Success!");
        } else {
            System.out.println("You must enter 1 value");
        }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Cum se pot lista toate elementele folderului root ?

A

File[] roots = File.listRoots();

    for (int i = 0; i < roots.length; i++) {
        System.out.println(roots[i]);
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Cum se poate verifica daca un elementul este director sau fisier ?

A

File file = new File(“img.bmp”);

file. isFile();
file. isDirectory();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Cum putem verifica daca un fisier sau un folder exista in sistem ?

A

file.exists();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Cum putem verifica daca un fisier sau un folder exista in sistem ?

Cum putem verifica data si timpul cand a fost modificat ultima data un fisier ?

Cum putem seta un director sau fisier ca read only ?

A

file. exists();
file. lastModified(); va afisa rezultatul intr-un format Instant care trebuie convertit intr-un format inteligibil
file. setReadOnly();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Cum se pot citi toate elementele unui director ?

A

File dir = new File(“dir”);

if( dir.exists() && dir.isDirectory){

String [] elements = dir.list();
for (int i = 0; i < elements.length; i++) {System.out.println(elements[i]);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Prima metoda de citire a elementelor dintr-un director/folder returna denumirile in String, cum se pot returna obiectele in sine ?

A

File dir = new File(“C:\Users\Radu\Desktop\Udemy”);

    if (dir.isDirectory() && dir.exists()) {

        File[] elements = dir.listFiles();

        for (int i = 0; i < elements.length; i++) {
            System.out.println(elements[i].getName());
        }
    }F
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Cum se poate crea un director pe locatia

C:\Users\Radu\Desktop\Udemy ?

A

File dir = new File(“C:\Users\Radu\Desktop\Udemy\newFolder”);

        try {
            if (!dir.exists()) {
                dir.mkdir();
            }
        } catch (Exception e) {
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Cum se poate sterge directorul newFolder de pe locatia C:\Users\Radu\Desktop\Udemy ?

A

File dir = new File(“C:\Users\Radu\Desktop\Udemy\newFolder”);

    if (dir.exists()) {
        dir.delete();
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

C:\Users\Radu\Desktop\Udemy\oldname.txt

Redenumiti oldname.txt in newname.txt

A
File oldFile = new File("C:\\Users\\Radu\\Desktop\\Udemy\\oldname.txt");
        File newFile = new File("C:\\Users\\Radu\\Desktop\\Udemy\\newname.txt");
    if (!oldFile.exists()) {
        System.out.println("File with name oldname.txt does not exist!");
    }

    if (oldFile.renameTo(newFile)) {
        System.out.println("Rename successful");
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Logica de copiere a unui fisier de pe o locatie pe alta

C:\Users\Radu\Desktop\Udemy\oldname.txt

A

Path source = Paths.get(“C:\Users\Radu\Desktop\Udemy\oldname.txt”);
Path destination = Paths.get(“C:\Users\Radu\Desktop\Udemy\destination\oldname.txt”);

    try {
        if (!Files.exists(source)) {
            System.out.println("source does not exist!");
            return;
        }
        if (Files.exists(destination)) {
            System.out.println("destination file already exists!");
            return;
        }

        Files.copy(source, destination);
        System.out.println("File " + source.getFileName() + " is copied!");
    } catch (IOException e) {
        System.out.println(e);
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Efectuati serializarea binara a obiectului Car

public class Car {

public int id;
public String name;

@Override
public String toString() {
    return "id = " + this.id + " name = " + this.name;
}

}

A

Car car = new Car();

    car. id = 10;
    car. name = "Dacia";
        try (FileOutputStream fos = new FileOutputStream("C:\\Users\\Radu\\Desktop\\Udemy\\Car");
                ObjectOutputStream oos = new ObjectOutputStream(fos)) {
        oos. writeObject(car.id);
        oos. writeObject(car.name);
        oos. flush();

    } catch (IOException e) {
    }
17
Q

Efectuati deserializarea binara a obiectului Car creat si explicati care este dezavantajul acestei abordari ?

A

Dezavantajul acestei abordari este ca trebuie sa scriem fiecare camp in parte si nu obiectul complet.

try (FileInputStream fos = new FileInputStream("C:\\Users\\Radu\\Desktop\\Udemy\\Car");
                ObjectInputStream oos = new ObjectInputStream(fos)) {
            Car c = new Car();
        c.id = (int) oos.readObject();
        c.name = (String) oos.readObject();
        System.out.println(c);

    } catch (IOException e) {
    }
18
Q

Pentru a serializa obiectul complet cum se procedeaza ?

A
try (FileOutputStream fos = new FileOutputStream("C:\\Users\\Radu\\Desktop\\Udemy\\Car");
                ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            //   Car c = new Car();
        oos. writeObject(car);
        oos. flush();

    } catch (IOException e) {
    }
19
Q

Pentru a deserializa obiectul complet cum se procedeaza ?

A
try (FileInputStream fos = new FileInputStream("C:\\Users\\Radu\\Desktop\\Udemy\\Car");
                ObjectInputStream oos = new ObjectInputStream(fos)) {
            Car c = (Car) oos.readObject();
            System.out.println(c);
    } catch (IOException e) {
    }
20
Q

Efectuati serializarea textuala a clasei Car

A

Pentru a efectua acest lucru trebuie sa adaugam metodele set si get clasei Car

Car car = new Car();

    car. setId(10);
    car. setName("dacia");
        try (FileOutputStream fos = new FileOutputStream("C:\\Users\\Radu\\Desktop\\Udemy\\Car");
                XMLEncoder xml = new XMLEncoder(fos);) {
        xml. writeObject(car);
        xml. flush();

    } catch (IOException e) {
    }
21
Q

Efectuati deserializarea textuala a clasei Car

A
try (FileInputStream fos = new FileInputStream("C:\\Users\\Radu\\Desktop\\Udemy\\Car");
                XMLDecoder xml = new XMLDecoder(fos);) {
        Car c = (Car) xml.readObject();
        System.out.println(c);

    } catch (IOException e) {
    }