重庆实惠网站建设5118数据分析平台
文章目录
- File文件操作
- IO流
- 处理流
- 缓冲流
- 转换流
- 对象流
File文件操作
利用File类来操作。
文件操作中常用到相对目录和绝对路径
package org.File; import java.io.File; public class demo01 { public static void main(String[] args) { try{ File file = new File("./test.txt");
// 文件路径是以项目目录为起点的。
// 创建一个文件。 file.createNewFile(); }catch (Exception e ) { e.printStackTrace(); } }
}
获取文件名
package org.File; import java.io.File; public class demo02 { public static void main(String[] args) { try { File file = new File("src/main/java/org/File/Files/test.txt"); file.createNewFile();
// 不能创建文件夹,创建的是文件
// System.out.println(file.getParentFile());//得到上一层文件夹的对象
// System.out.println(file.getParent());//得到上一层文件夹
// System.out.println(file.exists());//判断文件是否存在 file.mkdir();file.mkdirs();
// 创建文件夹,一般选择mkdirs() }catch (Exception e ){ e.printStackTrace(); } }
}
文件的所有相关操作
package org.File; import java.io.File; public class demo02 { public static void main(String[] args) { try { File file = new File("src/main/java/org/File/Files/test.txt"); file.createNewFile();
// 不能创建文件夹,创建的是文件
// System.out.println(file.getParentFile());//得到上一层文件夹的对象
// System.out.println(file.getParent());//得到上一层文件夹
// System.out.println(file.exists());//判断文件是否存在 file.mkdir();file.mkdirs();
// 创建文件夹,一般选择mkdirs() // 重命名 file.renameTo(new File("src/main/java/org/File/Files/test2.md"));
// 删除文件 file.delete(); System.out.println(file.isAbsolute());
// 判断是否是绝对路劲
// 判断是不是文件夹 System.out.println(file.isDirectory()); System.out.println(file.isFile());
// 判断是不是文件 System.out.println(file.length());
// 查看文件大小 System.out.println(file.getName());
// 文件名字。 }catch (Exception e ){ e.printStackTrace(); } }
}
创建文件的完整步骤
package org.File; import java.io.File;
import java.io.IOException;
import java.text.FieldPosition; public class demo03 { public static void main(String[] args) { File file = new File("resource/test.txt");
// 1、判断上层文件夹是否存在 File parentFile = file.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); }
// 2、创建文件 try { file.createNewFile(); } catch (IOException e) { throw new RuntimeException(e); } }
}
IO流
流的分类:
- 按照读写的方向讲,是输入流和输出流
- 按照读写单位分,字节流和字符流
- 流的功能不同分,节点流和处理流
流的家族体系
输入 | 输出 | |
---|---|---|
字节流 | InputStream | OutputStream |
字符流 | Reader | Writer |
节点流和处理流
节点流:直接连接在文件上的
处理流:套在其他流上的
文件流:
FileInputStream
FileOutputStream
FileReader
FileWriter
package org.FileStream; import com.sun.org.apache.xpath.internal.operations.String; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; public class demo01 { public static void main(String[] args) throws Exception { // 创建流 FileInputStream fils = new FileInputStream(new File("./src/main/resources/a.txt")); int read = fils.read(); System.out.println(read);
// 输出115 s的Ascii码 System.out.println((char) read); byte[] bs = new byte[1024]; int len = fils.read(bs ); //返回多少个字节 System.out.println(new String()); } }
读取文件的一套写法
package org.FileStream; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; public class demo02 { public static void main(String[] args) throws Exception { FileInputStream files = new FileInputStream(new File("./src/main/resources/a.txt")); byte[] bytes = new byte[1024]; int len=0; while ((len= files.read(bytes))!=-1){ String s = new String(bytes, 0, len); System.out.println(s); }
// 关闭文件流 files.close(); }
}
写入文件:
public FileOutputStream(File file, boolean append)
append为true时是在文件后加入数据
默认是false,会清空文件,再添加数据。
package org.FileStream; import com.sun.org.apache.xpath.internal.operations.String; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader; public class demo04 { public static void main(String[] args) throws Exception { FileReader files = new FileReader(new File("./src/main/resources/a.txt")); char[] cs = new char[1024]; int len = 0 ; while ((len = files.read(cs))!=-1){ System.out.println(new String(cs,0 ,len)); } files.close(); }
}
package org.FileStream; import java.io.File;
import java.io.FileWriter;
import java.io.IOException; public class demo05 { public static void main(String[] args) throws IOException { FileWriter files = new FileWriter(new File("./src/main/resources/a.txt"),true); files.write("simple"); files.flush(); files.close(); }
}
将一张图片从一个地方复制到另一个地方
就将图片从a移动到b
package org.FileStream; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; public class demo06 { public static void main(String[] args) throws Exception { File f = new File("a/ba1.jpg"); FileInputStream fis = new FileInputStream(new File("a/ba1.jpg")); FileOutputStream fos = new FileOutputStream(new File("b/bb1.jpg"));
// 读取内容 byte[] bs = new byte[1024]; int len =0; while ((len = fis.read(bs))!=-1){//读取数据 fos.write(bs,0,len);//写数据 } fis.close(); fos.flush(); fos.close(); // 如果是剪切 ,就把原来的图片删掉就行了
// f.delete(); }
}
处理流
缓冲流
带有一个缓冲区的数据流
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
package org.Process_of_Stream; import java.io.*; public class demo01_Buffer_stream { public static void main(String[] args) throws Exception {
// BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("/src/main/resources/a.txt")));
// 这里面套了三层东西,缓冲区套在节点流上,节点流套在文件流上 String path = Thread.currentThread().getContextClassLoader().getResource("a.txt").getPath(); System.out.println(path);
// 获取路径 BufferedReader br = new BufferedReader(new FileReader(new File(path))); System.out.println(br.readLine()); //读文本文件最好用的方法 System.out.println(br.readLine()); }
}
标准的读取文件的写法
package org.Process_of_Stream; import java.io.*; public class demo01_Buffer_stream { public static void main(String[] args) throws Exception {
// BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("/src/main/resources/a.txt")));
// 这里面套了三层东西,缓冲区套在节点流上,节点流套在文件流上 String path = Thread.currentThread().getContextClassLoader().getResource("a.txt").getPath(); System.out.println(path);
// 获取路径 BufferedReader br = new BufferedReader(new FileReader(new File(path))); // System.out.println(br.readLine()); //读文本文件最好用的方法
// System.out.println(br.readLine()); String str =""; while ((str=br.readLine())!=null ){ System.out.println(str); } }
}
转换流
有时需要将流进行转换. 一般我们得到的会是一个字节流. 这是一般我们需要转换成字符流
java中有的转换:
字节流 --> 字符流
InputStreamReader
OutputStreamWriter
package org.Process_of_Stream; import jdk.nashorn.internal.runtime.regexp.joni.exception.SyntaxException; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner; public class demo02_Convert_stream { public static void main(String[] args) throws Exception {
// 系统输入是一个字节流 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 通过转换流,将字节流转换成了字符流 String s = br.readLine(); System.out.println(s); }
}
package org.Process_of_Stream; import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer; public class demo03_Conver_stream { public static void main(String[] args) throws IOException { Writer writer = new OutputStreamWriter(System.out); writer.write("hello simplecatlover"); writer.flush();
// writer.close(); //这个流不可以关 System.out.println("sgjagjla"); }
}
对象流
也就是序列化和反序列化接口
两个类:
ObjectInputStream
ObjectOutputStream
类:
一定要implements Serializable
package org.Process_of_Stream.obj; import java.io.Serializable; public class Person implements Serializable { private int id; private String name; private int age; public Person(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; }
}
序列化:
package org.Process_of_Stream.obj; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream; public class test1 { public static void main(String[] args) throws Exception { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("person.dat"))); Person p = new Person(1,"simple",2); oos.writeObject(p); oos.flush(); oos.close(); }
}
反序列化:
package org.Process_of_Stream.obj; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream; public class test1 { public static void main(String[] args) throws Exception { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("person.dat"))); Person p = new Person(1,"simple",2); oos.writeObject(p); oos.flush(); oos.close(); }
}