Java基础I/O流(一)

1、I/O流基本概念:

I/O流专门用于软件开发层面的理解,把数据从一个存储介质写出(output)或者读取(input)到另外一个存储介质的过程表示为I/O流的模型。

2、流的分类:

根据传输数据的类型分类
字节流:读取和写入数据的时候,以字节为最小单元进行读取和写入操作;
字符流:读取和写入数据的时候,以字符为最小单元进行读取和写入操作,因为每个字符根据编码不同,所占用的字节个数不同,每次读取和写入字符时候,
是不定长的字节 个数写入读取。
根据传输数据的方向分类
输入流:把数据从其他存储机制读入到当前内存机制当中;
输出流:把内存当中的数据写出到其他存储机制当中。

java中I/O的抽象基类:

3、字节流
InputStream派生的实现子类:FileputStream
示例1:
public class IOTest {
public static void main(String[] args) {
IOTest io=new IOTest();
io.readFile();
io.readFile2();

}
public void readFile(){
try {
FileInputStream fis=new FileInputStream("My.txt");

int value=fis.read();
while(value!=-1){
char c=(char)value;
System.out.print(c);
value=fis.read();


}
fis.close();
System.out.println();



} catch (Exception e) {
e.printStackTrace();
}


}
public void readFile2(){
try {
FileInputStream fis=new FileInputStream("My.txt");
byte[] by=new byte[5];
int value=fis.read(by);

while(value!=-1){
String str=new String(by,0,value);
System.out.print(str);
value=fis.read(by);


}
fis.close();


} catch (Exception e) {
e.printStackTrace();
}


}
}
运行结果:
12345678??±?
abcdefg
12345678随便
abcdefg


InputStream派生的实现子类:FileputStream
示例2:
public class FileOutTest {
public static void main(String[] args) {
FileOutTest fot = new FileOutTest();
fot.writeFile3();
}


public void writeFile1(){
try {
//创建一个文件输出流
FileOutputStream fos = new FileOutputStream("输出流测试文件.txt",false);
//往文件当中写出一个字节
fos.write(65);
//强制刷流
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public void writeFile2(){
//创建一个文件输出流
try {
FileOutputStream fos = new FileOutputStream("输出流测试文件.txt");
byte[] bytes = {48,49,50,51,2,4,7,9,10};
fos.write(bytes,1,3);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public void writeFile3(){
//创建一个文件输出流
try {
FileOutputStream fos = new FileOutputStream("输出流测试文件.txt");
String value = "热烈欢迎领美丽的某某大学";
//把字符串转为化为字节数组
byte[] bytes = value.getBytes();
fos.write(bytes);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}

运行结果:


4、字符流
Reader派生的子类FileReader
示例1:
public class ReaderTest {
public static void main(String[] args) throws Exception {
ReaderTest rt = new ReaderTest();
rt.charRead();
}


public void charRead() throws Exception {
// 根据本地的一个文件,构造一个文件字符输入流
FileReader fr = new FileReader("C:/Users/Administrator/Desktop/Test.txt");
// 使用FileReader的API函数
int value = fr.read();
while (value != -1) {
System.out.print((char) value);
value = fr.read();
}
fr.close();
}
public void charRead1() throws Exception {
// 根据本地的一个文件,构造一个文件字符输入流
FileReader fr = new FileReader("C:/Users/Administrator/Desktop/Test.txt");
// 使用FileReader的API函数
char[] chars = new char[5];
//读取指定字符数组长度的内容
int value = fr.read(chars);
while(value!=-1){
//把字符数组转化为字符串
String str = new String(chars,0,value);
System.out.print(str);
value = fr.read(chars);
}

fr.close();
}
}
运行结果:
你是好人你也是好人


Writer派生的子类FileWriter
示例2:
public class WriterTest {
public static void main(String[] args) throws Exception {
WriterTest wt = new WriterTest();
wt.writeFile1();
}


public void writeFile() throws Exception {
// 创建一个文件字符输出流
FileWriter fw = new FileWriter("C:/Users/Administrator/Desktop/test1.txt");
for (int i = 30000; i < 40000; i++) {
fw.write(i);
}
fw.flush();
fw.close();
}


public void writeFile1() throws IOException {
// 创建一个文件字符输出流
FileWriter fw = new FileWriter("C:/Users/Administrator/Desktop/test1.txt");
String str = "欢迎来到雁城衡阳";
// char[] chars = str.toCharArray();
// fw.write(chars);
fw.write(str);
fw.flush();
fw.close();
}
}

运行结果:



































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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值