Java三章学习内容(Reader,Data)

一.Reader

该方法是使用字符串进行读取,可以一行读取相应的信息

1.BufferedReader(读取)

思路:要使用先new相应BufferedReader输入流,该流需要FileReader 作为参数并构造相应目录,当条件都搭建后可通过br.readLine()方法在循环里遍历出来

br.readLine()            //读取一行数据
br.close();                //关闭流

参考代码:
public class Reader {
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br =null;

try {
fr = new FileReader("D:\\wuzhijie.txt"); //new输入流所需的参数和目录

br=new BufferedReader(fr); //new输入流

String str; //定义字符串类型

while ((str=br.readLine())!=null) { //使用br.readLine()方法依次赋值给str再通过循环遍历出来
System.out.println(str);
}


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if (fr!=null) {
fr.close();
}
if (br!=null) {
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}
2.BufferedWriter(写入
首先new BufferedWriter,该流需要Writer作为参数并构造相应目录,当条件都搭建后可通过bw.write(str);输出出来
bw.write(str);
bw.close();

参考代码:

public class WriterText {
public static void main(String[] args) {
Writer wt =null;
BufferedWriter bw =null;


try {
wt =new FileWriter("D:\\wuzhijie.txt"); //new输出流所需的参数和目录
bw =new BufferedWriter(wt); //new输出流
String str ="学到想哭"; //定义字符串类型
bw.write(str); //bw.write(str);方法写相应内容
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if (wt!=null) { //按顺序关,由下往上关
wt.close();
}
if (bw!=null) {
bw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

注:在日常读取文件或写文件出现中文字符时,可能出现中文编码格式问题,可以通过InputStreamReader作为中转站用于规定相应的编码格式(正常用UTF-8,也有GBK格式),改变的是读取字节流,不会对源文件进行改变,输出流也一样可以使用

  OutputStreamWriter isr = new OutputStreamWriter(is, "UTF-8");

参考代码:
public static void main(String[] args) {
InputStream is =null;
InputStreamReader sr = null;
BufferedReader bf = null;


try {
is=new FileInputStream("D:\\wuzhijie.txt");
sr = new InputStreamReader(is,"GBK"); //GBK,正常用UTF-8
bf = new BufferedReader(sr);
String str;

while ((str=bf.readLine())!=null) {
System.out.println(str);

}

二.data
DataInputStream和DataOutputStream
该方法是用于读取二进制文件(如音频,图片等都是二进制文件)

方法大概一样,先new对象,根据需求再 构造相应目录,当条件都搭建后可通过相应方法使用
ds.write(by);

参考代码:
public class DateText {
public static void main(String[] args) {
InputStream is = null;
DataInputStream di = null;
OutputStream os= null;
DataOutputStream ds = null;

try {
is = new FileInputStream("D:\\1.jpg"); //new输入流所需的参数和目录
di = new DataInputStream(is); //new输入流
os=  new FileOutputStream("D:\\2.jpg"); //new输出流所需的参数和目录
ds = new DataOutputStream(os); //new输出流

byte [] by = new byte[1024]; //创建相应变量数组
int length=0; //创建相应变量
while ((length=di.read(by))!=-1) { //使用di.read遍历数组by并赋给length变量
System.out.println(length);
ds.write(by); //把数据信息输出出去
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if (ds!=null) {
ds.close();
}
if (os!=null) {
os.close();
}
if (di!=null) {
di.close();
}
if (is!=null) {
is.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值