Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Last active December 27, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/7409655 to your computer and use it in GitHub Desktop.
Save shigemk2/7409655 to your computer and use it in GitHub Desktop.
やさしいJava 14

FileWriterクラス…ファイルに書き込むための文字ストリーム BufferedWriterクラス…バッファを介して書き込むための文字ストリーム PrintWriterクラス…1行書き出すための文字ストリーム

FileReaderクラス…ファイルを読み込むための文字ストリーム BufferedReaderクラス…バッファを介して読み込むための文字ストリーム

  • 例外はtry catch finallyブロックを使って処理する
  • 独自の例外クラスを作るには、Throwableクラスのサブクラスを拡張します
  • 例外を送出するにはthrow文を使います
  • 例外を送出する可能性のあるメソッドにはthrowsをつけます
  • 入出力処理を行うにはストリームを利用します
  • コマンドライン引数を利用するとプログラムに引数をわたすことができます
// ArrayIndexOutOfBoundsExceptionエラーが出るが、
// プログラムを実行したときでないとこのエラーは分からない
class Sample1
{
public static void main(String[] args)
{
int[] test;
test = new int[5];
System.out.println("test[10]に値を代入します");
test[10] = 80;
System.out.println("test[10]に値を代入しました");
System.out.println("無事終了しました");
}
}
// コマンドライン引数を使う
import java.io.*;
class Sample10
{
public static void main(String[] args)
{
// 入力した文字列の個数を調べる
if(args.length != 1) {
System.out.println("ファイルを正しく指定してください");
System.exit(1);
}
// 入力した1番目の文字列(ファイル名)から文字ストリームを作成する
try {
BufferedReader br = new BufferedReader(new FileReader(args[0]));
String str;
while((str = br.readLine()) != null) {
System.out.println(str);
}
br.close();
}
catch(IOException e) {
System.out.println("入出力エラーです");
}
}
}
// ArrayIndexOutOfBoundsExceptionエラーが出るが、
// プログラムを実行したときでないとこのエラーは分からない
// 故に、例外処理を書く
class Sample2
{
public static void main(String[] args)
{
try {
int[] test;
test = new int[5];
System.out.println("test[10]に値を代入します");
test[10] = 80;
System.out.println("test[10]に値を代入しました");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("配列の要素をこえています");
}
System.out.println("無事終了しました");
}
}
// finallyブロック
class Sample3
{
public static void main(String[] args)
{
try {
int[] test;
test = new int[5];
System.out.println("test[10]に値を代入します");
test[10] = 80;
System.out.println("test[10]に値を代入しました");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("配列の要素をこえています");
}
finally {
System.out.println("最後に必ずこの処理をします");
}
System.out.println("無事終了しました");
}
}
// 例外の情報を出力する
class Sample4
{
public static void main(String[] args)
{
try {
int[] test;
test = new int[5];
System.out.println("test[10]に値を代入します");
test[10] = 80;
System.out.println("test[10]に値を代入しました");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("配列の要素をこえています");
System.out.println(e+"という例外が発生しました");
}
System.out.println("無事終了しました");
}
}
// 独自の例外を宣言する
class CarException extends Exception
{
}
// 車クラス
class Car
{
private int num;
private double gas;
public Car()
{
num = 0;
gas = 0.0;
System.out.println("車を作成しました");
}
// 例外を送出する可能性のあるメソッドであることを宣言します
public void setCar(int n, double g) throws CarException
{
if (g < 0) {
CarException e = new CarException();
// 作成したオブジェクトを送出します
throw e;
}
else {
num = n;
gas = g;
System.out.println("ナンバーを"+num+"ガソリン量を"+gas+"にしました");
}
}
public void show()
{
System.out.println("車のナンバーは"+this.num+"です");
System.out.println("ガソリン量は"+this.gas+"です");
}
}
class Sample5
{
public static void main(String[] args)
{
Car car1 = new Car();
try {
// この呼び出しで例外が送出される
car1.setCar(1234, -10.0);
}
catch(CarException e) {
System.out.println(e+"が送出されました");
}
car1.show();
// 例外 CarException は報告されません。スローするにはキャッチまたは、スロー宣言をしなければなりません。エラー
// Car car2 = new Car();
// car2.setCar(1234, -10.0);
// car2.show();
}
}
// 入出力を支える概念「ストリーム」の仕組み
import java.io.*;
class Sample6
{
public static void main(String[] args)
{
System.out.println("文字列を入力してください");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
System.out.println(str+"が入力されました");
}
catch(IOException e) {
System.out.println("入出力にエラーです");
}
}
}
// ファイルに出力する
// 1. ファイル名を指定して、FileWriterクラスのオブジェクトを作成する
// 2. 1からBufferedWriterクラスのオブジェクトを作成する
// 3. 2からPrintWriterクラスのオブジェクトを作成する
// 4. 3のprintlnメソッドを使って1行データを書き出す
import java.io.*;
class Sample7
{
public static void main(String[] args)
{
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.txt")));
pw.println("Hello!");
pw.println("GoodBye!");
System.out.println("ファイルに書き込みました");
pw.close();
}
catch(IOException e) {
System.out.println("入出力エラーです");
}
}
}
// ファイルから入力する
// 1. ファイル名を指定して、FileReaderクラスのオブジェクトを作成する
// 2. 1からBufferedReaderクラスのオブジェクトを作成する
// 3. 2のreadLine()メソッドを使って1行データを読み込む
import java.io.*;
class Sample8
{
public static void main(String[] args)
{
try {
BufferedReader br = new BufferedReader(new FileReader("test1.txt"));
String str1 = br.readLine();
String str2 = br.readLine();
System.out.println("ファイルに書き込まれている2つの文字列は");
System.out.println(str1+"です。");
System.out.println(str2+"です。");
br.close();
}
catch(IOException e) {
System.out.println("入出力エラーです");
}
}
}
// 大量のデータをファイルから入力する
import java.io.*;
class Sample9
{
public static void main(String[] args)
{
try {
BufferedReader br = new BufferedReader(new FileReader("test2.txt"));
int[] test = new int[8];
String str;
for(int i=0; i<test.length; i++) {
str = br.readLine();
test[i] = Integer.parseInt(str);
}
int max = test[0];
int min = test[0];
for(int i=0; i<test.length; i++) {
if(max < test[i])
max = test[i];
if(min > test[i])
min = test[i];
System.out.println(test[i]);
}
System.out.println("最高点は"+max+"です");
System.out.println("最高点は"+min+"です");
br.close();
}
catch(IOException e) {
System.out.println("入出力エラーです");
}
}
}
// ファイルに出力する
import java.io.*;
class SampleP2
{
public static void main(String[] args)
{
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("testP1.txt")));
pw.println("A long time ago,");
pw.println("There was a little girl.");
System.out.println("ファイルに書き込みました");
pw.close();
}
catch(IOException e) {
System.out.println("入出力エラーです");
}
}
}
import java.io.*;
class SampleP3
{
public static void main(String[] args)
{
// 入力した文字列の個数を調べる
if(args.length != 1) {
System.out.println("ファイルを正しく指定してください");
System.exit(1);
}
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(args[0])));
pw.println("A long time ago,");
pw.println("There was a little girl.");
System.out.println("ファイルに書き込みました");
pw.close();
}
catch(IOException e) {
System.out.println("入出力エラーです");
}
}
}
Hello!
GoodBye!
80
68
22
33
56
78
33
56
A long time ago,
There was a little girl.
A long time ago,
There was a little girl.
A long time ago,
There was a little girl.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment