package com.job;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class renwuyi {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File file =new File("D:\\a.jpg");//要复制的图片
File file2 =new File("D:\\a\\b.jpg");//
file2.createNewFile();
FileInputStream fis=new FileInputStream(file);
FileOutputStream fos =new FileOutputStream(file2);
int len =fis.available();
//边读边写 如果用fileinputstream读汉字,速度比较慢,一次读8bit
for (int i = 0; i < len; i++) {
fos.write(fis.read());//边读边写
}
fos.close();fis.close();
}
}
package com.job;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.InputStreamReader;
public class BufferReadtest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// FileInputStream fis=new FileInputStream("D:\\a.txt");
// InputStreamReader isr=new InputStreamReader(fis);
// BufferedReader br=new BufferedReader(isr);
// String str="";
// while((str=br.readLine())!=null){
// System.out.println(str);
// }
// br.close();
//BufferedWriter
File file=new File("d:\\a\\a.txt");
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
String str="你好,我的祖国\b我心有我";
bw.write(str);
str="人生百味,尝多无味";
bw.newLine();
bw.write(str);
bw.close();
}
}
package com.job;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedBufferWriteTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FileInputStream fis=new FileInputStream("D:\\a.txt");
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
// br.close();
//BufferedWriter
File file=new File("d:\\a\\d.txt");
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
// String str="你好,我的祖国\b我心有我";
String str="";
while((str=br.readLine())!=null){
System.out.println(str);
bw.write(str);
bw.newLine();
}
str="人生百味,尝多无味";
bw.newLine();
bw.write(str);
bw.close();
br.close();
}
}
package com.entriy;
import java.io.Serializable;
public class student implements Serializable {
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public student(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
public student() {
super();
}
}
package com.entriy;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class test {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//------序列化 ObjectOutputStream 对对象进行操作
//------序列化的文本是二进制
/* 1.创建一个类
* 2.实例化一个对象 赋值
* 实例化
* 4.写
* 5.关闭流
* 有可能出现NotSerializableException异常
* 解决方法就是被实例化的类要实现Serializable接口
* 序列化的文本是二进制,想要查看的话需要反序列化。
*/
student stu=new student("小王","男");
System.out.println(stu);
FileOutputStream fos=new FileOutputStream("D:\\a\\new.txt");
ObjectOutputStream op=new ObjectOutputStream(fos);
op.writeObject(stu);
op.close();
//--------反序列化 ObjectInputStream
FileInputStream fis=new FileInputStream("D:\\a\\new.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
student stu1=(student) ois.readObject();
System.out.println(stu1.getName()+"\t"+stu1.getSex());
ois.close();//关闭流
}
}
package com.entriy;
import java.awt.List;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class duoduixiang {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//---------------多个对象-------------//需要把对象放到集合里面
student stu1 = new student("小王", "男");
student stu2 = new student("小明", "男");
student stu3 = new student("小丹", "女");
ArrayList<student> list=new ArrayList<student>();
list.add(stu1);list.add(stu2);list.add(stu3);
FileOutputStream fos = new FileOutputStream("D:\\a\\new.txt");
ObjectOutputStream op = new ObjectOutputStream(fos);
op.writeObject(list);
op.close();
FileInputStream fis=new FileInputStream("D:\\a\\new.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
ArrayList<student> list1= (ArrayList<student>) ois.readObject();
for (int i = 0; i <list1.size(); i++) {
System.out.println(list.get(i).getName()+"\t"+list.get(i).getSex());
}
ois.close();
}
}