BufferedReader Class

last modified: Jan./5th/2002

$B%9%H%j!<%`$N%i%C%W(B

Reader $B%/%i%9$r7Q>5$7$?%5%V%/%i%9$N%9%H%j!<%`$O#1#6%S%C%HJ8;z%9%H%j!<%`$K$J$j$^$9$,!"FI$_9~$`$N$O0lJ8;z$:$D$G$9!#$3$l$G$O!"8F$S=P$7$4$H$K%U%!%$%k$+$i%P%$%H$rFI$_9~$_!"J8;z7?$KJQ49$7!"$=$N$?$S$KI|5"$9$k$?$a!"Hs>o$K8zN($,0-$/$J$j$^$9!#$3$l$KBP$7$F!"%F%-%9%H$r#19T$:$DFI$_9~$s$G!"8zN($r>e$2$k%9%H%j!<%`$,(B BufferedReader $B$G$9!#%$%s%9%?%s%92=$9$k;~$K!"(B Reader $B%/%i%9$N%5%V%/%i%9$r0z?t$KM?$($F!"$3$l$r(B BufferedReader $B%/%i%9$G%i%C%T%s%0$7$F$$$k$o$1$G$9!#(B

$B7Q>54X78!'(B

java.lang.Object
  |
  +--java.io.Reader
        |
        +--java.io.BufferedReader

$B%3%s%9%H%i%/%?!'(B

BufferedReader(Reader in)
BufferedReader(Reader in, int sz)

$B0z?t$O(B Reader $B7?$G$9$,!"$3$l$OCj>]%/%i%9$G$"$j!"sz $B$O:n@.$9$k%P%C%U%!$N%5%$%:$G$9!#(B

$B$3$N$h$&$KB>$N%9%H%j!<%`$rDL$7$FF~=PNO$9$k%9%H%j!<%`$r(B High-level I/O Streams $B$H8F$V$3$H$,$"$j$^$9!#$[$+$N%9%H%j!<%`$r%i%C%W$7$?>l9g$O!"30B&$N%9%H%j!<%`$+$i=gHV$K(B close() $B$7$^$9!#30B&$N%9%H%j!<%`$,@jM-$7$F$$$?%^%7%s!&%j%=!<%9$+$i=gHV$K2rJ|$7$F$$$/$3$H$K$J$j$^$9!#(B

$B%5%s%W%k(B

UNIX $B$G$O(B concatenate $B%3%^%s%I(B (cat) $B$H$$$&$b$N$,MQ0U$5$l$F$$$^$9!#0z?t$K;XDj$7$?%U%!%$%k$NFbMF$r%3%^%s%I%i%$%s$K$D$J$2$F=PNO$9$k%3%^%s%I$G$9!#

Cat.java:

import java.io.*;

class Cat {
	public static void main(String args[]) throws IOException {
		File inputFile = new File(args[0]);
		FileReader in = new FileReader(inputFile);
		BufferedReader br = new BufferedReader(in);

		String line;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}

		br.close();
		in.close();
	}
}

$B$rM?$($k$H!"%3%^%s%I%i%$%s$K0z$-B3$$$F!";XDj$7$?%U%!%$%k$NFbMF$,=PNO$5$l$^$9!#$3$N%5%s%W%k$G$OI8=`=PNO$X$N=PNO$r!"2~9T$D$-=PNO$G$"$k(B println() $B$G9T$C$F$$$k$3$H$KCm0U$7$F$/$@$5$$!#FI$_9~$_;~$K!"$b$H$b$H$N%U%!%$%k$N2~9T%3!<%I!J(B<LF> $B$d(B <CR>$B!K$O:o=|$5$l$F$$$^$9!#(B

File $B%/%i%9$O!"%U%!%$%kL>$@$1$G$O$J$/!"%G%#%l%/%H%j!"%\%j%e!<%`%i%Y%k!"%Q%9$bG'<1$7$^$9!#=>$C$F!"0z?t$K%U%k%Q%9(B (full path) $B$r;XDj$9$l$PG$0U$N%U%!%$%k$r;2>H$G$-$^$9!#C"$7!"%G%#%l%/%H%jL>$J$I$K6uGrN`J8;z$r4^$`>l9g$O!"Fs=E0zMQId!V(B"$B!W$G$/$/$C$F$*$+$J$$$H!"@5$7$/G'<1$5$l$:$KJ#?t$N0z?t$@$H2r

BufferedReader $B%/%i%9$N%3%s%9%H%i%/%?$O(B Reader $B7?$N0z?t$r$H$j$^$9$,!"(B FileReader $B%/%i%9$O(B Reader $B%/%i%9$N%5%V%/%i%9$J$N$G!"LdBj$J$/BeF~$G$-$^$9!#(B

$B$3$N%5%s%W%k$G$O!"(B BufferedReader $B%/%i%9$N(B readLine() $B%a%=%C%I$rMQ$$$^$7$?!#$3$N%a%=%C%I$O!"0l9T$N%F%-%9%H$rFI$_9~$_J8;zNs$rJV$7$^$9!#=*C\n $B$+(B \r $B$rG'<1$7!"FI$_9~$_$K=*Cnull $B$rJV$7$^$9!#9T6h@ZJ8;z$r4^$^$J$$$?$a$K!"$3$N%5%s%W%k$G$O2~9TJ8;z(B '\n' $B$rL@<(E*$K=PNO$7$^$7$?!#(B

C:\IO>javac Cat.java

C:\IO>java Cat test.txt
$B$3$s$K$A$O(B
Hello World.

$B$5$h$&$J$i(B
Bye World.

C:\IO>

$B$3$N%5%s%W%k$G$O!"FI$_9~$`%U%!%$%k$NJ8;zId9f2=J}K!$O!"%7%9%F%`$N%G%U%)%k%HJ8;zId9f2=J}K!$K0lCW$7$F$$$kI,MW$,$"$j$^$9!#$=$&$G$J$$$H!"%3%^%s%I%i%$%s$K$OJ8;z2=$1$r5/$3$7$F=PNO$5$l$^$9!#(B

$B=PNO$NJ8;zId9f2=J}K!$rL@<($7$?$$>l9g$O!"(B$BA0@a(B$B$G8+$?$h$&$K!"(B InputStreamReader/OutputStreamWriter $B%/%i%9$rMQ$$$^$9!#(B

CatEUC.java:

import java.io.*;

class CatEUC {
	public static void main(String args[]) throws IOException {

		File inputFile = new File(args[0]);
		FileInputStream infile = new FileInputStream(inputFile);
		InputStreamReader in = new InputStreamReader(infile, "EUC_JP");
		BufferedReader br = new BufferedReader(in);

		String line;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}

		br.close();
		in.close();
		infile.close();

	}
}

EUC-JP $B$GId9f2=$5$l$?%F%-%9%H%U%!%$%k(B test.txt $B$,B8:_$7$?>l9g$N

CatEUC.java $B$N<B9TNc(B

Cat.java $B$GCATEUC.java $B$Gl9g$O!"J8;z2=$1$,5/$3$j$^$;$s!#(B

UNIX $B7O(B OS $B$N(B cat $B%3%^%s%I$O!"%U%!%$%kL>$,J#?tM?$($i$l$k$H!"0l$D$N$b$N$K7k9g$7$F=PNO$5$l$^$9!#$3$N%5%s%W%k$b!"%3%^%s%I%i%$%s0z?t$N8D?t$r(B args.length $B$G=&$C$F!"(B for $B%k!<%W(B$B$5$;$k$3$H$G

$B%5%s%W%k(B

$BA0@a(B$B$N:G8e$K3NG'$7$?!"%F%-%9%H%U%!%$%k$K9THV9f$rIU2C$9$k%5%s%W%k$r!"0lJ8;z$:$D$G$O$J$/!"0l9T4]$4$H07$&$b$N$r:n$C$F$_$^$7$g$&!'(B

Buffered.java:

import java.io.*;

public class Buffered {
	public static void main(String[] args) throws IOException {
		//$B%U%!%$%kL>$N%$%s%9%?%s%92=(B
		File inputFile = new File("in.txt");
		File outputFile = new File("out.txt");

		//$BF~=PNO%9%H%j!<%`$N%$%s%9%?%s%92=(B
		FileReader in = new FileReader(inputFile);
		FileWriter out = new FileWriter(outputFile);

		//FileReader $B$N(B BufferedReader $B$K$h$k%i%C%T%s%0(B
		BufferedReader br = new BufferedReader(in);

		String str;
		int i = 1;
		while ((str = br.readLine()) != null) {
			out.write("\n"+i+": ");
			out.write(str);
			i++;
		}

		br.close();
		in.close();
		out.close();
	}
}

$BA0@a$N%5%s%W%k$h$j$b$9$C$-$j$7$F$$$^$9!#(B

$B=PNONc$O<($7$K$/$$$N$G$9$,!"$?$H$($P(B in.txt $B$,

in.txt:

In the beginning God created the heaven and the earth.
And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.
And God said, Let there be light: and there was light.
And God saw the light, that it was good: and God divided the light from the darkness.
And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day.

$B$3$N%F%-%9%H%U%!%$%k$,B8:_$9$k%G%#%l%/%H%j$G!"(B Buffered.java $B$rout.txt $B$,@8@.$5$l$^$9!'(B

out.txt:

1: In the beginning God created the heaven and the earth.
2: And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.
3: And God said, Let there be light: and there was light.
4: And God saw the light, that it was good: and God divided the light from the darkness.
5: And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day.

Reader $B%/%i%9$N%i%C%T%s%0(B

$B>e$N%5%s%W%k$G$OJ,$+$j$d$9$/$9$k$?$a$K!"%$%s%9%?%s%92=$No$O0J2<$N$h$&$K$^$H$a$F$7$^$$$^$9!'(B

BufferedReader br
	= new BufferedReader(new FileReader(new File(args[0])));
...$BCfN,(B...
br.close();

FileReader $B$N%*%V%8%'%/%H$rJD$8$kBe$o$j$K!"(B BufferedReader $B$N%*%V%8%'%/%H(B br $B$rJD$8$F$$$^$9!#(B FileReader $B7?%*%V%8%'%/%H$N;2>H$O!";2>H7?JQ?t$KBeF~$5$l$F$$$k$o$1$G$O$J$/!"(B br $B$N0z?t$K;H$o$l$F$*$j!"(B br $B$,;2>H$9$k%*%V%8%'%/%H$N%$%s%9%?%s%9$H$7$FJ];}$5$l$F$$$^$9!#(B

$B$7$?$,$C$F!"(B br $B$K4XO"$9$k%a%b%j$J$I$N%j%=!<%9$r2rJ|$7$F$bNI$$$H;X<($9$k$3$H(B (br.close()) $B$O!"0z?t$N(B FileReader $B7?%*%V%8%'%/%H$K4XO"$9$k%j%=!<%9$N2rJ|$b;X<($9$k$3$H$K$J$j$^$9!#(B



Copyright © 2002 SUGAI, Manabu. All Rights Reserved.
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送