1.字节流
进行读取出文本的内容
try {
FileInputStream fis=new FileInputStream(fileName);
byte[] bytes;
bytes = new byte[fis.available()];
fis.read(bytes);
String content=new String(bytes);// 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新 的String。
fis.close();
return content;
} catch (IOException e) {
e.printStackTrace();
}
注意事项:
1.只要涉及到流的操作,在读写操作之后一定要将流进行关闭;
2.文件读写会在编译阶段检查异常
实现方法:readStrFormFile 完成读取文件中的内容并返回
try{
FileOutputStream fos=new FileOutputStream(fileName);
fos.write(content.getBytes()); //使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
fos.close();
}catch(IOException e){
e.printStackTrace();
}
多次运行写入文件的内容始终是最后的一次输入,之前的写入都被覆盖掉了。
构造函数构造出来的FileOutputStream(fileName) 是不支持追加内容的
在输出流被构造到被关闭这一次操作内的写入时可以多次write
但是再次打开流时,文件会重头写,所以会覆盖掉以前的内容
FileOutputStream fos = new FileOutputStream(fileName);
构造追加内容的输出流:
FileOutputStream fos = new FileOutputStream(fileName,true);
更改后的代码是能够追加内容到文件中
PS:如果在文本中想要换行windows 下\r\n linux下\n 制表\t
通过上面的练习可以发现不管是写还是读内容都是通过字节来操作的
fos.write(content.getBytes());按照字节写
fis.read(bytes);按照字节读
那么问题来了,如果文件中有一大段文本,我读取其中的某个字符呢
“no pain no gain 没有付出就没有收获”
只读取“pain” 或者“没有付出”??
字符串在存储数字和英文的时候,一字节的存储空间
读取n 从0开始读取1字节,读取no pain 需要从0 开始读取7字节
fis.read(bytes,off,len)// byte 要存放读取的字节数组,off起点,len读取长度
PS :注意bytes定义的长度一定要大于 len 否则不够存储的
fis.skip(len) 跳过多少字节
在 main函数的开始处输出编码,查看代码如下(提交时注释掉该代码):
String ec= System.getProperty(“file.encoding”);
System.out.println(ec);
序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。
JAVA中对于对象的状态序列化和反序列化,提供了一对操作API
ObjectInputStream 和ObjectOutputStream 这两个可以将可序列化的对象的状态序列化到文件中保存或者传输。
可序列化的对象,这个类必须是实现了Serializable接口的类,而且其成员变量也必须是可序列化的。
序列化:
父类如果是可序列化的,子类也是可序列化的
类的成员变量也应该是可序列化的,类才能被正常可序列化的
将students序列化到文件中
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
out.writeObject(students);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
将文件中的数据反序列化到List中
List students = null;
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
students = (List) in.readObject();
in.close();
} catch (IOException e) {
e.printStackTrace();
students = Collections.emptyList();
} catch (ClassNotFoundException e) {
e.printStackTrace();
students = Collections.emptyList();
}
2.字符流
try{
InputStreamReader fr=new FileReader(fileName);
char[] c=new char[10];
fr.read(c);
String s=new String(c);
return s;
}catch (IOException e) {
e.printStackTrace();
}
return “”;
}
try{
FileWriter fw=new FileWriter(fileName);
String[] content1={“Lily,女,14”,”Lucy,女,15”,”Lilei,男,16”};
fw.write(content1[0],0,content1[0].length());
fw.write(“\r\n”);
fw.write(content1[1],0,content1[1].length());
fw.write(“\r\n”);
fw.write(content1[2],0,content1[2].length());
fw.close();
}catch (IOException e) {
e.printStackTrace();
}
按照字符还不够过瘾,因为最终还是要一个字符一个字符的读取或写入,敢不敢按照一行读取啊?不少同学这样问道。
缓存式的字符输入输出流BufferedReader BufferedWriter里面有方法是按照一行一行的进行写和读的的方法
try{
BufferedReader br=new BufferedReader(new FileReader(fileName));
String info;
while((info=br.readLine())!=null){
String[] s1=info.split(“,”);
StudentInfo s=new StudentInfo(s1[0],s1[1],s1[2]);
list.add(s);
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
String infos=student.getName()+”,”+student.getStuNo()+”,”+student.getClaName();
Writer writer=new FileWriter(fileName,true);
BufferedWriter bw=new BufferedWriter(writer);
bw.write(infos);
bw.newLine();
bw.close();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}