C#でファイルを読み取るさまざまな方法
ここでは、2つの異なるファイルを読み取っています-
テキストファイルを読む-
例
using System;
using System.IO;
namespace FileApplication {
class Program {
static void Main(string[] args) {
try {
using (StreamReader sr = new StreamReader("d:/new.txt")) {
string line;
// Read and display lines from the file until
// the end of the file is reached.
while ((line = sr.ReadLine()) != null) {
Console.WriteLine(line);
}
}
} catch (Exception e) {
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
} 出力
The file could not be read: Could not find a part of the path "/home/cg/root/4281363/d:/new.txt".
バイナリファイルの読み取り-
例
using System.IO;
namespace BinaryFileApplication {
class Program {
static void Main(string[] args) {
BinaryWriter bw;
BinaryReader br;
int i = 25;
double d = 3.14157;
bool b = true;
string s = "I am happy";
//create the file
try {
bw = new BinaryWriter(new FileStream("mydata", FileMode.Create));
} catch (IOException e) {
Console.WriteLine(e.Message + "\n Cannot create file.");
return;
}
//writing into the file
try {
bw.Write(i);
bw.Write(d);
bw.Write(b);
bw.Write(s);
} catch (IOException e) {
Console.WriteLine(e.Message + "\n Cannot write to file.");
return;
}
bw.Close();
//reading from the file
try {
br = new BinaryReader(new FileStream("mydata", FileMode.Open));
} catch (IOException e) {
Console.WriteLine(e.Message + "\n Cannot open file.");
return;
}
try {
i = br.ReadInt32();
Console.WriteLine("Integer data: {0}", i);
d = br.ReadDouble();
Console.WriteLine("Double data: {0}", d);
b = br.ReadBoolean();
Console.WriteLine("Boolean data: {0}", b);
s = br.ReadString();
Console.WriteLine("String data: {0}", s);
} catch (IOException e) {
Console.WriteLine(e.Message + "\n Cannot read from file.");
return;
}
br.Close();
Console.ReadKey();
}
}
} -
MacでInstagramでDMする方法のさまざまな方法
Instagram は、写真や動画をオンラインで共有できるソーシャル メディア プラットフォームとして知られています。このソーシャル メディア プラットフォームは、実際には世界で最も人気のあるソーシャル メディア ネットワークの 1 つである Facebook が所有しています。 また、Instagram の機能の 1 つは、DM またはダイレクト メッセージを使用して、実際に友達に個人的なメッセージを送信できることです。これは、Mac を除くすべてのデバイスで実際にアクセスできます。 Mac の Instagram で DM を送信する方法 ? Instagramに写真や動画を投稿
-
Windows 11 でファイルを解凍する 3 つの方法
Zip または圧縮ファイルは、ファイルを圧縮してグループ化するための最良の方法です。これにより、ユーザーはハード ディスクのストレージ スペースを節約できます。ただし、これらの zip ファイルを解凍または解凍する微妙な技術を知らない人もいます。この記事は、読者が Microsoft が提供する無料の組み込みツールを使用して Windows 11 でファイルを解凍するのに役立ちます。 Windows 11 でファイルを解凍する 3 つの方法 ファイル エクスプローラーを使用して Windows 11 でファイルを解凍する Windows ファイル エクスプローラーを使用すると、ユーザーはファ