
Source de Fichier.java
import java.io.*;
public class Fichier
extends File {
public Fichier(String nomFichier) {
super(nomFichier);
}
public boolean lirePossible() {
return (this.exists()&&this.isFile()
&&this.canRead());
}
public boolean ecrirePossible() {
boolean fichierEnregistrable;
if (this.exists())
if (this.isFile() && this.canWrite())
fichierEnregistrable=true;
else
fichierEnregistrable=false;
else {
String nomAbsolu=this.getAbsolutePath();
String nomRepertoire=
nomAbsolu.substring
(0,nomAbsolu.lastIndexOf(File.separator));
File repertoire=new File(nomRepertoire);
if (repertoire.exists() && repertoire.canWrite())
fichierEnregistrable=true;
else
fichierEnregistrable=false;
}
return fichierEnregistrable;
}
public String proprietes() {
StringBuffer s=new StringBuffer();
if (!this.exists())
s.append("fichier non existant");
else if (this.isFile()) {
s.append("fichier");
if (this.canRead())
s.append(" acces en lecture");
if (this.canWrite())
s.append(" acces en ecriture");
}
else if (this.isDirectory())
s.append("repertoire");
return new String(s);
}
public String parent() {
String nomAbsolu=this.getAbsolutePath();
return nomAbsolu.substring(0,
nomAbsolu.lastIndexOf(File.separator));
}
}
|
Source de Fichier1.java
import java.io.*;
public class Fichier1 {
public static void main(String[] args) {
Fichier fichier;
if (args.length==0) {
System.out.println("argument "
+"nom_de_fichier ...!");
System.exit(0);
}
for (int i=0; i<args.length; ++i) {
fichier= new Fichier(args[i]);
System.out.println(fichier.getName()+" :");
System.out.println(fichier.proprietes());
}
}
}
|
$ java Fichier1 ../cours/chap15_d.html .. chap15_d.html : fichier acces en lecture acces en ecriture .. : repertoire |
Etant donné
la quantité d'information d'un fichier, il est peu conseillé
voire impossible de le charger d'un bloc dans une variable.
Source de LireFichier1.java
import java.io.*;
public class LireFichier1 {
public static void main(String[] args) {
if (args.length!=1) {
System.out.println("argument nom_de_fichier !");
System.exit(0);
}
Fichier fichier= new Fichier(args[0]);
if (!fichier.lirePossible()) {
System.out.println(args[0]
+" "+fichier.proprietes());
System.exit(0);
}
try {
FileReader flotLecture = new FileReader(fichier);
long longueurFichier= fichier.length();
int dejaLu = 0;
char car=0;
while (dejaLu < longueurFichier) {
car= (char)flotLecture.read();
dejaLu = dejaLu + 1;
System.out.print(car);
}
flotLecture.close();
} catch (IOException e) {
System.out.println(" erreur :" + e.toString());
}
}
}
|
$ type essai.txt voici 1 ligne une autre fini $ java LireFichier1 essai.txt voici 1 ligne une autre fini |
Source de EcrireFichier1.java
import java.io.*;
public class EcrireFichier1 {
public static void main(String[] args) {
if (args.length!=1) {
System.out.println("argument nom_de_fichier !");
System.exit(0);
}
Fichier fichier= new Fichier(args[0]);
if (!fichier.ecrirePossible())
System.out.println(args[0]
+" "+fichier.proprietes());
else {
try {
FileWriter flotEcriture = new FileWriter(fichier);
for (char car='a'; car<='z'; ++car)
flotEcriture.write(car);
flotEcriture.close();
} catch (IOException e) {
System.out.println(" erreur :" + e.toString());
}
}
}
}
|
$ java EcrireFichier1 essai.txt $ type essai.txt abcdefghijklmnopqrstuvwxyz |