lang:csharp:readerwriter
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
lang:csharp:readerwriter [2016/10/19 14:01] – [Lecture de 5 octets sous forme d'une string] : orthographe root | lang:csharp:readerwriter [2020/04/27 08:04] (Version actuelle) – Conversion de <note> vers <WRAP> root | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
=====Lecture de données binaire avec un StreamReader===== | =====Lecture de données binaire avec un StreamReader===== | ||
Il faut utiliser '' | Il faut utiliser '' | ||
+ | |||
+ | =====BinaryReader / BinaryWriter===== | ||
+ | <code csharp> | ||
+ | using (BinaryWriter bw = new BinaryWriter(File.Open(fichier, | ||
+ | </ | ||
+ | |||
+ | Pour info, il est possible de faire un '' | ||
+ | |||
+ | '' | ||
=====Lecture de 5 octets sous forme d'un string===== | =====Lecture de 5 octets sous forme d'un string===== | ||
Ligne 11: | Ligne 20: | ||
string result = System.Text.Encoding.ASCII.GetString(lecture); | string result = System.Text.Encoding.ASCII.GetString(lecture); | ||
</ | </ | ||
- | Apparemment, | + | |
+ | Il n'y a pas besoin du caractère '' | ||
+ | |||
+ | <WRAP center round info 60%> | ||
+ | Par contre, cette méthode crée un '' | ||
+ | <code csharp> | ||
+ | if (result.IndexOf(' | ||
+ | { | ||
+ | result = result.Substring(0, | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
=====Remplacement dans un fichier via regex/ | =====Remplacement dans un fichier via regex/ | ||
Ligne 24: | Ligne 44: | ||
{ | { | ||
string tempLineValue; | string tempLineValue; | ||
- | | + | FileStream inputStream = null; |
+ | try | ||
{ | { | ||
+ | inputStream = File.OpenRead(originalFile) | ||
using (StreamReader inputReader = new StreamReader(inputStream)) | using (StreamReader inputReader = new StreamReader(inputStream)) | ||
{ | { | ||
+ | inputStream = null; | ||
using (StreamWriter outputWriter = File.AppendText(outputFile)) | using (StreamWriter outputWriter = File.AppendText(outputFile)) | ||
{ | { | ||
Ligne 37: | Ligne 60: | ||
} | } | ||
} | } | ||
+ | finally | ||
+ | { | ||
+ | if (inputStream != null) | ||
+ | inputStream.Dispose(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | [[https:// | ||
+ | =====Ajout d'un texte à l' | ||
+ | Pas de méthode miracle malheureusement. | ||
+ | <code csharp> | ||
+ | var sb = new StringBuilder(); | ||
+ | using (var sr = new StreamReader(" | ||
+ | { | ||
+ | string line; | ||
+ | do | ||
+ | { | ||
+ | line = sr.ReadLine(); | ||
+ | sb.AppendLine(line); | ||
+ | } while (!line.Contains("< | ||
+ | |||
+ | sb.Append(myText); | ||
+ | sb.Append(sr.ReadToEnd()); | ||
+ | } | ||
+ | |||
+ | using (var sr = new StreamWriter(" | ||
+ | { | ||
+ | sr.Write(sb.ToString()); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | =====Chargement d'un fichier texte en mémoire===== | ||
+ | <code csharp> | ||
+ | File.ReadAllLines(nomFichier, | ||
+ | </ | ||
+ | |||
+ | =====Création d'un fichier d'une taille précise===== | ||
+ | <code csharp> | ||
+ | using (var fs = new FileStream(" | ||
+ | { | ||
+ | fs.SetLength(15021); | ||
} | } | ||
</ | </ | ||
- | [[http:// | + | [[https:// |
lang/csharp/readerwriter.1476878500.txt.gz · Dernière modification : 2016/10/19 14:01 de root