I tried to chew C # (reading and writing files)

In java, it was fisfos as below ...


package fileconvert;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Convert {

    public static void main(String[] args) throws IOException{
        //Read file
        FileInputStream fis=new FileInputStream("assets/sample.txt"); 
        InputStreamReader isr=new InputStreamReader(fis,"sjis");
        BufferedReader br=new BufferedReader(isr);
        //Writing a file
        FileOutputStream fos=new FileOutputStream("assets/sample2.txt");        
        OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8");
        BufferedWriter bw=new BufferedWriter(osw);

        String line;
        while((line=br.readLine())!=null) {
            bw.append(line);
            bw.newLine();
        }
        br.close();
        bw.flush();
        bw.close();
        System.out.println("done!");
        /*
        while(true) {
            String line=br.readLine();
            if(line == null) {
                break;
            }
            System.out.println(line);
        }
        */

    }

}

Use it like this in C

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WeightApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var path=@"./log.txt";
            while (true)
            {
                Console.WriteLine("Please enter the operation Please enter the operation 1...Enter your weight, 2...See list, 3...End>");
                var select = Console.ReadLine();
                switch (select)
                {
                    case "1":
                        Console.Write("Please enter your weight>");
                        var weight = double.Parse(Console.ReadLine());
                        using (var writer = new StreamWriter(path, true))

                        {
                            writer.WriteLine($"{DateTime.Now.ToString()} {weight}kg");
                        }
                        break;
                    case "2":
                        var file = new FileInfo(path);
                        //Is there or not
                        if (file.Exists)
                        {
                            using (var reader = new StreamReader(path))
                            {
                                string line;
                                while ((line = reader.ReadLine()) != null)
                                {
                                    Console.WriteLine(line);
                                }
                            }

                        }
                        else
                        {
                            Console.WriteLine("There is no data yet.");
                        }
                        break;
                    default:
                        Console.WriteLine("Exit the app.");
                        return;
                }
            }
        }
    }
}

It is recognition. I'm not proud, but only fisfos can understand the mechanism and memorize it.

It's a mystery myself. ..

Recommended Posts

I tried to chew C # (reading and writing files)
I tried to chew C # (indexer)
I tried to chew C # (polymorphism: polymorphism)
[Java] Reading and writing files with OpenCSV
Reading and writing gzip files in Java
I tried to link grafana and postgres [docker-compose]
I also tried WebAssembly with Nim and C
I tried to link JavaFX and Spring Framework.
[Review] Reading and writing files with java (JDK6)
I tried to read and output CSV with Outsystems
I started MySQL 5.7 with docker-compose and tried to connect
I tried to verify yum-cron
I tried to collect and solve Ruby's "class" related problems.
I tried to summarize the basics of kotlin and java
* Android * About saving / reading files to external storage and permissions
I tried to verify this and that of Spring @ Transactional
I tried to make Java Optional and guard clause coexist
I tried to summarize personally useful apps and development tools (development tools)
I tried to summarize personally useful apps and development tools (Apps)
[Rails] I tried to implement "Like function" using rails and js
I tried to integrate Docker and Maven / Netbean nicely using Jib
A person writing C ++ tried writing Java
I tried to summarize iOS 14 support
I tried connecting to Oracle Autonomous Database 21c with JDBC Thin
I tried to interact with Java
I tried to see if Koalas and Elasticsearch can work together
I tried to summarize the methods of Java String and StringBuilder
I tried to summarize Java learning (1)
I tried to generate a C language program source from cURL
I tried to understand nil guard
I tried to summarize Java 8 now
I tried to explain Active Hash
I introduced WSL2 + Ubuntu to Window10 and tried using GDC, DMD, LDC
I tried to summarize the key points of gRPC design and development
I tried to make my own transfer guide using OpenTripPlanner and GTFS
I made a virtual currency arbitrage bot and tried to make money
I introduced OpenAPI (Swagger) to Spring Boot (gradle) and tried various settings
I tried to measure and compare the speed of GraalVM with JMH
I tried to summarize the methods used
I tried to introduce CircleCI 2.0 to Rails app
I tried to summarize Java lambda expressions
I tried to get started with WebAssembly
I tried to solve AOJ's Binary Search
[Java] How to output and write files!
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried to build AdoptOpenjdk 11 on CentOS 7
I tried to build Ruby 3.0.0 from source
I tried to use Selenium like JQuery
I tried to touch JavaScript Part.2 Object-oriented
I tried to implement ModanShogi with Kinx
Java to C and C to Java in Android Studio
A story when I tried to make a video by linking Processing and Resolume
I tried to verify whether it would be fun to combine "programming" and "hobbies".
[JDBC ③] I tried to input from the main method using placeholders and arguments.
I tried to convert JavaBean and XML with Jackson formatter XML in this era
I tried to summarize about JVM / garbage collection
Scraping and writing specific elements to a file
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
I tried to implement polymorphic related in Nogizaka.