读取文件和写入文件

今天老大让写一个文件,四五百条数据,一条条整实在费劲,最后从网上千辛万苦找来两个demo组合一下成了

代码没解决异常,只是用来简单生成文件使用

拷贝的话记得解决异常,还有很多细节需要改动,但是平常拿来解决问题是够了

public class Demo1 {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\feiq\\Recv Files\\职业.txt");
        BufferedReader reader = null;

        System.out.println("以行为单位读取文件内容,一次读一整行:");
        reader = new BufferedReader(new FileReader(file));
        String tempString = null;
        int line = 1;
        // 一次读入一行,直到读入null为文件结束    
        while ((tempString = reader.readLine()) != null) {
            // 显示行号
            System.out.println("line " + line + ": " + tempString);
            String[] str = tempString.split("    ");
            File file2 = new File("E:" + File.separator + "demo.sql");
            OutputStream out = null; // 声明字节输出流
            out = new FileOutputStream(file2, true); // 通过子类实例化
            String s = str[0]+":"+"'"+str[1]+"'"+","; // 要输出的信息
            byte b[] = s.getBytes(); // 将String变为byte数组
            out.write(b); // 写入数据
            out.close(); // 关闭
        }
        reader.close();

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值