RandomAccessFile文件操作类

//io流:初始化
fileWriterSrv = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempDir), "UTF-8"));

//读取文件初始化
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(tempDir),"GBK"), 30 * 1024 * 1024);
//nio:RandomAccessFile示例
//例一:
public class InsertContent {
    private String filePath; //要操作的文件的路径
    private String content;    //要插入的内容
    private long position; //要插入的位置

    //构造方法
    public InsertContent(String path, String con, long pos) {
        filePath = path;
        content = con;
        position = pos;
    }

    //设置要操作的文件的路径
    public void setFilePath(String path) {
        filePath = path;
    }

    //设置要插入文件的内容
    public void setContent(String con) {
        content = con;
    }

    //设置要插入的位置
    public void setPosition(long pos) {
        position = pos;
    }

    //插入内容的具体实现方法
    public void insertCon(){

        RandomAccessFile raf = null;

        try {

            raf = new RandomAccessFile(filePath, "rw"); //将随机存取文件流连接到文件,访问方式设置为可读可写
            raf.seek(position); //指定插入的位置




            //***************先将插入点后面的内容保存起来****************
            StringBuffer sb = new StringBuffer();
            int len;

            sb.append("123");
            sb.append("456");


            byte[] b = new byte[100];
            while( (len=raf.read(b)) != -1 ) {
                sb.append( new String(b, 0, len) );
            }


            //.................................................

            raf.seek(position); //重新设置插入位置
            raf.write( content.getBytes("UTF-8") ); //插入指定内容
            raf.write( sb.toString().getBytes("UTF-8") ); //恢复插入点后面的内容

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            //关闭随机存取文件流
            try {
                raf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //测试方法
    public static void main(String[] args) {

        InsertContent ic = new InsertContent("D:\\rpt\\number\\00010000\\file1.txt", "mio", 0);
        ic.insertCon();
    }
}

//例二:
  public boolean creatTxt(){
        boolean falg = false;
        try {
            SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) ApplicationContextHelper.getBean("testSqlSessionFactory");
            SqlSession sqlSession = sqlSessionFactory.openSession();
            Cursor<User> cursor = sqlSession.selectCursor("com.example.study.dao.UserMapper.selectAll");
            Iterator<User> iterator = cursor.iterator();
            RandomAccessFile raf = null;
            String four = null;
            StringBuffer sb = null;
            String stauts = null;
            while (iterator.hasNext()){
                User user = iterator.next();
                if (!user.getMobilefour().equals(four)){
                    if (null != raf){
                        raf.seek(0);
                        raf.write("定位到第一行\r\n".getBytes("UTF-8"));
                        raf.write(sb.toString().getBytes("UTF-8"));
                        raf.close();
                    }
                    String fileName = rptDir + "/" + user.getMobilefour()+".txt";
                    File file = new File(fileName);
                    if (file.exists())
                        file.delete();
                    if (!file.exists())
                        file.createNewFile();
                    logger.info("文件生成路径:" + fileName);
                    raf = new RandomAccessFile(fileName, "rw");
                    four = user.getMobilefour();
                    //写入文件
                    sb = new StringBuffer();
                    String title = String.format("%s\r\n",user.getMobilefour().trim());
                    sb.append(title);
                }
                stauts = "1".equals(user.getStauts()) ? "在线" : "消失";
                String body = String.format("%s|%s|%s|%s\r\n",
                        user.getName(),
                        user.getEmail(),
                        user.getSex(),
                        stauts);
                sb.append(body);
            }
            raf.seek(0);
            raf.write("定位到第一行\r\n".getBytes("UTF-8"));
            raf.write(sb.toString().getBytes("UTF-8"));
            raf.close();
            falg = true;
        }catch (Exception e){
            logger.info("失败",e);
            falg =false;
        }
        return falg;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值