1.文件中的数据读入程序
File f = new File("data/DATA.txt");//文件的地址
try {
FileInputStream fis = new FileInputStream(f);
/*定义一个字节数组用于缓存*/
byte []bytes = new byte[1024];
int n =0;
while((n=fis.read(bytes))!=-1){
String s= new String(bytes,0,n);
System.out.println(s);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

 

2.程序中为数据写入到文件

File f = new File("data/DATA.txt");//文件的地址

FileOutputStream fos=null;
try {
fos = new FileOutputStream(f);
String s="你好";
fos.write(s.getBytes());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();//关闭文件输出流
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}