package Number;
//import java.util.*;
import java.io.File;//如果不导入这个类 则提示错误File cannot be resolved to a type
import java.io.FileOutputStream;
import java.io.FileInputStream;
public class Test{
public static void main(String[] args){
File file = new File("word.txt");
try{
FileOutputStream out = new FileOutputStream(file);
String temp = "lallalaalalala";
byte buy[] = temp.getBytes();//字节数组
System.out.println(temp.getBytes());
out.write(buy);//通过字节数组来写入
out.close();
}
catch(Exception e){
e.printStackTrace();
}
try{
FileInputStream in = new FileInputStream(file);
byte byt[] = new byte[1024];//初始化字节数组1024个0
int len = in.read(byt);//从文件中读取信息:长度
System.out.println(new String(byt,0,len) );//String(byt,0,len)输出byt字节数组中的类容,输出范围0-length
in.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
输出结果:
[B@3b896429
lallalaalalala