java流的应用,JAVA IO流的应用

JAVA IO流的应用

IO流的典型应用

尽管库内存在大量IO流类,可通过多种不同的方式组合到一起,但实际上只有几种方式才会经常用到。然而,必须小心在意才能得到正确的组合。下面这个相当长的例子展示了典型IO配置的创建与使用,可在写自己的代码时将其作为一个参考使用。注意每个配置都以一个注释形式的编号起头,并提供了适当的解释信息。

//: IOStreamDemo.java

// Typical IO Stream Configurations

import java.io.*;

import com.bruceeckel.tools.*;

public class IOStreamDemo {

public static void main(String[] args) {

try {

// 1. Buffered input file

DataInputStream in =

new DataInputStream(

new BufferedInputStream(

new FileInputStream(args[0])));

String s, s2 = new String();

while((s = in.readLine())!= null)

s2 += s + "\n";

in.close();

// 2. Input from memory

StringBufferInputStream in2 =

new StringBufferInputStream(s2);

int c;

while((c = in2.read()) != -1)

System.out.print((char)c);

// 3. Formatted memory input

try {

DataInputStream in3 =

new DataInputStream(

new StringBufferInputStream(s2));

while(true)

System.out.print((char

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值