JAVA 字符串 的转换

1、输入输出流

基于字节流的stream

DataOutputStream----DataInputStream

FileOutputStream-----FileInputStream:
基于字符流的stream(典型的以write和reader来标识的):
FileWriter---FileReader:
StringWriter---StringReader:

1.2 InputStream 和OutputStream的使用

InputStream 和OutputStream,是字节流操作的两个顶级抽象类,一般情况下都是通过如FileOutputStream和FileInputStream,及其他实现类来进行操作

InputStream:
(1)从流中读取数据
   int read() 读取一个字节,返回值为所读得字节
   int read(byte b[]) 读取多个字节,放置到字节数组b中,通常读取的字节数量为b的长度,返回值为实际独取的字节的数量。
   int read(byte b[] ,int off,int len)读取len个字节,放置到以下标off开始字节数组b中,返回值为实际读取的字节的数量。
   int available() 返回值为流中尚未读取的字节的数量。
   long skip(long n);读指针跳过n个字节不读,返回值为实际跳过的字节数量
(2)关闭流
   close() 流操作完毕后必须关闭
(3)使用输入流中的标记
   void mark(int readlimit)纪录当前指针的所在位置.readlimit表示读指针读出的readlimit个字节后所标记的指针位置才实效。
   void reset() 把读指针重新指向用mark方法所记录的位置
   boolean markSupported() 当前的流是否支持读指针的记录功能。

OutputStream:
(1)输出数据
   void write(int b)往流中写一个字节b
   void write(byte b[])往流中写一个字节数组b
   void write(byte b[],int off,int len)把字节数组b中从下标off开始,长度为len的字节写入流中
 (2)
   flush()刷空输出流,并输出所有被缓存的字节
           由于某些流支持缓存功能,该方法将把缓存中所有内容强制输出到流中。
(3)关闭流
   close()流操作完毕后必须关闭。

/**
     * 流寫入文件
     * @param in
     * @param file
     * @return
      * @throws IOException
     */
    public static File copyFile(InputStream in ,File file ) throws IOException{
       int reader = 0;
       byte [] read = new byte [4096];
       OutputStream out = new FileOutputStream( file );
       while (( reader = in .read( read , 0, 1024)) != -1){
            out .write( read , 0, reader );
       }
       in .close();
       out .close();
       return file ;
    }

1.3 Reader和Writer是所有字符流类的的抽象基类,用于简化对字符串的输入输出编程,即用于读写文本数据:

      /**
      * 字符串写入文件 --- 字符流
      * @param str
      * @param filepath
      * @throws IOException
      */
      public static void writer(String str ,String filepath ) throws IOException{
           File file = getFile( filepath );
           FileWriter writer = new FileWriter( file );
            writer .write( str );
            writer .flush();
            writer .close();
     }
     
      /**
      * 字符串读取 --- 字符流
      * @param filepath
      * @return
      * @throws IOException
      */
      public static String reader(String filepath ) throws IOException{
           File file = getFile( filepath );
            FileReader reader = new FileReader ( file );
           StringBuffer buffer = new StringBuffer();
            char [] chars = new char [100];
            int i = 0;
            while (( i = reader .read( chars , 0, 100)) != -1){
                 buffer .append( chars ,0, i );
           }
            return buffer .toString();
     }

1.4 字节流与字符流的区别

1、字节流在操作的时候本身是不会用到缓冲区(内存)的,是与文件本身直接操作的,而字符流在操作的时候是使用到缓冲区的。

2、字节流在操作文件时,即使不关闭资源(close方法),文件也能输出,但是如果字符流不使用close方法的话,则不会输出任何内容,说明字符流用。

3、的是缓冲区,并且可以使用flush方法强制进行刷新缓冲区,这时才能在不close的情况下输出内容。


2、字符串与流之间的简单转换:

这里推荐使用字节流,因为可以设置编码方式,如果要快速的进行读写,可以使用字符流。

/**
      * 字符串写入文件 --- 字节流
      * @param str
      * @param filepath
      * @throws IOException
      */
      public static void StrToFile(String str ,String charset ,String filepath ) throws IOException{
           
           File file = StrToStream. getFile( filepath );
            //Set names=Charset.availableCharsets().keySet(); 获取所有的编码方式名称
            byte [] bs = str .getBytes( charset );  //注意编码方式,解析时编码方式要对应,否则会有乱码
            //byte[] bs = str.getBytes();  //这里默认使用GBK编码
           OutputStream out = new FileOutputStream( file );
            out .write( bs );
            out .flush();
            out .close();
           System. out .println( "done" );
     }
     
      /**
      * 字符串读取 --- 字节流
      * @param filepath
      * @param charset
      * @return
      * @throws IOException
      */
      public static String StreamToStr(String filepath ,String charset ) throws IOException{
           File file = StrToStream. getFile( filepath );
           InputStream strem = new FileInputStream( file );
           StringBuffer buffer = new StringBuffer();
            byte [] bs = new byte [2048];
            int i = 0;
            while (( i = strem .read( bs , 0, 2048)) != -1){
                 buffer .append( new String( bs , charset )); //使用对应编码方式转换
           }
            return buffer .toString();
     }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值