zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    【Vegas原创】mysql更改用户密码之无敌方法
    【Vegas原创】Xcopy屡试不爽
    【Vegas原创】ctrl shift无法切换输入法的解决方法
    【Vegas原创】将SQLServer表、视图、存储过程的所有者批量改为dbo的处理方法
    【Vegas原创】SQL Server2005应急备机切换步骤 生产机正常
    【Vegas原创】SQLServer 2000 企业管理器展开数据库列表错误的解决方法
    【Vegas原创】win7下打开telnet服务
    【Vegas原创】Windows 2003下CACTI的安装及配置
    【Vegas原创】SecureCRT个性化设置
    Mathematica实现微分算子功能
  • 原文地址:https://www.cnblogs.com/tianhengblogs/p/5328302.html
Copyright © 2011-2022 走看看