文件操作
import java.io.*;
public class FileDemo {
public static void main(String[] args) {
File f1=new File("src\\aa.txt");//相对路径,如果没有前面的src,就在当前目录创建文件
if(f1.exists()) {
System.out.println("文件已经存在");
}else {
try {
f1.createNewFile();
System.out.println("文件创建成功");
} catch (Exception e) {
// TODO: handle exception
}
}
System.out.println("文件已经存在:"+f1.exists());
System.out.println("文件的名字:"+f1.getName());
System.out.println("文件的路径:"+f1.getPath());
System.out.println("文件的绝对路径:"+f1.getAbsolutePath());
System.out.println("是目录吗:"+f1.isDirectory());
System.out.println("文件大小:"+f1.length());
}
}
遍历文件夹及目录下的文件夹
//遍历文件和目录下的文件
public static void fun(File files) {
File[] fs = files.listFiles();
for (File fs1 : fs) {
if (fs1.isDirectory()) {
fun(fs1);
}
if (fs1.isFile()) {
System.out.println(fs1);
}
}
}
字符流复制文件
import java.io.*;
public class TEst1 {
public static void main(String[] args) throws IOException {
FileReader w = new FileReader("text.txt");
FileWriter f = new FileWriter("abc.txt");
char[] c = new char[1024];
int len;
while((len = w.read(c))!=-1){
System.out.println(len);
System.out.println(c);
f.write(c,0,len);
}
f.close();
w.close();
}
}
字节流复制图片
import java.io.*;
public class TEst1 {
public static void main(String[] args) throws IOException {
FileInputStream f = new FileInputStream("D:\\zhaopian.jpg");
FileOutputStream o = new FileOutputStream("abc.txt");
byte[] b =new byte[1024];
int len;
while ((len=f.read(b))!=-1){
System.out.println(len);
o.write(b,0,len);
}
f.close();
o.close();
}
}
字符缓冲流复制文件
import java.io.*;
public class TEst1 {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader("text.txt"));
BufferedWriter w = new BufferedWriter(new FileWriter("writer.txt"));
String s;
while ((s=r.readLine())!=null) {
w.write(s);
w.newLine();
w.flush();
}
w.close();
r.close();
}
}
字节缓冲流复制文件
import java.io.*;
public class TEst1 {
public static void main(String[] args) throws IOException {
BufferedInputStream f = new BufferedInputStream(new FileInputStream("D:\\zhaopian.jpg"));
BufferedOutputStream o = new BufferedOutputStream(new FileOutputStream("abc.txt"));
byte[] b =new byte[1024];
int len;
while ((len=f.read(b))!=-1){
System.out.println(len);
o.write(b,0,len);
}
f.close();
o.close();
}
}
集合写文件
import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class TEst1 {
public static void main(String[] args) throws IOException {
//创建ArrayList集合
ArrayList<Student> array = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("001","左",19,"天津");
Student s2 = new Student("002","黑",22,"天津");
Student s3 = new Student("003","曹",20,"长治");
Student s4 = new Student("004","李",21,"太原");
//
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
//创建字符缓冲流写入对象
BufferedWriter w = new BufferedWriter(new FileWriter("text.txt"));
for (Student s : array){
//吧学生对象的数据拼接成指定格式的字符串
StringBuilder sb = new StringBuilder();
sb.append(s.getSid()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
//调用字符缓冲流输出流对象的方法写数据
w.write(sb.toString());
w.newLine();
w.flush();
}
w.close();
}
}
文件到集合
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class TEst2 {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader("text.txt"));
ArrayList<Student> array = new ArrayList<Student>();
//调用字符缓冲输入流对象的方法读数据
String line;
while ((line=r.readLine())!=null){
//把读到到的字符串数据用split方法进行分割,得到一个字符串数组
String[] strarray = line.split(",");
//创建学生对象
Student s = new Student();
//吧字符串数组中的每一个元素取出来对应的赋值给学生对象的成员变量值
s.setSid(strarray[0]);
s.setName(strarray[1]);
s.setAge(Integer.parseInt(strarray[2]));
s.setAddress(strarray[3]);
//把学生对象添加到集合
array.add(s);
}
r.close();
for (Student s :array){
System.out.println(s.getSid()+","+s.getName()+","+s.getAge()+","+s.getAddress());
}
}
}
集合到文件排序版
public class TEst2 {
public static void main(String[] args) throws IOException {
//创建treeset集合,通过比较器进行排序
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//成绩从高到低排序
int num = s2.getSum()-s1.getSum();
//次要条件
int num2 = num ==0 ? s1.getChinese() - s2.getChinese() : num;
int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;
int num4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3;
return num4;
}
});
//录入学生信息
for(int i=0;i<5;i++){
Scanner sc = new Scanner(System.in);
System.out.println("请录入第"+(i+1)+"个学生信息");
System.out.println("学生姓名");
String name = sc.nextLine();
System.out.println("语文成绩");
int chinese = sc.nextInt();
System.out.println("数学成绩");
int math = sc.nextInt();
System.out.println("英语成绩");
int english = sc.nextInt();
//创建学生对象
Student s = new Student();
s.setName(name);
s.setChinese(chinese);
s.setMath(math);
s.setEnglish(english);
ts.add(s);
}
//创建字符流输出对象
BufferedWriter bf = new BufferedWriter(new FileWriter("text.txt"));
//遍历集合,得到每一个学生对象
for(Student s : ts){
StringBuilder sb = new StringBuilder();
sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish());
bf.write(sb.toString());
bf.newLine();
bf.flush();
}
bf.close();
}
}
字节打印流
PrintStream ps = new PrintStream(filename:)
ps.print()
字符打印流
PrintWrite pw = new PrintWrite(new FileWriter(filename:),true) / / true是自动刷新
pw.print()
字符打印流
public class TEst2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("text.txt"));
PrintWriter pw = new PrintWriter(new FileWriter("writer.txt"));
String s;
while ((s = br.readLine()) != null) {
pw.println(s);
}
br.close();
pw.close();
}
}
对象序列化
定义学生类的时候必须定义无参构造方法
实现接口implements Serializable
如果类发生变化 可以给序列化类和反序列化类一个定义的版本号
private static final long serialVersionUID = 42L
如果一个成员变量不愿意被序列化 可以变成临时的
private transient int age
public class ObjectOutPutStreamdemo {
public static void main(String[] args) throws IOException {
//ObjectOutputStream (OutStream out): 创建一个写入指定的OutputStream的objectOutputStream
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("text.txt"));
//创建学生对象
Student s = new Student("张振昊",18);
//将指定的对象写入objectoutputstream
oos.writeObject(s);
oos.close();
}
}
对象反序列化
public class ObjectOutPutStreamdemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//ObjectInputStream (InputStream in): 创建一个写入指定的OutinStream的objectinputStream
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("text.txt"));
//Object readObject() : 从ObjectInputStream中读取一个对象
Object o = ois.readObject();
Student s = (Student)o;
System.out.println(s.getName()+","+s.getAge());
ois.close();
}
}