JAVA入门到精通-第45讲-IO编程

1167156-20181130180837224-2056913539.png
image1=ImageIO.read(new File("bomb_1.gif"));
图片拷贝:
1167156-20181130180837629-323614417.png
输入流和输出流同时要用的问题:
图片读入到内存;
写入,输出到某个文件;
二进制文件,字节流完成;
1167156-20181130180837893-970005373.png
FileInputStream fis=new FileInputStream("xxxx.jpg");
输 出到指定文件: FileOutputStream  fos=null;
1167156-20181130180838161-259203793.png
关闭打开的文件流:
1167156-20181130180838426-260963214.png

------------------------------------------
1167156-20181130180838694-537583915.png
FileReader,FileWriter类,文件字符流的对象;
Reader读进来,Writer写出去;

byte 是一位,不足以表示汉字;
char 是两位,可以表示一个汉字;
1167156-20181130180838977-1352184880.png
达到文件的末尾,返回-1;
1167156-20181130180839248-1154320828.png
char是1024个字符;
1167156-20181130180839492-1786069994.png
String s=new String(c,0,n);
这样,就没有乱码;

1167156-20181130180839718-1357232198.png
关闭文件:finally---fr.close---fw.close
fw.write(c,0,n); 读取文件,防止乱码;
1167156-20181130180840032-1630417164.png

因为,发现JAVA开发者效率还是不够高,
提出缓冲字符流:提高效率;
一个一个字节/字符读取,速度太慢;
1167156-20181130180840346-923070276.png
是一种编译异常,
1167156-20181130180840680-2137587777.png
BufferedReader缓存为对象;
先通过FileReader找到一个文件,
然后通过BufferReader进行缓存;
1167156-20181130180840916-1967317135.png
1167156-20181130180841165-1225172127.png
readLine不读取换行的;末尾,返回null;
1167156-20181130180841434-501816322.png
一行一行的读取,速度比较快;

BufferedWriter输出到磁盘;
FileWriter找到对象;
BufferedWriter输出到缓存;

文件的关闭:文件不关闭,后果很严重:
1167156-20181130180841656-1835481854.png
换行:\r\n
1167156-20181130180842505-2054764452.png

坦克存盘退出:
记录坦克的坐标
1167156-20181130180842921-1305678359.png
一行行读进来,让坦克暂停;
记事本功能的实现:
1167156-20181130180843337-1719225991.png
1167156-20181130180843744-1350842683.png
---------------------------------------------
java文件编程--常用io流 常用io流--文件字节流 1、案例[Io02.java]:读取文件(文件字节输入流使用,目的:FileInputStream类)把用FileInputStream的对象把文件读入到内存 /** * File类的基本用法 * io流--文件字节流 * FileInputStream类的使用 */ import java.io.*; public class Io02 { public static void main(String[] args) { //得到一个文件对象,f指向e:\ff\hsp.txt文件 File f=new File("e:\\ff\\hsp.txt"); FileInputStream fis=null; try { //因为File没有读写的能力,所以需要使用InputStream类 fis=new FileInputStream(f); //定义一个字节数组,相当于缓存 byte []bytes=new byte[1024]; int n=0;//得到实际读取到的字节数 //循环读取 while((n=fis.read(bytes))!=-1){ //把字节转成String String s=new String(bytes,0,n); System.out.println(s); } } catch (Exception e) { e.printStackTrace(); }finally{ //关闭文件流必需放在finally语句块中 try { fis.close(); } catch (Exception e) { e.printStackTrace(); } } } }
x
39
 
1
java文件编程--常用io流
2
常用io流--文件字节流
3
1、案例[Io02.java]:读取文件(文件字节输入流使用,目的:FileInputStream类)把用FileInputStream的对象把文件读入到内存
4
/**
5
 * File类的基本用法
6
 * io流--文件字节流
7
 * FileInputStream类的使用
8
 */
9
import java.io.*;
10
public class Io02 {
11
    public static void main(String[] args) {
12
        //得到一个文件对象,f指向e:\ff\hsp.txt文件
13
        File f=new File("e:\\ff\\hsp.txt");
14
        FileInputStream fis=null;
15
        try {
16
            //因为File没有读写的能力,所以需要使用InputStream类
17
            fis=new FileInputStream(f);
18
            //定义一个字节数组,相当于缓存
19
            byte []bytes=new byte[1024];
20
            int n=0;//得到实际读取到的字节数
21
            //循环读取
22
            while((n=fis.read(bytes))!=-1){
23
                //把字节转成String
24
                String s=new String(bytes,0,n);
25
                System.out.println(s);
26
            }
27
        } catch (Exception e) {
28
            e.printStackTrace();
29
        }finally{
30
            //关闭文件流必需放在finally语句块中
31
            try {
32
                fis.close();
33
            } catch (Exception e) {
34
                e.printStackTrace();
35
            }
36
        }
37
    }
38
}
39
 
          

2、案例[Io03.java]:从键盘接收用户输入内容,并保存到文件中(文件字节输出流,目的:FileOutputStream)


/** * File类的基本用法 * io流--文件字节流 * FileOutputStream类的使用 */ import java.io.*; public class Io03 { public static void main(String[] args) { File f=new File("e:\\ff\\ss.txt");//直接覆盖写同一个文件 //字节输出流 FileOutputStream fos=null; if(f.exists()){ System.out.println("文件已存在"); }else{ try { fos=new FileOutputStream(f); String s="hello,world!\r\n"; String s1="中国人"; fos.write(s.getBytes()); fos.write(s1.getBytes()); } catch (Exception e) { e.printStackTrace(); }finally{ try { fos.close(); } catch (Exception e2) { e2.printStackTrace(); } } } } }
1
33
 
1
/**
2
 * File类的基本用法
3
 * io流--文件字节流
4
 * FileOutputStream类的使用
5
 */
6
import java.io.*;
7
public class Io03 {
8
    public static void main(String[] args) {
9
        File f=new File("e:\\ff\\ss.txt");//直接覆盖写同一个文件
10
                //字节输出流
11
        FileOutputStream fos=null;
12
        if(f.exists()){
13
            System.out.println("文件已存在");
14
        }else{
15
            try {
16
                fos=new FileOutputStream(f);
17
                String s="hello,world!\r\n";
18
                String s1="中国人";
19
                fos.write(s.getBytes());
20
                fos.write(s1.getBytes());
21
            } catch (Exception e) {
22
                e.printStackTrace();
23
            }finally{
24
                try {
25
                    fos.close();
26
                } catch (Exception e2) {
27
                    e2.printStackTrace();
28
                }
29
            }
30
        }
31
    }
32
}
33
 
          

3、案例[Io04.java]:图片拷贝


/** * File类的基本用法 * io流--文件字节流 * 图片拷贝--FileInputStream类与 FileOutputStream类 */ import java.io.*; public class Io04 { public static void main(String[] args) { //先将图片读入到内存,再将内存中的图片写入到某个文件 //因为二进制文件只能拿使用字节流来处理 //输入流 FileInputStream fis=null; //输出流 FileOutputStream fos=null; try { fis=new FileInputStream("e:\\ff\\a.jpg"); fos=new FileOutputStream("e:\\a.jpg"); byte buf[]=new byte[1024]; int n=0;//记录实际读取到的字节数 //循环读取图片 while((n=fis.read(buf))!=-1){ //输出到指定文件 fos.write(buf); } } catch (Exception e) { e.printStackTrace(); }finally{ //一定要关闭打开的文件流 try { fis.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } } }
1
40
 
1
/**
2
 * File类的基本用法
3
 * io流--文件字节流
4
 * 图片拷贝--FileInputStream类与 FileOutputStream类
5
 */
6
import java.io.*;
7
public class Io04 {
8
    public static void main(String[] args) {
9
        //先将图片读入到内存,再将内存中的图片写入到某个文件
10
        //因为二进制文件只能拿使用字节流来处理
11
        //输入流
12
        FileInputStream fis=null;
13
        //输出流
14
        FileOutputStream fos=null;
15
        
16
        try {
17
            fis=new FileInputStream("e:\\ff\\a.jpg");
18
            fos=new FileOutputStream("e:\\a.jpg");
19
            byte buf[]=new byte[1024];
20
            int n=0;//记录实际读取到的字节数
21
            //循环读取图片
22
            while((n=fis.read(buf))!=-1){
23
                //输出到指定文件
24
                fos.write(buf);
25
            }
26
            
27
        } catch (Exception e) {
28
            e.printStackTrace();
29
        }finally{
30
            //一定要关闭打开的文件流
31
            try {
32
                fis.close();
33
                fos.close();
34
            } catch (Exception e) {
35
                e.printStackTrace();
36
            }
37
        }
38
    }
39
}
40
 
          


















转载于:https://www.cnblogs.com/xuxaut-558/p/10045776.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值