MyEclipse------随机流(能读也能写数据)

RandomAccessFile流指向文件时,不刷新文件。

其中seek(long a)用来定位RandomAccessFile流的读写位置,其中参数a确定读写位置距离文件开头的字节个数.

 

other.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@page import="java.io.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> 
<title>My JSP 'other.jsp' starting page</title>
</head>

<body>
    <%
        //dat为数据流格式,改为txt会出现乱码,这里如果直接用txt也会出现乱码
        File file=new File("C:\\Users\\X550V\\Desktop","cc.txt");
        if(!file.exists()){
            //这里的代码用于解决输入流乱码时,不用时可以注释掉
            file.createNewFile();
            out.print("文件创建成功");
            byte d[]="新年快乐".getBytes();//将字符串转换为字节,使用机器默认的编码方式,也可以在括号里修改
            OutputStream ou=new FileOutputStream(file);
            ou.write(d);
            ou.close();
        }
        RandomAccessFile inAndOut=null;
        int data[]={1,2,3,4,5,6,7,8,9,10};
        char c[]={'你','好','世','界'};
        String str[]={"","","","我喜欢青檀","0000","zzzz"};
        try{
            inAndOut=new RandomAccessFile(file,"rw");
            
            //int类型
            /* for(int i=0;i<data.length;i++){
                inAndOut.writeInt(data[i]);
            } 
            for(long i=data.length-1;i>=0;i--){
                //一个Int类型的数据占四个字节
                inAndOut.seek(i*4);
                out.print(inAndOut.readInt());
            }     */
            
            //char类型,可输入中文
            /* for(int i=0;i<c.length;i++){
                inAndOut.writeChar(c[i]);
            }
            for(long i=0;i<c.length;i++){
                //一个char占两个字节
                inAndOut.seek(i*2);
                out.print(inAndOut.readChar());
            } */    
            
            //String类型,可输入中文
            /* for(int i=0;i<str.length;i++){
                inAndOut.writeChars(str[i]);
            }
            for(long i=0;i<6;i++){
                //一个String的汉字,数字,字母都是两个字节,所以i*2
                inAndOut.seek(i*2);
                //str.length=6,所以只能输出到第六个字:欢
                out.print(inAndOut.readChar());
                out.print(inAndOut.getFilePointer()+"<br>");//获取流的当前读写位置
            }
            out.print("<br>"+str.length); */
            
            //解决输入流乱码问题
            long length=inAndOut.length();
            long position=0;
            inAndOut.seek(position);//将读取位置定位到文件的起始
            while(position<length){
                String info=inAndOut.readLine();
                byte b[]=info.getBytes("iso-8859-1");
                //如果机器的默认编码是gb2312,则info=new String(b);这样写也行
                info=new String(b,"gb2312");
                position=inAndOut.getFilePointer();
                out.print(info);
            }
        }
        catch(IOException e){
            out.print(e);
        }
     %>
</body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值