IO文字ストリームFileReaderデータ読み出し

1306 ワード

import java.io.*;
class  FileReaderDemo
{
	public static void main(String[] args) throws IOException
	{
		// , 。
		FileReader fr = new FileReader("demo.txt");

		// 。 。
		char[] arr = new char[1024];
		int num = 0;
		while((num = fr.read(arr))!=-1)
		{
			System.out.println(new String(arr,0,num));
		}

		/*
		int x = fr.read(arr);
		
		System.out.println(new String(arr,0,x)+","+x);

		int y = fr.read(arr);

		System.out.println(new String(arr,0,y)+","+y);

		int z = fr.read(arr);
		System.out.println(z);
		*/
		fr.close();


	}
}

 
import java.io.*;
class  FileReaderDemo2
{
	public static void main(String[] args) throws IOException
	{
		FileReader fr = new FileReader("demo.txt");
		
		int ch = 0;

		while((ch=fr.read())!=-1)
		{
			System.out.print((char)ch);
		}

		/*
		int ch = fr.read();
		System.out.println(ch);
		int ch1 = fr.read();
		System.out.println(ch1);
		*/
		fr.close();


	}
}