• 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>
  • 相关阅读:
    Android 表格布局
    Python 字符串操作分类
    设置Safari禁止访问某个网站
    java判断路径是文件夹还是文件
    java上下分页窗口流动布局
    Python获取网页html代码
    一次失败的java Box居中尝试
    装饰器进阶和迭代器
    函数对象补充,包函数与装饰器
    函数对象和名称空间
  • 原文地址:https://www.cnblogs.com/tianhengblogs/p/5328302.html
Copyright © 2020-2023  润新知