Gestionarea sistemului de fisiere Flashcards
Scrieti si cititi dintr-un fisier folosind fluxul de octeti ( byte streams )
//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) { }
Scrieti si cititi dintr-un fisier folosind fluxul de caractere
//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) { }
Exista un mod mai eficient de scriere si citire dintr-un fisier caracter cu caracter, care este acesta?
Exemple de citire si scriere
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) { }
Scrieti intr-un fisier 3 tipuri primitive si cititi-le apoi din fisier.
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) { }
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
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"); } }
Cum se pot lista toate elementele folderului root ?
File[] roots = File.listRoots();
for (int i = 0; i < roots.length; i++) { System.out.println(roots[i]); }
Cum se poate verifica daca un elementul este director sau fisier ?
File file = new File(“img.bmp”);
file. isFile();
file. isDirectory();
Cum putem verifica daca un fisier sau un folder exista in sistem ?
file.exists();
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 ?
file. exists();
file. lastModified(); va afisa rezultatul intr-un format Instant care trebuie convertit intr-un format inteligibil
file. setReadOnly();
Cum se pot citi toate elementele unui director ?
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]);
}
Prima metoda de citire a elementelor dintr-un director/folder returna denumirile in String, cum se pot returna obiectele in sine ?
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
Cum se poate crea un director pe locatia
C:\Users\Radu\Desktop\Udemy ?
File dir = new File(“C:\Users\Radu\Desktop\Udemy\newFolder”);
try { if (!dir.exists()) { dir.mkdir(); } } catch (Exception e) {
}
Cum se poate sterge directorul newFolder de pe locatia C:\Users\Radu\Desktop\Udemy ?
File dir = new File(“C:\Users\Radu\Desktop\Udemy\newFolder”);
if (dir.exists()) { dir.delete(); }
C:\Users\Radu\Desktop\Udemy\oldname.txt
Redenumiti oldname.txt in newname.txt
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"); }
Logica de copiere a unui fisier de pe o locatie pe alta
C:\Users\Radu\Desktop\Udemy\oldname.txt
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); }