tanakahdaのプログラマ手帳

プログラミングとかソフトウェア開発とかの備忘録

テキスト読み込み@Python

レガシーな書き方

/data/data.txt

Hello World!
file = open("./data/data.txt")
text = file.read()
file.close()
print(text)
Hello World!

with open()の書き方

# with open()構文でファイルを開くと、ファイルの入出力が、構文内だけで実行される
# 明示的にファイルオープンが完了するので、閉じ忘れの心配がない。
with open("./data/data.txt") as file:
    print(file.read())

Generics@Java

package com.tanakahda.apps.generics;

public class GenericsExample<T> {
    private T value;
    
    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public GenericsExample(T value) {
        this.value = value;
    }
    
}
package com.tanakahda.apps.generics;

public class Main {

    public static void main(String[] args) {
        GenericsExample<String> hoge = new GenericsExample<String>("Hello");
        System.out.println(hoge.getValue());
        
        GenericsExample<Integer> foo = new GenericsExample<Integer>(100);
        System.out.println(foo.getValue());
    }

}
Hello
100

Hello SpringBoot2@Java

HelloController.java

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
    
    @RequestMapping("/")
    public String index() {
        return "hello";
    }
    
}

hello.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
    <h2>Hello World</h2>
</body>
</html>

構成

f:id:tanakahda:20220209005757p:plain

実行

f:id:tanakahda:20220209005835p:plain

結果

f:id:tanakahda:20220209005902p:plain

Recordクラス@Java

package com.tanakahda.apps.record;

public record Vehicle(int gas) {
}
package com.tanakahda.apps;

import com.tanakahda.apps.record.Vehicle;

public class Main {
    public static void main(String[] args) {
        var car = new Vehicle(60);
        System.out.println(car.gas());
    }
}
60