课堂案例
package day31.t1;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestOutPutStream {
public static void main(String[] args) throws IOException {
FileOutputStream os = new FileOutputStream("D:\\develop_tools\\课堂练习\\day31\\Files\\test1.txt");
FileOutputStream fos = new FileOutputStream("Files\\target.txt");
byte[] bytes = new byte[]{65,66,67,68,69,70};
fos.write(bytes, 2, 2);
os.write(65);
os.write(66);
os.write(67);
os.write(68);
os.write('E');
fos.write(bytes);
}
}
效果图:
package day31.t1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestInputStream {
public static void main(String[] args) throws IOException {
FileInputStream fis =new FileInputStream("Files\\target.txt");
// while(true){
// int n =fis.read();
// if(n== -1){
// break;
// }
// System.out.println((char)n);
// }
byte[] bytes = new byte[3];
while(true){
int count =fis.read(bytes); //参数为数组的返回值为int类型
if(count == -1){ //返回的是每次读的个数
break;
}
for(int i =0 ;i<count;i++){//count 是每次read的个数
System.out.println((char)bytes[i]);//这个循环将每次循环的内容都打印出来
}
}
}
}
运行效果图
package day31.t1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestFlieIO {
public static void main(String[] args) throws IOException {
//复制图片过程:首先将图片输入到程序中,然后将图片输出到文件中
FileInputStream fis= new FileInputStream("C:\\Users\\张阔\\Desktop\\111.png");
byte[] bytes =new byte[1024 * 1024 * 3];
fis.read(bytes);
FileOutputStream fos =new FileOutputStream("Files\\zhuomian.png");
fos.write(bytes);
fis.close();
fos.close();
}
}
效果图:
package day31.t2;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestBufferedOutput {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("Files\\buff.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write('A');
bos.write('B');
bos.write('C');
bos.write('D');
bos.flush(); //执行flush 才会刷新缓冲区,并写入内容
bos.write('E');
bos.write('F');
bos.close(); //执行close 级联执行了flush
}
}
效果图:
package day31.t3;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class TestObjectStream {
public static void main(String[] args) throws IOException {
FileOutputStream fos =new FileOutputStream("Files\\obj.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(new Student("jack",18,"男",100D,new Address("北京","100000")));
oos.writeObject(new Student("mike",19,"男",99D,new Address("北京","100000")));
oos.writeObject(new Student("annie",18,"女",100D,new Address("北京","100000")));
oos.flush();
InputStream is = new FileInputStream("Files\\obj.txt");
ObjectInputStream ois = new ObjectInputStream(is);
while(true){
try {
Object obj =ois.readObject();
System.out.println(obj.toString());
} catch (Exception e) {
break;
}
}
}
}
class Student implements Serializable{
String name;
Integer age;
String sex;
double score;
transient Address add;
public Student(String name, Integer age, String sex, double score,Address add) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
this.add=add;
}
@Override
public String toString() {
return “Student [name=” + name + “, age=” + age + “, sex=” + sex + “, score=” + score + “, add=” + add + “]”;
}
}
class Address implements Serializable {
String position;
String zipCode;
public Address(String position, String zipCode) {
super();
this.position = position;
this.zipCode = zipCode;
}
@Override
public String toString() {
return “Address [position=” + position + “, zipCode=” + zipCode + “]”;
}
}
运行效果图:
课后习题
-
输入 字节 节点
-
int 读入的一个字节的值,读到末尾返回-1
读取的字节个数 把所读的内容存入的数组
读取的字节个数 起始位置 读取长度 -
A B
-
自动创建 覆盖 追加写入 会
-
主函数之后没有声明异常
fin.close方法的调用最好放在finally代码块中
源代码:
package day31.t4;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestQ13_8 {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream("text.txt",true);
String s ="Hellow World";
byte[] bytes=s.getBytes();
fos.write(bytes);
fis =new FileInputStream("text.txt");
byte[] bytes1 =new byte[10];
while(true){
int count =fis.read(bytes1);
if(count ==-1){
break;
}
for(int i = 0; i<count;i++){
System.out.println((char)bytes1[i]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行效果图:
-
Serializable transient
-
print(Object obj)方法
意思是调用这个对象的toString方法。
writeObject(Object obj)方法
是把对象写到一个流中
总体而言,print是输出对象 write是写入对象 -
B Address 类没有实现Serializable接口