菜鸟记所有篇章都是个人随手记,可能存在知识点遗漏,请多包涵。
之后篇章的菜鸟记 概念居多。
一、I/O: 流 思想
生活中什么地方能体现?
1.物流
2.水流
3.电流
4.客流
程序中的流的作用就是 将数据进行 相互传递;
java如何划分它们:
1.按方向划分:
输入流 InputStream / Reader
输出流 OutputStream / Writer
2.按类型划分:
字节流 : InputStream / OutputStream
字符流 : Reader / Writer
字节输入流: InputStream
字节输出流: OutputStream
java中如何表现一个字节 :byte
字符输入流: Reader
字符输出流: Writer
java中如何表现一个字符: char
二、File类: 文件
代表了两个内容 : 目录 : 就是路径 : C:\Program Files\Java\jdk1.7.0_71\bin
文件 : 文件夹 和 文件 : java文件夹
: Person.txt
File file = new File("C:\Program Files\Java\jdk1.7.0_71\bin");
File file = new File("java");
File file = new File("java.txt");
常用构造器:
File(String pathname);
File(String parent, String child);
File(File parent,String child);
常用方法:
1.createNewFile(); 创建一个新文件
createTempFile(String prefix, String suffix, File directory); 指定位置创建临时文件
前缀 后缀 文件夹
2.mkdir(); 创建 单层 文件夹
mkdirs(); 创建 多层 文件夹
3.delete();删除 注意可以直接删除文件 或 空文件夹 ,
不可以删除 含有内容的文件夹(目录)
4.exists(); 判断是否存在 , 一般都是如果不存在 就创建, 否则就不创建
5.getAbsoluteFile(); 获取绝对路径的对象, getPath();
getAbsolutePath(); 直接返回String 类型的路径
6.getName(); 获取文件 或 文件夹的名字
7.isDirectory(); 判断是否是 目录
9.isFile(); 判断是否是文件
10.isHidden(); 判断是否是隐藏文件
11.lastModified(); 最后的修改时间 long 类型毫秒值
12.length();文件内容的长度
*13. File[] listFiles(); 查看当前目录下内容,只能查看一层
14. file.renameTo(new File(“新名字”));
三、字节输入流 和 字节输出流:
InputStream :输入流 抽象类
1.FileInputStream:
close();
read(); 一次读一个
read(byte[] b); 一次性 读完
read(byte[] b,int offset , int len);
一次性读完, 将读完的数据 存储到指定数组的什么位置, 存多长
2.ObjectInputStream:
3.ByteArrayInputStream:
new ByteArrayInputStream(byte[] buf);
close();
read();
read(byte[] b, int off, int len);
4.PipedInputStream
OutputStream :输出流
ByteArrayOutputStream :
FileOutputStream:
FileOutputStream(File file);
FileOutputStream(File file,boolean append);
FileOutputStream(String name);
FileOutputStream(String name ,boolean append);
close();
write(int b)
write(byte[] b);
write(byte[] b , int offset , int len);
ObjectInputStream: 对象输入流 ----> 反序列化
readObject();
ObjectOutputStream: 对象 输出流 -----> 序列化
writeObject(Object obj);
序列化 和 反序列化的区别:
1.序列化的 意思 就是 将 内存中的 对象,用流的形式 存储到 物理文件中
2.反序列化的意思是 将物理文件中的 内容, 用过 流的方式 存储到 内存中
1.总案例
代码如下(示例):
1.创建百层文件夹
import java.io.File;
import java.io.IOException;
public class BaiCeng {
//递归
public static void del(File f1) {
if(f1.isDirectory()) {
File[] fs = f1.listFiles();
for(File f2 : fs) {
del(f2);
}
f1.delete();
}else {
f1.delete();
}
}
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
File f = new File("d:/school/PeiXun/IO/1") ;
//String c ="d:/school/PeiXun/IO" ;
/*for(int i = 1; i <= 100; i++) {
c += "/"+i;
System.out.println(c);
}*/
/*f = new File(c);
f.mkdirs();*/
del(f);
// 循环删除
/*for(int i = 1; i < 100; i ++) {
String c ="d:/school/PeiXun/IO" ;
//c +="/"+i;
for(int j = 1; j < 100-i; j++) {
c += "/"+j;
}
System.out.println(c);
f = new File(c);
f.delete();
}*/
/* f = new File(c);
f.mkdirs();
*/
}
}
2.利用io流对图片进行加密和解密
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Gif {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//对222图片进行加密
FileInputStream fis = new FileInputStream("d:/school/PeiXun/IO/22.jpeg");
FileOutputStream fos = new FileOutputStream("d:/school/PeiXun/IO/222.jpeg");
byte[] b = new byte[10];
byte key = 88;
int length = fis.read(b);
while(length != -1) {
//解密
for(int i = b.length ; i < 0; i--) {
b[i] = (byte) (b[i] ^ key);
}
fos.write(b, 0, length);
length = fis.read(b);
}
fos.flush();
fis.close();
fos.close();
}
}
3.将一个文件内的内容 读取后分开存储
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class FenKaiCunChu {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File f= new File("d:/school/PeiXun/IO/b.txt");
FileInputStream fis = new FileInputStream(f);
FileOutputStream fos = new FileOutputStream("d:/school/PeiXun/IO/c.txt");
FileOutputStream foss = new FileOutputStream("d:/school/PeiXun/IO/d.txt");
byte[] b = new byte[100];
long x = f.length()/2;
int length = fis.read(b);
int conunt = 0;
while(length != -1) {
conunt += length;
if(conunt <= x) {
fos.write(b,0,length);
}else {
foss.write(b,0,length);
}
length = fis.read(b);
}
foss.flush();
fos.flush();
fis.close();
fos.close();
foss.close();
//属性文件类的操作
Properties p = new Properties();
p.load(new FileReader("d:/school/PeiXun/IO/db.properties"));
System.out.println(p.get("url"));
//数据流 定义的类型必须和读取的类型一致 int---->readInt()
DataInputStream dis = new DataInputStream(new FileInputStream("d:/school/PeiXun/IO/c.txt"));
int s = dis.readInt();
System.out.println(s);
//对象流 对象必须实现序列化接口
}
}
4.复制操作
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class TestIO {
public static void main(String[] args) throws IOException {
//输入
/*FileWriter fw = new FileWriter("d:/school/PeiXun/IO/a.txt",true);//加true 代表追加,不覆盖掉之前的内容;
fw.write("已经开始写入");
fw.flush();
fw.close();
//读取
FileReader fr = new FileReader("d:/school/PeiXun/IO/a.txt");
char[] arr = new char[2];
int length = fr.read(arr);
//读取结束后length的值会为-1
while(length != -1) {
String str = new String(arr,0,length);
System.out.print(str);
length = fr.read(arr);
}
fr.close();*/
//复制操作
/* File f = new File("d:/school/PeiXun/IO/b.txt");
f.createNewFile();
FileReader fr = new FileReader("d:/school/PeiXun/IO/a.txt");
FileWriter fw = new FileWriter("d:/school/PeiXun/IO/b.txt");
char[] arr = new char[10];
int length = fr.read(arr);
while(length != -1) {
fw.write(arr, 0, length);
length = fr.read(arr);
}
fw.flush();
fw.close();
fr.close();*/
5.从控制台输入的内容存储到文件中
Scanner sc = new Scanner(System.in);
System.out.println("请输入您要输入的内容:");
String input = sc.nextLine();
FileWriter fw = new FileWriter("d:/school/PeiXun/IO/b.txt",true);
fw.write(input);
FileReader fr = new FileReader("d:/school/PeiXun/IO/b.txt");
char[] arr = new char[20];
int length = fr.read(arr);
while(length != -1) {
String str = new String(arr,0,length) ;
System.out.println(str);
length = fr.read(arr);
}
fw.flush();
fw.close();
fr.close();
}
}