InputStream和OutputStream

输出流: OutputStream和Writer 基类

输入流:InputStream和Reader 基类


字节流:InputStream和OutputStream

字符流:Reader和Writer

字节流是8位通用字节流,字符流是16位Unicode字符流


流是一组有序的数据序列,以先进先出方式发送信息的通道
Java输入输出流:输入输出流是相对于计算机内存来说的
字节流:
输入流:InputStream抽象类   必须保证文件存在
使用子类FileInputStream 
int read( ) ASCII码  从输入流中读取下一个字节,返回0-255之间的int值.返回值代表着读出来的字节对应的整形数字
int read(byte[] b) 一次性获取全部的ASCII 从输入流中读取一堆字节,把这些字节存储在字节数组中。返回值代表着真实的读到的字节的个数(b的长度)
int read(byte[] b,int off,int len)
从输入流中读取一堆字节,把这些字节存储在字节数组中。off对应的是b开始存储字节的标识或下表(将读取的第一个字节存储在元素b[off]中)。len对应的是读到的字节的个数或长度。
void close( )
int available():可以从输入流中读取的字节数目
子类 FileInputStream 常用的构造方法
FileInputStream(File file)
FileInputStream(String name)

a.引入相关类

import java.io.IOException;
import java.io.FileInputStream;

b.创建输入流对象
FileInputStream fis= new
FileInputStream("c:\\test.txt");

c.读取文本文件
fis.available();
fis.read();

d.关闭流
fis.close();

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class InputDemo {
	public static void main(String[] args) {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("D:\\liangsong\\hello.txt");
			int len = fis.available();
			System.out.println("文件可读取的字节数:"+len);
			int data;
			while((data = fis.read()) != -1){
				System.out.print((char)data);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}



OutputStream抽象类常用方法
void write(int c)
void write(byte[] buf)
void write(byte[] b,int off,int len)
void close()
void flush():强制把缓冲区的数据写到输出流中
子类FileOutputStream常用的构造方法
FileOutputStream (File file)
FileOutputStream(String name)
FileOutputStream(String name,boolean append)
FileOutputStream
1、前两种构造方法在向文件写数据时将覆盖文件中原有的内容
2、创建FileOutputStream实例时,如果相应的文件并不存在,
则会自动创建一个空的文件


import java.io.IOException;
import java.io.FileOutputStream;
String str ="好好学习Java";
byte[] words = str.getBytes();
fos.write(words, 0, words.length);
fos.close();


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class OutputDemo {
public static void main(String[] args) {
FileOutputStream fos = null;

try {
fos = new FileOutputStream("D:\\liangsong\\hello.txt",true);
String mess = "good good study,day day up!";
byte[] words = mess.getBytes();
fos.write(words,0,words.length);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


字节流中文乱码问题:
InputStreamReader(inputStream,encoding)
String(byte[],encoding)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值