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>
  • 相关阅读:
    能大大提升工作效率和时间效率的9个重要习惯
    hibernate的校验
    8. semahpore原理
    chklist
    android textview 自动换行 整齐排版
    fiddler接口测试,js代码修改日志展示(埋点用)
    Python模块之 tqdm进度条
    Python抓取网页到本地
    Python模块之 clint 进度条
    python windows下pip安装错误whl文件安装错误
  • 原文地址:https://www.cnblogs.com/tianhengblogs/p/5328302.html
Copyright © 2011-2022 走看看