io(文件操作)
in (读取) out(写出)
java.io.File
常用的三个构造方法:
/*
*File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
*/
public class Demo01 {
public static void main(String[]args) throws IOException {
File file = new File("D://1.txt");
boolean flag = file.createNewFile ();//当且仅当具有此名称的文件尚不存在时,以原子方式创建 由此抽象路径名命名的新空文件。
System.out.println (flag?"创建成功":"创建失败");
}
}
/*
*File(File parent, String child) //先传一个文件夹,在文件夹里创建文件。
*/
public class Demo02 {
public static void main(String[]args) throws IOException {
File file = new File("D://hahaha");
//boolean flag = file.mkdir (); //创建文件夹
//System.out.println (flag?"创建成功":"创建失败");
File dir = new File(file,"a.txt");
dir.createNewFile ();
/*
*File(String parent, String child)//先传字符串路径,在文件夹里创建文件。
*/
File dd = new File ("D://hahaha","c.txt");
dd.createNewFile ();
}
}
常用方法:
pathSeparator 与系统相关的路径分隔符,为方便起见,表示为字符串。 //详细见File -字段
文件遍历案例
public class Demo03 {
public static void main(String[]args) throws IOException {
File e = new File ("E:\\");
File [] files = e.listFiles ();
ListFiles(files);
}
private static void ListFiles(File[] files) {
if(files != null && files.length>0){
for( File file:files) {
if (file.isFile ()) { //判断是否为文件
if (file.getName ().endsWith (".pdf")) {
//找到一个avi文件
if(file.length ()>10*1024*1024) {
System.out.println ("找到一个avi文件:" + file.getAbsolutePath ());
}
}
}else {
File[] file2 = file.listFiles ();
ListFiles (file2);
}
}
}
}
}
文件过滤器
了解即可
相对路径与绝对路径
相对路径:Java中不完整路径 ,相对于项目下的路径,a.txt.
绝对路径:路径只能拿到一个文件,c://a.txt.
流概述(文件内容)
*Io流概述
* 可以将这种数据操作,看作是一种数据的流动,按照流动的方向分为输入Input和输出Output
* Java中的IO操作主要指的是java.io包下的一些常用类的使用,通过这些常用类对数据
* 进行读取(输入Input)和写出(输出Output)
*
* IO流的分类
* c
*
* 字节流:
* - 输入流:(顶级父类)InputStream
* - 输出流: ()OutputStream
* 字符流:
* -输入流:Reader
* -输出流:Writer
* 一切皆字节:
* 计算机中任何数据(文本,图片,音乐等等) 都是以二进制的形式存储的。
* 在数据传输时,在数据传输时,也都是以二进制的形式存储的。
* 后续学习的任何流,在数据底层都是二进制
*/
java.io.OutputStream
-
-
write(byte[] b, int off, int len) //从off开始 ,len= 写的个数
write(int b) 将指定的字节写入此输出流。 //输入是int,传出wei字节
java.io.FileOutputStream
构造方法:
FileOutputStream(FileDescriptor fdObj) 创建要写入指定文件描述符的文件输出流,该文件描述符表示与文件系统中实际文件的现有连接。
FileOutputStream(File file, boolean append) 创建文件输出流以写入由指定的 File对象表示的文件。
FileOutputStream(String name) 创建文件输出流以写入具有指定名称的文件。
FileOutputStream(String name, boolean append) 创建文件输出流以写入具有指定名称的文件。
1.3传入的是对象
4,5传的是路径
public class Demo05 {
public static void main(String[]args) throws IOException {
//FileOutputStream
FileOutputStream fos = new FileOutputStream ("d://a.txt",true);
byte [] bytes = {65,56,78,56,46};
fos.write (bytes);
fos.close ();
System.out.println ("已经写出");
}
}
java.io.FileInputStream
构造方法
FileInputStream(File file) //通过打开与实际文件的连接来创建FileInputStream ,该文件由文件系统中的 File对象 file命名。(对象)
FileInputStream(FileDescriptor fdObj) //使用文件描述符 fdObj创建 FileInputStream ,该文件描述符 表示与文件系统中实际文件的现有连接。
FileInputStream(String name) //通过打开与实际文件的连接来创建 FileInputStream ,该文件由 文件系统中的路径名 name命名。 (路径)
方法
close() 关闭此文件输入流并释放与该流关联的所有系统资源。
read() 从输入流中读取下一个数据字节。 从输入流中读取下一个数据字节。 值字节返回int ,范围 为0至255 。 如果由于到达流末尾而没有可用字节,则返回值-1 。 此方法将阻塞,直到输入数据可用,检测到流的末尾或抛出异常。
read(byte[] b) 从输入流中读取一些字节数并将它们存储到缓冲区数组 b 。//最后一次读取,如果不够一组,元数据不会被完全清空。
读取一个字的方法 read()
public class Demo06 {
public static void main(String[]args) throws IOException {
//FileInputStream
FileInputStream fis = new FileInputStream ("d://a.txt");
// byte b = (byte) fis.read();
// System.out.println ((char) b);
// byte c = (byte) fis.read ();
// System.out.println ((char) c);
while (true){
byte b = (byte) fis.read ();//读的内容超出最后一个,返回-1;不会报错,除非内存溢出
if(b==-1){
break;
}
System.out.println ((char) b);
}
fis.close ();
}
}
*/读一组数
用返回值是否为-1来判断我们打印几次
public class Demo07 {
public static void main(String[]args) throws IOException {
//FileInputStream
FileInputStream fis = new FileInputStream ("d://a.txt");
byte [] bytes = new byte[10];
int len = fis.read (bytes);
System.out.println (new String (bytes,0,len));
len = fis.read (bytes);
System.out.println (new String (bytes,0,len));
len = fis.read (bytes);
System.out.println (new String (bytes,0,len));
fis.close ();
}
}
用返回值是否为-1来判断我们打印几或者是判断len
public class Demo07 {
public static void main(String[] args) throws IOException {
//FileInputStream
FileInputStream fis = new FileInputStream ("d://a.txt");
byte[] bytes = new byte[10];
int len;
while (true) {
len = fis.read (bytes);
System.out.println (new String (bytes, 0, len));
if(len<10){
break;
}
or
if(fis.read () ==-1){
break;
}
}
fis.close ();
}
}
文件加密和解密工具
/*
*文件加密或解密
*/
public class Demo08 {
public static void main(String[]args) throws IOException {
System.out.println ("请输入文件存储的全路径:");
Scanner input = new Scanner (System.in);
String name = input.nextLine ();
//原文件: 777.pdf
File oidFile = new File (name);
//加密存储文件 mi-777.pdf
File newFile = new File (oidFile.getParentFile (),"mi-"+oidFile.getName ());
FileInputStream fis = new FileInputStream (oidFile);
FileOutputStream fos = new FileOutputStream (newFile);
while (true) {
int b = fis.read ();
if(b==-1){
break;
}
//任何数据^相同的数字两次,结果就是其本身
fos.write (b^10);
}
System.out.println("加密或解密已完成");
}
}
字符输出
public class Demo09 {
public static void main(String[]args) throws IOException {
//Writer
FileWriter fw = new FileWriter ("d://f.txt",true);//true:是追加模式,没有的相对于清空
fw.write ("锄禾日党务,汗滴禾下土");
System.out.println ("擦胡歌");
fw.close ();
}
}
append ()
public class Demo09 {
public static void main(String[]args) throws IOException {
//Writer
FileWriter fw = new FileWriter ("d://f.txt",true);
// fw.write ("锄禾日党务,汗滴禾下土")
FileWriter fw2 = (FileWriter) fw.append ("鹅鹅鹅,去学习系统");//append()//追加,且返回this
fw.append ("鹅鹅鹅,去学习系统").append("测试测试测试");
@Override
public Writer append(CharSequence csq) throws IOException {
if (csq instanceof CharBuffer) {
se.write((CharBuffer) csq);
} else {
se.write(String.valueOf(csq));
}
return this;
// }
System.out.println ("fw ==fw2");
fw.close ();
}
}
字符读取
/*
*字符输入
*/
public class Demo10 {
public static void main(String[]args) throws IOException {
//Reader
FileReader fr = new FileReader ("d://f.txt");
// while(true){
// int c=fr.read ();
// if(c==-1){
// break;
// }
// System.out.print ((char) c);//一个个的读
// }
char [] chars = new char[100];
int len = fr.read (chars);
String text = new String (chars,0,len);
System.out.println (text);
fr.close ();//关闭会导致刷新,追加也会刷新( flush ();)
}
}
字节流转化为字符流
public class Demo11 {
public static <ReaderInputStream> void main(String[]args) throws IOException {
FileInputStream frs = new FileInputStream ("d://f.txt");
//将字节输入流,转换为字符输入流
//参数1,要转换的字节流
//参数2,指定的编码格式
InputStreamReader isr = new InputStreamReader (frs,"UTF-8");
while(true){
int c =isr.read ();
if(c ==-1){
break;
}
System.out.println ((char)c);
}
}
}
public class Demo12 {
public static <ReaderInputStream> void main(String[]args) throws IOException {
FileOutputStream frs = new FileOutputStream ("d://f.txt",true);
OutputStreamWriter oew = new OutputStreamWriter (frs);
oew.write ("充电电池的懊恼");
oew.flush ();
oew.close ();
}
}
Print 与BuffereReader
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pY4yJ5ch-1595864645306)(C:\Users\hu\AppData\Roaming\Typora\typora-user-images\1595845425613.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xtkJw1Nx-1595864645312)(C:\Users\hu\AppData\Roaming\Typora\typora-user-images\1595845580632.png)]
收集异常日志
public class Demo13 {
public static <ReaderInputStream> void main(String[]args){
try {
String s = null;
s.toString ();
}catch ( Exception e) {
e.printStackTrace ();//打印到控制台
}
}
}
//打到文件夹
public class Demo13 {
public static <ReaderInputStream> void main(String[]args) throws FileNotFoundException {
try {
String s = null;
s.toString ();
}catch ( Exception e) {
PrintWriter pw = new PrintWriter ("d://f.txt");
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-mm-dd HH:mm:zz");
pw.println (sdf.format (new Date ()));
e.printStackTrace (pw);
pw.close ();
e.printStackTrace ();
}
}
}
Properties(extends Hasmap)
public class Demo14 {
public static void main(String[]args) throws IOException {
// Properties ppt = new Properties ();
// ppt.put ("name","金苹果");
// ppt.put ("info","讲述了苹果种植的过程");
// FileWriter fw = new FileWriter ("d://Book.properties");
// ppt.store (fw,"存储的图书");
// fw.close ();
Properties ppt = new Properties ();
FileReader fe = new FileReader ("d://Book.properties");
ppt.load (fe);
System.out.println (ppt.getProperty ("name"));
System.out.println (ppt.getProperty("info"));
}
}
序列化 反序列化
序列化 :将创建对象以文件形式存储起来,
反序列化:把文件对象载到程序,读取
public class Book {
public static void main(String []args) throws IOException, ClassNotFoundException {
//序列化
/*Booktext b = new Booktext ("金苹果","描述苹果种植的过程");
ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream ("D://Book.txt"));
oos.writeObject (b);
oos.close ();*/
//反序列化
ObjectInputStream fe = new ObjectInputStream (new FileInputStream ("D://Book.txt"));
Object o = fe.readObject ();
System.out.println (o);
}
static class Booktext implements Serializable { //只是为了申明是他的子类,没有重写任何方法
private String name; //属性也都必须实习Serializable
private String info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Booktext() {
}
public Booktext(String name, String info) {
this.name = name;
this.info = info;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", info='" + info + '\'' +
'}';
}
}
}
try -with -resources
public class Demo15 {//jdk1.7
public static void main(String[]args) throws IOException {
try(FileReader fr = new FileReader ("d://book.txt")) {//继承CLoseable类(close())
int c = fr.read ();
System.out.println ((char) c);
}catch (IOException e) {
e.printStackTrace ();
}
// JDK1.9
FileReader fr = new FileReader ("d://book.txt");
PrintWriter pw = new PrintWriter ("d://book.txt");
try(fr;pw){
int c = fr.read ();
System.out.println ((char) c);
}catch (IOException e) {
e.printStackTrace ();
}
}