2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

かみ砕いてみたC#(ファイルの読み書き)

Last updated at Posted at 2019-10-07

javaなら下記のようにfisfosでしたが・・


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{
        //ファイルの読み込み
        FileInputStream fis=new FileInputStream("assets/sample.txt"); 
        InputStreamReader isr=new InputStreamReader(fis,"sjis");
        BufferedReader br=new BufferedReader(isr);
        //ファイルの書き込み
        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);
        }
        */

    }

}

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("操作を入力してください操作を入力してください1...体重を入力、2...一覧を見る、3...終了>");
                var select = Console.ReadLine();
                switch (select)
                {
                    case "1":
                        Console.Write("体重を入力してください>");
                        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);
                        //あるかないか
                        if (file.Exists)
                        {
                            using (var reader = new StreamReader(path))
                            {
                                string line;
                                while ((line = reader.ReadLine()) != null)
                                {
                                    Console.WriteLine(line);
                                }
                            }

                        }
                        else
                        {
                            Console.WriteLine("まだデータがありません。");
                        }
                        break;
                    default:
                        Console.WriteLine("アプリを終了します。");
                        return;
                }
            }
        }
    }
}

って認識です。
自慢ではありませんがfisfosだけは仕組みを理解して暗記できてます。

自分でも謎ですが。。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?