-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Formulatrix Indonesia |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Formulatrix Bootcamp 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Text; | ||
using System.IO; | ||
|
||
public class Program | ||
{ | ||
static void Main() | ||
{ | ||
int offset = 0; | ||
|
||
// Write to a file | ||
string myString = "Hello World"; | ||
using(FileStream fs = new(@"./MyString.txt", FileMode.Create, FileAccess.Write)) { | ||
byte[] bytes = Encoding.UTF8.GetBytes(myString); | ||
fs.Write(bytes, offset, bytes.Length - offset); | ||
} | ||
|
||
// Read from a file | ||
using(FileStream fs = new(@"./MyString2.txt", FileMode.Open, FileAccess.Read)) | ||
{ | ||
Console.WriteLine(fs.Length); | ||
byte[] bytes = new byte[fs.Length]; | ||
fs.Read(bytes, offset, bytes.Length - offset); | ||
string myString2 = Encoding.UTF8.GetString(bytes); | ||
Console.WriteLine(myString2); | ||
} | ||
|
||
// FileShare | ||
using(FileStream fs = new(@"./MyString3.txt", FileMode.Open, FileAccess.Read, FileShare.None)) | ||
{ | ||
while (true) | ||
{ | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
public class Program | ||
{ | ||
static void Main() | ||
{ | ||
string file1 = @"./Text1.txt"; | ||
string file2 = @"./Text2.txt"; | ||
string file3 = @"./Text3.txt"; | ||
|
||
using (FileStream fs = new(file1, FileMode.OpenOrCreate, FileAccess.ReadWrite)) | ||
{ | ||
using (StreamWriter sw = new(fs)) | ||
{ | ||
sw.BaseStream.Position = fs.Seek(0, SeekOrigin.End); | ||
sw.WriteLine(); | ||
sw.WriteLine("This is new line"); | ||
} | ||
} | ||
string[] myString = File.ReadAllLines(file1); | ||
foreach (var s in myString) | ||
{ | ||
Console.WriteLine(s); | ||
} | ||
|
||
// Modify specific line | ||
int line = 2; | ||
string newString = "This is new line"; | ||
string[] myString2 = File.ReadAllLines(file2); | ||
using (FileStream fs = new(file2, FileMode.OpenOrCreate, FileAccess.ReadWrite)) | ||
{ | ||
using (StreamWriter sw = new(fs)) | ||
{ | ||
myString2[line - 1] = newString; | ||
foreach (var s in myString2) | ||
{ | ||
sw.WriteLine(s); | ||
} | ||
} | ||
} | ||
|
||
// Async method | ||
Task.Run(async () => | ||
{ | ||
using (FileStream fs = new(file3, FileMode.OpenOrCreate, FileAccess.ReadWrite)) | ||
{ | ||
using (StreamWriter sw = new(fs)) | ||
{ | ||
sw.BaseStream.Position = fs.Seek(0, SeekOrigin.End); | ||
await sw.WriteLineAsync("Hello"); | ||
await sw.WriteLineAsync("Hi"); | ||
} | ||
} | ||
}).Wait(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
W6 D1/StreamWriterAndStreamReader/StreamWriterAndStreamReader.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This is line 1 | ||
This is line 2 | ||
This is line 3 | ||
This is line 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This is line 1 | ||
This is new line | ||
This is line 3 | ||
This is line 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Hello | ||
Hi | ||
Hello | ||
Hi | ||
Hello | ||
Hi | ||
Hello | ||
Hi |