zoukankan      html  css  js  c++  java
  • IO流(随机流,数组内存流

    File file1=new File("test1.txt");
    RandomAccessFile in2=new RandomAccessFile(file1,"rw");

    后面参数是"rw"他可读可写,用seek()来设定位置

    不过在用readLine()时,非ASCII码(比如中文)会出现乱码现象

    不过有办法

    用"iso-8859-1"重新编码,然后重新化为String 

    String s1=in2.readLine();
    byte b1[]=s1.getBytes("iso-8859-1");
    String s3=new String(b1,"GB2312");//GB2312要看机器的默认编码

    数组内存流

    这类流的目的地和源都是内存

    分别有字节数组和字符数组

                File file1=new File("test1.txt");
                ByteArrayOutputStream out1=new ByteArrayOutputStream();//new字节输出流
                byte[] b1="hello world".getBytes();//字节数组
                out1.write(b1);//输出到内存
                ByteArrayInputStream in1=new ByteArrayInputStream(out1.toByteArray());//public byte[] toByteArray()可以返回输出流写入到内存缓冲区的全部字节
                byte[] b2=new byte[out1.toByteArray().length];//new字节输入流
                in1.read(b2);//读入
                System.out.println(new String(b2));
                CharArrayWriter out2=new CharArrayWriter();//new字符输出流
                char[] b3="你好啊".toCharArray();//化为字符数组
                out2.write(b3);//写入内存
                CharArrayReader in2=new CharArrayReader(out2.toCharArray());//new字符输入流
                char[] b4=new char[out2.toCharArray().length];
                System.out.println(b3);
  • 相关阅读:
    8 pandas模块,多层索引
    7 numpy 傅里叶,提取图片轮廓
    6 DataFrame处理丢失数据--数据清洗
    5 pandas模块,DataFrame类
    4 pandas模块,Series类
    3 numpy模块
    2 线性代数基础
    1 Ipython、Jupyter 入门
    jdk安装与环境变量配置(一劳永逸)
    对jsp可见域的变量感悟
  • 原文地址:https://www.cnblogs.com/vhyc/p/6022432.html
Copyright © 2011-2022 走看看