diff --git a/FormulatrixBootcamp.sln b/FormulatrixBootcamp.sln index 2c82e00..beade2f 100644 --- a/FormulatrixBootcamp.sln +++ b/FormulatrixBootcamp.sln @@ -149,6 +149,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "W5 D5", "W5 D5", "{A966AD2E EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PreprocessingDirectives", "W5 D5\PreprocessingDirectives\PreprocessingDirectives.csproj", "{0AA3960D-7152-4FEE-B125-3C1E18FBE395}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "W6 D1", "W6 D1", "{BC48B9D4-ECEE-4451-B974-453C56155A59}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileStream", "W6 D1\FileStream\FileStream.csproj", "{39C38AE1-9B7A-4957-9C49-952A96ACF06A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamWriterAndStreamReader", "W6 D1\StreamWriterAndStreamReader\StreamWriterAndStreamReader.csproj", "{DE7BEB38-C495-41B5-A5F2-71691E61C55B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -383,11 +389,21 @@ Global {0AA3960D-7152-4FEE-B125-3C1E18FBE395}.Debug|Any CPU.Build.0 = Debug|Any CPU {0AA3960D-7152-4FEE-B125-3C1E18FBE395}.Release|Any CPU.ActiveCfg = Release|Any CPU {0AA3960D-7152-4FEE-B125-3C1E18FBE395}.Release|Any CPU.Build.0 = Release|Any CPU + {39C38AE1-9B7A-4957-9C49-952A96ACF06A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {39C38AE1-9B7A-4957-9C49-952A96ACF06A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {39C38AE1-9B7A-4957-9C49-952A96ACF06A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {39C38AE1-9B7A-4957-9C49-952A96ACF06A}.Release|Any CPU.Build.0 = Release|Any CPU + {DE7BEB38-C495-41B5-A5F2-71691E61C55B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DE7BEB38-C495-41B5-A5F2-71691E61C55B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DE7BEB38-C495-41B5-A5F2-71691E61C55B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DE7BEB38-C495-41B5-A5F2-71691E61C55B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution + {DE7BEB38-C495-41B5-A5F2-71691E61C55B} = {BC48B9D4-ECEE-4451-B974-453C56155A59} + {39C38AE1-9B7A-4957-9C49-952A96ACF06A} = {BC48B9D4-ECEE-4451-B974-453C56155A59} {0AA3960D-7152-4FEE-B125-3C1E18FBE395} = {A966AD2E-80DB-49E0-8070-B6DFE0C1E36D} {09D51696-9994-41B5-9959-9DFD1B331B02} = {5637E846-0989-4572-83A1-6CBC066E2D2D} {CCE2C0C8-CD08-44DA-AD87-32848B8B64A7} = {FC559EAF-E754-4775-A039-64B804F331A8} diff --git a/W6 D1/FileStream/FileStream.csproj b/W6 D1/FileStream/FileStream.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/W6 D1/FileStream/FileStream.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/W6 D1/FileStream/MyString.txt b/W6 D1/FileStream/MyString.txt new file mode 100644 index 0000000..5e1c309 --- /dev/null +++ b/W6 D1/FileStream/MyString.txt @@ -0,0 +1 @@ +Hello World \ No newline at end of file diff --git a/W6 D1/FileStream/MyString2.txt b/W6 D1/FileStream/MyString2.txt new file mode 100644 index 0000000..5740427 --- /dev/null +++ b/W6 D1/FileStream/MyString2.txt @@ -0,0 +1 @@ +Formulatrix Indonesia \ No newline at end of file diff --git a/W6 D1/FileStream/MyString3.txt b/W6 D1/FileStream/MyString3.txt new file mode 100644 index 0000000..58b1b97 --- /dev/null +++ b/W6 D1/FileStream/MyString3.txt @@ -0,0 +1 @@ +Formulatrix Bootcamp 5 \ No newline at end of file diff --git a/W6 D1/FileStream/Program.cs b/W6 D1/FileStream/Program.cs new file mode 100644 index 0000000..e59f5bc --- /dev/null +++ b/W6 D1/FileStream/Program.cs @@ -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) + { + + } + } + } +} diff --git a/W6 D1/StreamWriterAndStreamReader/Program.cs b/W6 D1/StreamWriterAndStreamReader/Program.cs new file mode 100644 index 0000000..0f013b3 --- /dev/null +++ b/W6 D1/StreamWriterAndStreamReader/Program.cs @@ -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(); + } +} \ No newline at end of file diff --git a/W6 D1/StreamWriterAndStreamReader/StreamWriterAndStreamReader.csproj b/W6 D1/StreamWriterAndStreamReader/StreamWriterAndStreamReader.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/W6 D1/StreamWriterAndStreamReader/StreamWriterAndStreamReader.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/W6 D1/StreamWriterAndStreamReader/Text1.txt b/W6 D1/StreamWriterAndStreamReader/Text1.txt new file mode 100644 index 0000000..861cc05 --- /dev/null +++ b/W6 D1/StreamWriterAndStreamReader/Text1.txt @@ -0,0 +1,4 @@ +This is line 1 +This is line 2 +This is line 3 +This is line 4 \ No newline at end of file diff --git a/W6 D1/StreamWriterAndStreamReader/Text2.txt b/W6 D1/StreamWriterAndStreamReader/Text2.txt new file mode 100644 index 0000000..26d958d --- /dev/null +++ b/W6 D1/StreamWriterAndStreamReader/Text2.txt @@ -0,0 +1,4 @@ +This is line 1 +This is new line +This is line 3 +This is line 4 diff --git a/W6 D1/StreamWriterAndStreamReader/Text3.txt b/W6 D1/StreamWriterAndStreamReader/Text3.txt new file mode 100644 index 0000000..b8ec6f7 --- /dev/null +++ b/W6 D1/StreamWriterAndStreamReader/Text3.txt @@ -0,0 +1,8 @@ +Hello +Hi +Hello +Hi +Hello +Hi +Hello +Hi