Admin Wed Jul 29, 2009 5:27 am
System.IO.StreamReaderUsed to explore streams that aren't files.
Constructor(path)Constructs a Read line stream from the file in the specified path (default UTF
.
Fieldsbool EndOfStream , returns true if the stream was read entirely.
MethodsCloseCloses the stream and liberates the resources allocated for the stream, MUST DO after finishing.
PeekReturns the next character in the stream without consuming a Read
ReadConsumes one character. //Conversion from int to char needed.
Read(CharTab, index,length)Reads length charrs from index and stores them in CharTab // Conversion needed (char)
ReadLineReturns the next line in the stream or null if it's it's end.
ReadToEnd - Code:
1. using System;
2. using System.IO;
3.
4. namespace Chap3 {
5. class Program {
6. static void Main(string[] args) {
7. // répertoire d'exécution
8. Console.WriteLine("Répertoire d'exécution : "+Environment.CurrentDirectory);
9. string ligne = null;
10. StreamReader fluxInfos = null;
11. // lecture contenu du fichier infos.txt
12. try {
13. // lecture 1
14. Console.WriteLine("Lecture 1----------------");
15. using (fluxInfos = new StreamReader("infos.txt")) {
16. ligne = fluxInfos.ReadLine();
17. while (ligne != null) {
18. Console.WriteLine(ligne);
19. ligne = fluxInfos.ReadLine();
20. }
21. }
22. // lecture 2
23. Console.WriteLine("Lecture 2----------------");
24. using (fluxInfos = new StreamReader("infos.txt")) {
25. Console.WriteLine(fluxInfos.ReadToEnd());
26. }
27. } catch (Exception e) {
28. Console.WriteLine("L'erreur suivante s'est produite : " + e.Message);
29. }
30. }
31. }
32. }