IO流基础练习

package my.yimu.io;


import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/*
 * 写入文件:
 * 1、找到指定的文件
 * 2、根据文件创建文件的输出流
 * 3、把内容转换成字节数组
 * 4、向文件写入内容
 * 5、关闭输入流
 */
public class FileWriter {

    public static void main(String[] args) {
        // 构建指定文件
        File file = new File("D:\\ideaWorkSpace\\ideaworkspace\\blog\\Test\\src\\main\\java\\my\\yimu\\io\\a.txt");
        OutputStream out = null;
        try {
            // 目标文件不存在就创建
            if (!(file.exists())) {
                file.createNewFile();
            }
            // 根据文件创建文件的输出流
            out = new FileOutputStream(file);
            String message = "";
            for (int i = 0; i < 10; i++) {
                message += "PO_" + i + "\t" + i + "\n";
            }
            // 把内容转换成字节数组
            byte[] data = message.getBytes();
            // 向文件写入内容
            out.write(data);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭输出流
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
package my.yimu.io;


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/*
 * 读取文件:
 * 1、找到指定的文件
 * 2、根据文件创建文件的输入流
 * 3、创建字节数组
 * 4、读取内容,放到字节数组里面
 * 5、关闭输入流
 */
public class FileRead {

    public static void main(String[] args) {
        // 构建指定文件
        File file = new File("D:\\ideaWorkSpace\\ideaworkspace\\blog\\Test\\src\\main\\java\\my\\yimu\\io\\a.txt");
        InputStream in = null;
        try {
            // 根据文件创建文件的输入流
            in = new FileInputStream(file);
            // 创建字节数组
            byte[] data = new byte[100];
            // 读取内容,放到字节数组里面
            in.read(data);
            String s = new String(data);
            String sql = "";
            for (String s1 : s.split("\n")) {
                String[] split = s1.split("\t");
                if (split.length > 1) {
                    sql = "insert int Test values('" + split[0] + "','" + split[1] + "');";
                }
                System.out.println(sql);
            }

            //   System.out.println(s);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭输入流
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

package my.yimu.io;


import java.io.*;
/*
 * 实现思路:
 * 1、构建源文件与目标文件
 * 2、源文件创建输入流,目标文件创建输出流
 * 3、创建字节数组
 * 4、使用循环,源文件读取一部分内容,目标文件写入一部分内容,直到写完所有内容
 * 5、关闭源文件输入流,目标文件输出流
 */
public class FileCopy {

    public static void main(String[] args) {
        // 构建源文件
        File file = new File("D:\\ideaWorkSpace\\ideaworkspace\\blog\\Test\\src\\main\\java\\my\\yimu\\io\\a.txt");
        // 构建目标文件
        File fileCopy = new File("D:\\ideaWorkSpace\\ideaworkspace\\blog\\Test\\src\\main\\java\\my\\yimu\\io\\b.txt");
        InputStream in = null;
        OutputStream out = null;
        try {
            // 目标文件不存在就创建
            if (!(fileCopy.exists())) {
                fileCopy.createNewFile();
            }
            // 源文件创建输入流
            in = new FileInputStream(file);
            // 目标文件创建输出流
            out = new FileOutputStream(fileCopy, true);
            // 创建字节数组
            byte[] temp = new byte[1024];
            int length = 0;
            // 源文件读取一部分内容
            while ((length = in.read(temp)) != -1) {
                // 目标文件写入一部分内容
                out.write(temp, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭文件输入输出流
                in.close();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值