java IO 编程学习

java IO 编程是指通过java 程序来操作文件。比如文件的读写,删除,备份等。

需要掌握文件流的基本概念。 掌握IO编程方法,把握字节流和字符流
文件在程序中是以流的形式来操作的

什么是字节流:字节流是以字节为单位byte去读取的,字节流可以用于读取二进制文件以及任何类型的文件

什么是字符流:字符流是以字符为单位去读取的 字符流可以读取文本文件。不能操作二进制文件

输入: 字节流 InputStream 字符流 Reader
输出: 字节流 OutputStream 字符流 Wirter

IO编程–File 类
要做java IO 编程就需要导入java.io包
import java.io.*;

使用File类获取到文件对象。 File类只能获取到文件对象 而无法读取或者写入文件

package File_IO;
import java.io.*;
/**
 * 主要是学习java的文件IO操作, java IO 操作需要导入java.io包里面包含了所有的java io 类
 * Created by admin on 2017/8/27.
 */
public class JavaIO {
    public static void main(String[] args){
        // File 类的使用需要传入文件作为参数, 获取文件对象
        File file = new File("E:\\PythonCode\\spider\\China\\20170201152643\\iast0ic4wfw.jpg");
        // 获取到File对象之后,我们就能获取文件的一些属性
// file.canExecute() 返回布尔值 文件是否可以执行
// file.canRead() 返回布尔值 文件是否可读
// file.canWrite() 返回布尔值 文件是否可写
// file.delete() 删除该文件
// file.exists() 文件是否存在
// file.getPath() 获取文件路径
// file.isFile() 是否是文件
// file.isDirectory() 是否是目录
// file.isHidden() 是否是隐藏文件
// file.length() 文件大小 单位是字节
    }
}

使用java的File类创建文件和文件夹

File file = new File("C:\\java.txt");
if (!file.exists()){
// 如果不存在该文件则创建,使用createNewFile方法创建
// createNewFile 方法必须要捕获异常
try {
                ff.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
}

// 使用File 类创建文件夹
File directory = new File("C:\\IO");
        // 创建文件夹
        if (directory.isDirectory()){
            System.out.println("文件夹已经存在");

        }else {
     // 创建文件夹。如果是mkdirs()则是递归创建目录
            directory.mkdir();
        }

使用File类列出一个文件夹下面的所有文件(非递归)

 File all = new File("C:\\");
        // listFiles() 会返回文件对象的数组
        File filelist[] = all.listFiles();
        for (File files: filelist){
            System.out.println(files);
        }

使用FileInputStream读取文件内容。因为File类没有读写的功能。因此需要使用FileInputStream

     File binFile = new File("F:\\test.txt");
        FileInputStream inputStream=null;
        try {
            inputStream = new FileInputStream(binFile);
            // 定义一个字节数组,相当于缓存,一次读取这么多的数据,避免读取超大文件时内存被撑爆
            byte []bytes = new byte[1024];
            // 定义的end 代表如果文件实际大小没有我们定义的1024 则以实际大小为准
            int end=0;
            while ((end=inputStream.read(bytes))!=-1){
                String s = new String(bytes, 0, end);
                System.out.println(s);
                // System.out.write(bytes, 0, end);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 关闭文件
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

从键盘接收用户输入的数据,写入文件。 要写入文件需要用到FileOutputStream

从键盘接收y用户输入需要用到System.in 和 BufferedReader

File fileObj = new File("C:\\object.txt");
        FileOutputStream fileOutputStream=null;
        if (!fileObj.exists()){
            fileObj.createNewFile();
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String string = null;
        string = bufferedReader.readLine();
        try {
            // 字节输出流
            fileOutputStream = new FileOutputStream(fileObj);
            // byte []bytes = new byte[1024];
            fileOutputStream.write(string.getBytes());
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }finally {
            if (fileOutputStream !=null){
                fileOutputStream.close();
            }
        }

文件复制。 文件复制,是一个输入流, 一个输出流。都需要用到。需要先把文件读取到内存,然后写入指定的位置

// 复制文件
        FileInputStream fileInputStream=null;
        FileOutputStream fileOutputStream1=null;
        File cpFile = new File("D:\\sqlback\\all.sql");
        File pasteFile = new File("C:\\all.sql");
        try {
            if (cpFile.exists()){
                fileInputStream = new FileInputStream(cpFile);
                byte []readbyte = new byte[1024];
                int fileEnd=0;
                fileOutputStream1 = new FileOutputStream(pasteFile);
                while ((fileEnd=fileInputStream.read(readbyte))!=-1){
                    // 读取到文件数据之后 写入其他位置,完成复制
                    fileOutputStream1.write(readbyte);
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (fileInputStream !=null){
                fileInputStream.close();
            }
            if (fileOutputStream1 !=null){
                fileOutputStream1.close();
            }
        }

读取一个文本文件的内容,写入另一个文件。 读写文本文件需要使用字符流FileReader, FileWriter

 FileReader fileReader=null;
        FileWriter fileWriter=null;
        try {
            char cache[]= new char[1024];
            int readchar=0;
            fileReader = new FileReader("F:\\test.txt");
            fileWriter = new FileWriter("C:\\cp-test.txt");
            while ((readchar=fileReader.read(cache))!=-1){
                // System.out.println(String.valueOf(cache));
                fileWriter.write(cache);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (fileReader !=null){
                fileReader.close();
            }
            if (fileWriter !=null){
                fileWriter.close();
            }
        }

缓冲字符流。 有时候当读取一个文件比较大的时候。我们不可能全部读进内存,我们需要一次读取一点缓存到内存当中。因此就需要缓存字符流。 缓存字符流又分为BufferReader和BufferWriter。直接操作String

// BufferReader BufferWriter 的使用
        BufferedReader bufferedReader1=null;
        BufferedWriter bufferedWriter=null;
        try{
            // BufferReader BufferWriter
            FileReader fr = new FileReader("F:\\test.txt");
            bufferedReader1 = new BufferedReader(fr);
            FileWriter fw = new FileWriter("C:\\okc.txt");
            bufferedWriter = new BufferedWriter(fw);
            // 循环读取
            String s="";
            // BufferReader 读取到的内容不包含任何行的终止符,比如换行符
            // 因此当你读取一个文件写入写入另外一个文件时,你会发现写入的内容没有换行符,需要我们写文件时自己加上去
            while ((s=bufferedReader1.readLine())!=null){
                fw.write(s+"##$$");
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (bufferedReader1 !=null){
                bufferedReader1.close();
            }
            if (bufferedWriter != null){
                bufferedWriter.close();
            }
        }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值