字符流
字符输入流
Reader类常用方法
int read( )
int read(char[] c)
read(char[] c,int off,int len)
void close( )
FileReader(File file)
FileReader(String name)
//一个字符一个字符的读
FileReader fr=new FileReader("a.txt");
int tmp;
StringBuffer sb=new StringBuffer();
while ((tmp = fr.read()) != -1) {
sb.append((char)tmp);//读出来的是字符的整数形式 转型
}
System.out.println(sb.toString());
FileReader fr=new FileReader("a.txt");
char[] c=new char[(int)(new File("a.txt").length())];
//获取文件的长度 long类型强制转换int
//一次性全部读入
fr.read(c);
System.out.println(c);//重写了String方法
//数组长度不等于文件的长度
FileReader fr=new FileReader("a.txt");
char[] c=new char[5];
int tem;
while ((tem = fr.read(c)) != -1) {
System.out.print(new String(c,0,tem));
}
字符输出流
Writer类常用方法
write(String str)
write(String str,int off,int len)
void close()
void flush()
FileWriter (File file)
FileWriter (String name)
FileReader fr=new FileReader("a.txt");
char[] c=new char[(int)(new File("a.txt").length())];
fr.read(c);
System.out.println(c);
FileWriter fw=new FileWriter("b.txt");
fw.write(c);
fw.close();
fr.close();
缓冲流
使用FileReader类与BufferedReader类 提高字符流读取文本文件的效率
使用FileWriter类与BufferedWriter类 提高字符流写文本文件的效率
public static String readBuffer(String path)throws IOException {
FileReader fr=new FileReader(path);
BufferedReader br=new BufferedReader(fr);//参数是Reader类型
String tmp;
StringBuilder sb=new StringBuilder();
while ((tmp = br.readLine()) != null) {//一行一行的读
sb.append(tmp+"\n");
}
br.close();//后开先关
fr.close();//先开后关
return sb.toString();
}
public static void writeBuffer(String path,String s,boolean isAppend)throws IOException{
FileWriter fw=new FileWriter(path,isAppend);
BufferedWriter bw=new BufferedWriter(fw);
bw.write(s);//可以直接字符串做参数
bw.close();
fw.close();
}
public static void main(String[] args)throws IOException {
String s=readBuffer("a.txt");
System.out.println(s);
writeBuffer("b.txt",s,true);
}
转换流
public static String readCharset(String path)throws IOException{
FileInputStream fis=new FileInputStream(path);
//设置编码格式
InputStreamReader isr=new InputStreamReader(fis,"GBK");
BufferedReader br=new BufferedReader(isr);
String tmp;
StringBuilder sb=new StringBuilder();
while ((tmp = br.readLine()) != null) {//一行一行地读
sb.append(tmp+"\n");
}
br.close();
isr.close();
fis.close();
return sb.toString();
}
public static void writeCharset(String path,String s,boolean isAppend,String charsetName)throws IOException{
FileOutputStream fos=new FileOutputStream(path,isAppend);
OutputStreamWriter osw=new OutputStreamWriter(fos,charsetName);
// BufferedWriter bw=new BufferedWriter(osw);
// bw.write(s);
// bw.close();
osw.write(s);//可以直接用OutputStreamWriter
osw.close();
fos.close();
}
public static void main(String[] args)throws IOException {
String s=readCharset("a.txt");//已经转换过了
System.out.println(s);
writeCharset("b.txt",s,true,"UTF-8");
}
读写二进制文件
DataInputStream类
FileInputStream的子类
与FileInputStream类结合使用读取二进制文件
DataOutputStream类
FileOutputStream的子类
与FileOutputStream类结合使用写二进制文件
FileInputStream fis=new FileInputStream(inPath);
DataInputStream dis=new DataInputStream(fis);
byte[] b=new byte[dis.available()];
dis.read(b);
// System.out.println(new String(b));
FileOutputStream fos=new FileOutputStream(outPath);
DataOutputStream dos=new DataOutputStream(fos);
dos.write(b);
dos.close();
fos.close();
dis.close();
fis.close();
}
public static void main(String[] args) throws IOException {
copy("D:/IOTest/picture.jpg","picture");
}
Java序列化和反序列化
序列化是将对象的状态写入到特定的流中的过程
反序列化则是从特定的流中获取数据重新构建对象的过程
public class Student implements Serializable {//实体类实现序列化接口
private int id;
private String name;
private double score;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", score=" + score +
'}';
}
public Student(int id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
public static void writeObject(String path, boolean isAppend, Object obj)
throws IOException {
//先将对象写到文件中
FileOutputStream fos=new FileOutputStream(path,false);
//创建对象输出流
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(obj);//装对象
oos.close();
fos.close();
}
public static Object readObject(String path) throws IOException, ClassNotFoundException {
//读出来
FileInputStream fis=new FileInputStream(path);
//创建对象输入流
ObjectInputStream ois=new ObjectInputStream(fis);
Object o=ois.readObject();
ois.close();
fis.close();
return o;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
Student s=new Student(1,"zz",100);
writeObject("obj.txt",true,s);
Object o=readObject("obj.txt");
if(o instanceof Student){
Student stu=(Student)o;
System.out.println(stu);
}
}
总结:
基本流
1、字节流 FileInputStream FileOutputStream
2、字符流
1)FileReader FileWriter
2)缓冲流 BufferedReader BufferedWriter 参数是Reader Writer
3)转换流 InputStreamReader OutputStreamWriter
二进制流
序列化和反序列化
a 65 01000001一个字节 字节–65
字节流
字节流- 转换流 设置编码
utf-8 国际标准 使用三个字节存储一个中文
GBK 中国 使用两个字节存储一个中文
往里面工程下的文件里写 要注意与工程的编码格式一致