Java day21

java day21
字节流
字节流的输出流
在这里插入图片描述
FileInputStream类
在这里插入图片描述
package demo01;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
public class demo02InputStrean {
public static void main(String[] args) throws IOException {
FileInputStream fis= new FileInputStream(“a.txt”);
//byte[] bytes =new byte[2];
//int len= fis.read(bytes);
//System.out.println(“读取到的长度”+len);
/System.out.println(bytes);
System.out.println(bytes[0]);//取出数组的值
System.out.println(bytes[1]);
System.out.println(Arrays.toString(bytes));
/
//System.out.println(new String(bytes));
len= fis.read(bytes);
System.out.println(“读取到的长度”+len);
System.out.println(new String(bytes));
len= fis.read(bytes);
System.out.println(len);
System.out.println(new String(bytes));
len= fis.read(bytes);
System.out.println(“读取到的长度”+len);
System.out.println(new String(bytes));
len= fis.read(bytes);
System.out.println(“读取到的长度”+len);
System.out.println(new String(bytes));//两个字节
byte[] bytes =new byte[9];
int len =0;
while((len=fis.read(bytes))!=-1) {
System.out.println(len);
System.out.println(new String(bytes));
}
}

}
字节流的输入流(InputStream)
在这里插入图片描述
package demo01;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class InputStrean {
public static void main(String[] args) throws IOException {
FileInputStream fis= new FileInputStream(“a.txt”);
int len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
len= fis.read();
System.out.println(len);
}
}
}
字符流
字符流输入(Reader)
java . io.Reader抽象类是表示用于读取字符流的所有类的超类,可以读取字符信息到内存中。它定义了字符输入流的基本共性功能方法
I public void close() :关闭此流并释放与此流相关联的任何系统资源
public int read() | :从输入流读取一一个字符
public int read(char[] cbuf) ]:从输入流中读取一些字符,并将它们存储到字符数组cbuf中
FileReader类
读取字符数据
package demo02;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo02Reaber {
public static void main(String[] args) throws IOException {
FileReader fis= new FileReader(“D:\exlipse\eclipse-workspace\Day21.java\a.txt”);
char[] cs = new char[14];
int len =0;
while((len =fis.read(cs))!=-1) {
System.out.println(len);
System.out.println(new String(cs));
}
}
}
使用字符数组读取
package demo02;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo02Reaber {
public static void main(String[] args) throws IOException {
FileReader fis= new FileReader(“D:\exlipse\eclipse-workspace\Day21.java\a.txt”);
char[] cs = new char[2];
int len =0;
while((len =fis.read(cs))!=-1) {
System.out.println(new String(cs));
}
}
}
字符输出流(Writer)
FileWriter类
package demo02;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo02Wreitre {
public static void main(String[] args) throws IOException {
FileWriter fw= new FileWriter(“D:\exlipse\eclipse-workspace\Day21.java\a.txt”);
fw.write(99);
fw.flush();
fw.write(100);
fw.flush();
fw.write(101);
fw.flush();
fw.write(102);
fw.flush();
fw.write(103);
fw.flush();
fw.close();
//fw.write(103);
}
}

2020080605024

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曾炟zd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值