FileInputStream fis = new FileInputStream(File);
FileInputStream fis = new FileInputStream(puth);
int read():读取一字节的数据并返回
int read(byte[] b );读取一定量的数据到给定的字节数组中,并且返回读到的长度
int read(byte[],int off,int len) 读取一定量的数据到给定的字节数组中,并且指定从数组的off位置处写入指定len字节的数据
FileOutputStream fos = new FileOutputStream(File/puth);
FileOutputStream fos = new FileOutputStream(File/puth,boolean append);//true:追加写
write(int d) 写出一字节的数据
write(byte[])写出给定字节数组中的所有数据
write(byte[]byte,int ofer,int len)从给定字节数组的指定位置出写出len个字节的数据
BufferedInputStream bis = new BufferedInputStream(InputStream is);
BufferedOutputStream bos = new BufferedOutputStream(OutputStream os);
bos.flush();
就算缓冲区没有满,直接写出数据,在关流操作的时候自动调用
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("src/测试.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src/测试01.txt"));
byte[] b = new byte[1024*10];
int len = -1;
while((len = bis.read(b))!=-1) {
bos.write(b,0,len);
}
bos.close();
bis.close();
}
FileReader fr = new FileReader(File/path);
int read():返回一字符的数据,以int的形式返回
read(char[] chbuffer):将数据读到给定的字符数组中,并返回读到的长度
FileWrite fw = new Filewrite(File/path);
FileWrite fw = new FileWrite(File/path,boolean,append);//追加写操作
write(String) ;允许直接写入String
FileReader fr = new FileReader("src/测试.txt");
FileWriter fw = new FileWriter("src/测试01.txt");
char[] c = new char[10];
int len =-1;
while((len = fr.read(c))!=-1) {
fw.write(c, 0, len);
}
fw.close();
fr.close();
FileInputStream fis = new FileInputStream(path);//创建文件输入流
InputStreamReader isr = new InputStreamReader(fis);//创建转换流
BufferedReader br = new BufferedReader(isr);//创建字符缓冲输入流
FileOutputStream fos = new FileOutputStream(path);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
String str ="测试字符缓冲输出流";
bw.write(str);
bw.flush();
System.out.println("写入成功");
PrintWrite pw = new PrintWrite(Paht/file/OutputStream)
PrintWrite pw = new PrintWrite(Paht/file/OutputStream,boolean,autoFlush)//带自动行刷新
println():带自动行刷新的写入
print():不带自动行刷新的写入
字符流和字节流相互转换的桥梁
字节流——————>字符流
FileInputStream fis = new FileInputStream("path"/file);
FileInputStream fis = new FileInputStream("path"/file,指定编码格式)
字符流——————>字节流
FileOutputStream fos = new FileOutputStream("src\\day0324\\cf2.txt",true);
OutputStreamWriter osw = new OutputStreamWriter(fos);
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("src/a.txt"));
osw.write("测试使用转换流写入数据");
osw.close();
InputStreamReader isr = new InputStreamReader(new FileInputStream("src/a.txt"));
char[] c = new char[100];
int len = isr.read(c);
isr.close();
System.out.println(new String(c,0,len));
将某个类的实例以对象的形式存入磁盘
ObjectInputStream ois = new ObjectInputStream(InputStream in);
ObjectOutputStream oos = new ObjectOutputStream(OutputStream out);
将对象转成一组字节数组的过程叫做对象序列化
被写入文件的对象的类一定要实现Serializable接口,用于在类中添加一个不可变的序列版本号,还原的时候将会检测,如果版本号不一致则还原失败
private static final long serialVersionUID = XXXL;
被该关键字修饰的类属性,在序列化的时候会被忽略,达到瘦身的目的