0
点赞
收藏
分享

微信扫一扫

I/O案例实操


 

/*
*作者:呆萌老师
*☑csdn认证讲师
*☑51cto高级讲师
*☑腾讯课堂认证讲师
*☑网易云课堂认证讲师
*☑华为开发者学堂认证讲师
*☑爱奇艺千人名师计划成员
*在这里给大家分享技术、知识和生活
*各种干货,记得关注哦!
*/

I/O案例实操_数组

 创建与文件关联的字节输出流对象 FileOutputStream fos = new
         FileOutputStream("c:\\cn8.txt"); //创建可以把字符转成字节的转换流对象,并指定编码
        OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
         调用转换流,把文字写出去,其实是写到转换流的缓冲区中 osw.write("你好");//写入缓冲区。 osw.close();
           创建读取文件的字节流对象
      

try {
in = new FileInputStream("d:/aa/5.txt");

// 创建转换流对象
// InputStreamReader isr = new
// InputStreamReader(in);这样创建对象,会用本地默认码表读取,将会发生错误解码的错误
InputStreamReader isr = new InputStreamReader(in, "gbk");
// 使用转换流去读字节流中的字节
int ch = 0;
while ((ch = isr.read()) != -1) {
System.out.println((char) ch);
}
// 关闭流
isr.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block

I/O案例实操_数组_02

 字符输出流

// TODO Auto-generated method stub

File file = new File("d:/aa/5.txt");

try {
// 字符输出流
FileWriter fileWriter = new FileWriter(file, true); // 字符输出流
// true代表追加

fileWriter.write("你好");

fileWriter.close();

// 字节输出流

FileOutputStream fileOutputStream = new FileOutputStream(file, true);

fileOutputStream.write('a');

fileOutputStream.close();

} catch (IOException e) {
// TODO Auto-generated catch block

I/O案例实操_输出流_03

字符输入流

      

//字符输入流  读  文本文件的读写(.java .txt...)

//1.创建对象

File file=new File("d:/aa/4.txt");



//2.创建字符输入流对象

try {

FileReader fileReader=new FileReader(file);

int a;



a= fileReader.read(); //读取一个字符



System.out.println((char)a);



char[] arr=new char[3];



fileReader.read(arr);//读取多个字符到数组中



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



//循环读取文件的内容

while((a=fileReader.read())!=-1)

System.out.println((char)a);



fileReader.close();



} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


        
       

字符输出流+文件

//字符输出流  写出
//1.创建文件对象
File file=new File("d:/aa/5.txt");
//2.创建字符输出流
try {

FileWriter fileWriter=new FileWriter(file);

fileWriter.write("bb\r\n"); //换行

fileWriter.write("呆萌");

fileWriter.close();

} catch (IOException e) {
// TODO Auto-generated catch block

I/O案例实操_转换流_04

//字节输入流  从文件中读数据进来 一个字节一个字节的读取    (图片 视频文件的读写)
//(一个英文字符或数字字符对应一个字节 一个中文字对应的是两个字节,所以读中文要用字符流)
//1.创建文件对象
File file=new File("d:/aa/2.txt");
//2.创建一个输入流对象
try {

FileInputStream fileInputStream=new FileInputStream(file);

//3.读数据
int a=fileInputStream.read(); //读取下一个字符 读到了返回当前字符的ascII码 读到文件结尾 则返回-1

System.out.println((char)a);

a=fileInputStream.read();

System.out.println((char)a);

//读取多个字节到数组中
byte[] arr=new byte[3];
fileInputStream.read(arr); //读取3个字节存入arr数组中

System.out.println(new String(arr, 0, 3));

//读取整个文件
while((a=fileInputStream.read())!=-1)
System.out.print((char)a);
//关闭文件
fileInputStream.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block

I/O案例实操_转换流_05

字节输出流+文件操作 

//字节输出流  将数据写到文件中

//1.创建文件对象
File file=new File("d:/aa/3.txt");

//2.创建字节输出流
try {

FileOutputStream fileOutputStream=new FileOutputStream(file);

//3.写内容
fileOutputStream.write('a'); //一次写入一个字节

byte[] arr=new byte[]{'b','c','d'}; //写入一个byte类型的数组

String string="daimeng";

byte[] brr=string.getBytes(); //可以将字符串转换为字节数组

fileOutputStream.write(brr);

fileOutputStream.close(); //关闭流




} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block

I/O案例实操_数组_06

更多了解

​​https://edu.51cto.com/course/20124.html​​

举报

相关推荐

0 条评论