package bao1;
import java.io.Serializable;
public class Student implements Serializable{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package bao1;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class Test {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
List<Student> list=new ArrayList<Student>();
Student s1=new Student("王",20);
Student s2=new Student("李",20);
list.add(s1);
list.add(s2);
OutputStream os=new FileOutputStream("D:/save.bin");
//java 提供类是 输出流 ObjectOutputStream
ObjectOutputStream oos=new ObjectOutputStream(os);
oos.writeObject(list);
oos.close();
os.close();
System.out.println("serialize ok!");
}
}
import java.awt.BufferCapabilities.FlipContents;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import
java.io.FileWriter;import java.io.IOException;import java.io.Reader;import java.io.Writer;public class Frist {/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {// TODO Auto-generated method stub//写文件File file=new
File("E:\\S2226.txt");file.createNewFile(); //判定文件是否存在if (file.exists()) {System.out.println("exists");}File file2 = file.getAbsoluteFile() ;long length = file.length();long time = file.lastModified();System.out.println("文件大小"+length);System.out.println(time);System.out.println(file2);
//读文件FileInputStream fis=new FileInputStream("E:/S2226.txt");byte[] bytes=new byte[1024];int data;while((data=fis.read(bytes))!=-1){ String temp=new String(bytes,0,data);System.out.println(temp);}fis.close();//2.Reader reader=new FileReader("E:/S2226.txt");BufferedReader
br=new BufferedReader(reader);String line ;while((line= br.readLine())!=null){System.out.println(line);}br.close();reader.close();//写文件//1.String words="今天气很好啊";FileOutputStream fos=new FileOutputStream("E:/S2226.txt");byte[] bytess = words.getBytes(); fos.write(bytess);
fos.close(); System.out.println("ok!"); //2.Writer writer=new FileWriter("E:\\S2226.txt");BufferedWriter bw=new BufferedWriter(writer);String word="你好";bw.write(word);bw.close();writer.close();package bao;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Test {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream is=new FileInputStream("C:/ming.png");
DataInputStream dis=new DataInputStream(is);
//copy到目标路径
OutputStream os=new FileOutputStream("D:/ming.png");
DataOutputStream dos=new DataOutputStream(os);
byte[] bytes=new byte[1024];
int data ;
while((data= dis.read(bytes))!=-1){
dos.write(bytes, 0, data);
}
dos.close();
os.close();
dis.close();
is.close();
System.out.println("copy");
}
}
}}
转载请注明原文地址: https://ju.6miu.com/read-674524.html