zoukankan      html  css  js  c++  java
  • IO流的String和Byte的相互转化

    在Java中IO输入流通常读入的是String,但是在字节流中的传递的始终是用字节,Byte

    于是就会用到Byte和String的相互转化

     // String2Byte
    byte[] c = str.getBytes();
    //Byte2String
    String value = new String(byte1,0,ins);
    System.out.println(value);
    public class Demo2 {
        public static void main(String[] args) {
            File file = new File("D:\b.txt");
            File file1 = new File("D:\c.txt");
            try {
                FileInputStream inputStream = new FileInputStream("src\main\java\com\nowcoder\Gday09\a.txt");
                FileReader fileReader = new FileReader("src\main\java\com\nowcoder\Gday09\a.txt");
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                FileOutputStream fileOutputStream1= new FileOutputStream(file1,true);
                int ins ;
                String str = "    i love ja";
                // String2Byte
                byte[] c = str.getBytes();
               byte[] byte1 = new byte[1];
               byte[] byte5 = new byte[5];
               // 一次写一个字节数组
    //           for (int i =0;i<byte1.length;i++){
    //             fileOutputStream.write(c);
    //           }
    //           for (int i=0;i<byte5.length;i++){
    //               fileOutputStream1.write(c);
    //           }
               while ((ins=inputStream.read())!=-1 ){
                   //Byte2String
                   String value = new String(byte1,0,ins);
                   System.out.println(value);
    //               System.out.println((char)ins);
    //               System.out.println("==");
               }
  • 相关阅读:
    20201215王馨瑶 实验一《Python程序设计》实验报告
    20201215第十六周学习总结
    python笔记
    信导笔记
    成绩调节
    2020-2021-1 20201226 《信息安全专业导论》第十三周学习总结
    链表(补交)
    2020-2021-1 20201226 《信息安全专业导论》第十二周学习总结
    Wireshark 实践
    ssh
  • 原文地址:https://www.cnblogs.com/GooKiki/p/11414232.html
Copyright © 2011-2022 走看看