• String与InputStream相互转换


    谁能给我举例说明下linux中管道符的用法?
    只要理解了什么是管道,就很简单了
    管道“| ”就是将前面命令输出作为管道后面命令的输入
    如:
    ls -a | grep test | awk "{print $1}"
    就是将ls -a 显示的结果,在帅选出含有test,然后打印出第一列。
     
    追问  awk 是什么意思?
     
    回答  awk 也是一个过滤的工具,可用于格式化报文或从一个大的文本中抽取数据包。
    具体使用方法,man awk  
    网上的资料也很多,随便找一下就可以了。
     
    提问者评价  非常感谢!
     
    JAVA技巧(java中获取当前类所在的目录)
    建立了一个包yyyb,其下有一个YyybDemo.class的类
     
      System.out.println(System.getProperty("user.dir"));
      System.out.println(this.getClass().getResource("").getPath());
      this.setIconImage(new ImageIcon(this.getClass().getResource("1.gif")).getImage());
     
      实例结果:
      上面的两个输出分别为:
      D:\liyuanxun\java\develop\develop
      /D:/liyuanxun/java/develop/develop/yyyb/
     
    ****************************************************************************************
     
    1.String to InputStream
    String str = "String与InputStream相互转换";
    InputStream   in_nocode   =   new   ByteArrayInputStream(str.getBytes());   
    InputStream   in_withcode   =   new   ByteArrayInputStream(str.getBytes("UTF-8"));   
     
     
    2.InputStream to String 
        这里提供几个方法。
    方法1:
      public String convertStreamToString(InputStream is) {   
       BufferedReader reader = new BufferedReader(new InputStreamReader(is));   
            StringBuilder sb = new StringBuilder();   
        
            String line = null;   
            try {   
                while ((line = reader.readLine()) != null) {   
                    sb.append(line + "/n");   
                }   
            } catch (IOException e) {   
                e.printStackTrace();   
            } finally {   
                try {   
                    is.close();   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
            }   
        
            return sb.toString();   
        }   
     
    方法2:
    public   String   inputStream2String   (InputStream   in)   throws   IOException   { 
            StringBuffer   out   =   new   StringBuffer(); 
            byte[]   b   =   new   byte[4096]; 
            for   (int   n;   (n   =   in.read(b))   !=   -1;)   { 
                    out.append(new   String(b,   0,   n)); 
            } 
            return   out.toString(); 
    方法3:
    public   static   String   inputStream2String(InputStream   is)   throws   IOException{ 
            ByteArrayOutputStream   baos   =   new   ByteArrayOutputStream(); 
            int   i=-1; 
            while((i=is.read())!=-1){ 
            baos.write(i); 
            } 
           return   baos.toString(); 
  • 相关阅读:
    .net 之邮件发送帮助类 MailKit 的使用
    .NET6 开源之JSON 2 SQL (JORM框架)
    批量上传依赖到离线nexus仓库(maven/npm)
    利用gitblit创建本地Git仓库
    [记录]Git 服务器怎么避免反复密码输入
    配置文件
    spring_IoC底层原理
    Spring_AOP
    事务
    java程序日志
  • 原文地址:https://www.cnblogs.com/huapox/p/3516343.html
Copyright © 2020-2023  润新知