java中 i,Java中的I/O (一)

Java中的I/O (1)

目标

1. I/O 操作的目标;

2. I/O 分类;

3. 读取文件和写入文件的方法。

是指:数据源与程序之间的通道。

I/O的流向:

[文件]     Input                    Output       [文件]

[键盘]     ------>   Java程序  ------->        [屏幕]

[网络]                                                    [网络]

--------------------

第1种分类:1. 输入流 2. 输出流

第2种分类:1. 字节流 2.字符流

第3种分类:1. 节点流 2.处理流

---------------------------------------

字节流

的核心类

extends

InputStream  

extends

OutputStream

----------------------------------------

InputStream:

int read(byte [] buffer, int offset, int length);

OutputStream:

void write (byte [] buffer, int offset, int length);

调用一个String对象的tring()方法去除掉这个字符串的首尾空格和空字符。

public

String

trim

()

Copies this string removing white space characters from the beginning and

end of the string.

//导入类

import java.io.InputStream;

import java.io.FileInputStream;

import java.io.OutputStream;

import java.io.FileOutputStream;

class Test{

public static void main(String args []){

//声明输入流的引用

FileInputStream fis = null;

//声明输出流的引用

FileOutputStream fos = null;

try{

//生成代表输入流的对象

fis = new FileInputStream("m:/src/from.txt");

//生成代表输出流的对象

fos = new FileOutputStream("m:/src/to.txt");

//生成一个字节数组

byte [] buffer = new byte[1024];

while (true) {

//调用输入流对象的read方法,读取数据

int temp = fis.read(buffer, 0, buffer.length);

if (temp == -1) {

break;

}

fos.write(buffer, 0, temp);

}

//String s = new String(buffer);

//Copies this string removing white space characters

//from the beginning and end of the string.

//s = s.trim();

//System.out.println(s);

}

catch (Exception e) {

System.out.println(e);

}

finally {

try{

fis.close();

fos.close();

}

catch (Exception e) {

System.out.println(e);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值