byte byt[] = new byte[1024]; //1024是什么意思
byte数组的初始化,数组的长度为1024, 从你的代码看来表达的是每次从文件读取1024个字节。
8bit(位)是1byte(字节)
1024byte(字节)是1kb
byte[] bytes = new byte[1024]是1kb
new String(byt, 0, len); //这里的0是什么意思
这是将字节数组中角标为 0 到角标为 len-1 转化为字符串。
第一个byt参数就是你定义的数据名;
第二个0就是从数组里角标为0(也就是第一位)开始转换字符串;
第三个len就是你读取文件所读到的字节个数。
package com.web;
import java.io.*;
public class Test36 {
public static void main(String[] args) {
File file = new File("word.txt");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
byte[] bytes = "i am programmer and i like programing with java".getBytes();
fileOutputStream.write(bytes);
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream fileInputStream = new FileInputStream(file);
byte[] bytes2 = new byte[1024];
int len = fileInputStream.read(bytes2);
System.out.println("it is write: "+new String(bytes2,0,len));//it is write: i am programmer and i like programing with java
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}