字符串数组转化成字符串 String(char[] chars)/String(char[] chars, int offset, int length)类似于字节数组转化成字符串。
String (byte[] bytes);每次读取数组长度个字符
byte[] bytes = new byte[1024];
String str = new String(bytes);
System.out.print(str);
String(byte[] bytes, int offset, int length);每次读取数组中实际字符个数
byte[] bytes = new byte[1024];
String str = new String(bytes,0,len);
System.out.print(str);
public class BytesToString {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("a.txt");
fos = new FileOutputStream("b.txt");
byte[] bytes = new byte[4];
int len = 0;
while ((len = fis.read(bytes)) != -1){
// String str = new String(bytes);
String str = new String(bytes,0,len);
System.out.println(str);
// fos.write(bytes,0,len);
fos.write(str.getBytes());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}