在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("=="); }