java io字节流 inputstream_JAVA 的IO操作(二) 字节输出流和输入流:OutputStream和InputStream...

一、字节输出流:OutputStream

Class OutputStream

public abstract class OutputStream

extends Object

implements Closeable, Flushable 关于Closeable和Flushable接口的说明:

Closeable接口

public interface Closeable{

void close() throws IOException

}

Flushable接口

public interface Flushable{

void flush() throws IOException

}

从两个接口的定义发现,一个用来表示关闭,一个用来表示刷新。

字节输出流:OutputStream的常用方法:

void

Closes this output stream and releases any system resources associated with this stream.

关闭此输出流并释放与此流有关的所有系统资源。

void

Flushes this output stream and forces any buffered output bytes to be written out.

刷新此输出流并强制写出所有缓冲的输出字节。

void

Writes

b.length bytes from the specified byte array to this output stream.

b.length 个字节从指定的 byte 数组写入此输出流。

void

Writes

len bytes from the specified byte array starting at offset

off to this output stream.

将指定 byte 数组中从偏移量

off 开始的

len 个字节写入此输出流。

abstract void

Writes the specified byte to this output stream.

将指定的字节写入此输出流。

列1:

package org.yts.iodemo;

import java.io.File;

import java.io.FileOutputStream;

/*

* 向一个文件中写入数据-->使用.write(byte[] b)或者.write(byte[] b,int off,int len)

* 或者通过for循环,挨个写进每个b[i];

*/

public class OutputStreamDemo01 {

public static void main(String args[]) throws Exception {

File file = new File("e:" + File.separator + "text.txt");

FileOutputStream out = new FileOutputStream(file);

String string = "hello 中间a 有中文 world!!!";

byte[] b = string.getBytes();

//out.write(b);

//out.write(b,0,b.length);

for(int i=0;i

out.write(b[i]);

}

out.close();

}

}

列2:

package org.yts.iodemo;

import java.io.File;

import java.io.FileOutputStream;

/*

* 若向追加内容,在new-->FileOutputStream时候

* 使用FileOutputStream(File fle,boolean append)

*

* 若想增加换行,-->在文字钱加\r\n

* Unix系统里,每行结尾只有“”,即“\n”;

* Windows系统里面,每行结尾是“”,即“\r\n”;

* Mac系统里,每行结尾是“”

*/

public class OutputStreamDemo02 {

public static void main(String args[]) throws Exception {

File file = new File("e:" + File.separator + "text.txt");

FileOutputStream out = new FileOutputStream(file,true);

//String string = "在原来后面再追加";

String string = "\r\n若想换行在前面(\\r\\n)";

byte[] b = string.getBytes();

//out.write(b);

//out.write(b,0,b.length);

for(int i=0;i

out.write(b[i]);

}

out.close();

}

}

二、字节输入流:InputStream

字节输入流:InputStream的常用方法:

int

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数。

void

Closes this input stream and releases any system resources associated with the stream.

关闭此输入流并释放与该流关联的所有系统资源。

void

Marks the current position in this input stream.

在此输入流中标记当前的位置。

boolean

Tests if this input stream supports the

mark and

reset methods.

测试此输入流是否支持

mark 和

reset 方法。

abstract int

Reads the next byte of data from the input stream.

从输入流中读取数据的下一个字节。

int

Reads some number of bytes from the input stream and stores them into the buffer array

b.

从输入流中读取一定数量的字节,并将其存储在缓冲区数组

b 中。

int

Reads up to

len bytes of data from the input stream into an array of bytes.

将输入流中最多

len 个数据字节读入 byte 数组。

void

Repositions this stream to the position at the time the

mark method was last called on this input stream.

将此流重新定位到最后一次对此输入流调用

mark 方法时的位置。

long

Skips over and discards

n bytes of data from this input stream.

跳过和丢弃此输入流中数据的

n 个字节。

列1:

package org.yts.iodemo;

import java.io.File;

import java.io.FileInputStream;

/*

* 使用FileInputStream读取某文件里面的内容。

* 注意输入文件的路径。在使用FileInputStream读取时,如果指定的路径不存在,则程序

* 会出现异常

*/

public class InputStreamDemo01 {

public static void main(String args[]) throws Exception{

File file = new File("e:" + File.separator + "text.txt");

FileInputStream input = new FileInputStream(file);

byte[] b = new byte[1024];

/*(1)

* 使用input.read(byte[]b)方法。把读取到的内容放入到开辟出来的byte数组b中。

* 但是此时开辟出来的容量过大,打印System.out时会有大量空格。

*/

//input.read(b);

//System.out.println(new String(b));

//(2)这时,推荐使用.read(byte[] b)该方法时候,前面加int类型的常量,来计算读取的长度

//然后使用new String(byte[] bytes,int offset,int length)来截取需要的内容

int len = input.read(b);

System.out.println(new String(b,0,len));

/*(3)

* 以上第2种方式,可以解决最后字符串空格的问题,但是同样会造成空间浪费的情况,

* 我们可以从File类着手,因为在File类中存在一个length()方法,可以得到文件的大小

* 从而决定开辟byte[]数组时应开辟多大的空间,(取长度时,别忘转型,转int型)

*/

//byte[] b = new byte[(int)file.length()];

//input.read(b);

//System.out.println(new String(b));

input.close();

}

}

列2:

package org.yts.iodemo;

import java.io.File;

import java.io.FileInputStream;

/*

* 除demo01介绍的方式外,还可以循环从文件中一个个地把内容读取出来,

* 直接使用read()方法即可。

*/

public class InputStreamDemo02 {

public static void main(String args[]) throws Exception{

File file = new File("e:" + File.separator + "text.txt");

FileInputStream input = new FileInputStream(file);

byte[] b = new byte[(int) file.length()];

for(int i=0;i

b[i] = (byte) input.read();

}

System.out.println(new String(b));

input.close();

}

}

列3:

package org.yts.iodemo;

import java.io.File;

import java.io.FileInputStream;

/*

* demo01和demo02都是在明确知道了具体数组大小的前提下开展的,

* 此时如果不知道要输入的内容有多大,则只能通过

* 判断是否读到文件末尾的方式来读取文件。

*/

public class InputStreamDemo03 {

public static void main(String args[]) throws Exception{

File file = new File("e:" + File.separator + "text.txt");

FileInputStream input = new FileInputStream(file);

int len = 0;//用于记录读取的数据个数

byte[] b = new byte[1024];

int temp = 0;//接收读取的每一个内容

while((temp = input.read()) != -1){//读到末尾,返回-1

b[len] = (byte) temp;

len++;

}

input.close();

System.out.println(new String(b,0,len));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值