IO流的String和Byte的相互转化

£可爱£侵袭症+ 提交于 2019-11-28 15:56:16

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

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

 // String2Byte byte[] c = str.getBytes();
//Byte2StringString 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("==");
           }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!