java中的IO流分为字节流和字符流:
字节流一般用来处理:图片、视频、PPT等多种文件,字符流只能用来处理纯文本文件如txt文件。
字节流可以处理所有文件,包括纯文本文件,但如果处理纯文本文件优先考虑字符流。
一、File类
java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建、查找和删除等操作。
File file = new File("d:\\aaa.txt");
try {
System.out.println(file.createNewFile()); // 创建文件
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("是否存在"+file.exists());
System.out.println(file.mkdir());//创建单个文件夹
System.out.println(file.getAbsolutePath()); // 绝对路径
System.out.println(file.getPath()); // 相对路径
File file2 = new File("abc");//位置在项目下(相对路径)
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());
File file3 = new File("d:\\aaa");
String[] nameList = file3.list(); // 获取该文件下的所有文件夹或者文件的名称
File[] fileList = file3.listFiles(); // 获取该文件下所有文件或文件夹,然后可以循环遍历操作文件
二、字节流
FileInputStream:字节输入流,简单来理解就是读文件,读磁盘上的文件到内存中。
FileOutputStream:字节输入流,写文件,输出一个文件,从内存到磁盘。
public class StreamDemo {
public static void main(String[] args) throws Exception{
File file = new File("ccc.txt");
FileOutputStream fos = new FileOutputStream(file);
//FileOutputStream fos2=new FileOutputStream("ddd.txt"); //两种创建方式
System.out.println(file.getAbsolutePath());
fos.write(97);
fos.write(98);
fos.write(99);// 输出abc
fos.close();
// 第二个参数表示是否续写,flase(不传是false)则清空原来的重写 a
FileOutputStream fos3= new FileOutputStream("ccc.txt",true);
byte[] bytes = "defg".getBytes();
fos3.write(bytes);
fos3.close();
FileInputStream fis = new FileInputStream("ccc.txt");
byte [] by= new byte[2];// 一次读两个字节
int len;
while ((len=fis.read(by))!=-1){
System.out.println(new String(by,0,len));// 每次读len个,就不会出现多读了
}
fis.close();// 流切记一定要关闭,一般都写在finally代码块中
}
}
复制图片案例:
public class CopyImage {
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream("D:\\workspace_Test\\timg.jpg");
FileOutputStream fos = new FileOutputStream("time2.jpg");
byte[] b = new byte[1024];// 一次读1024个字节
int len;
while ((len=fis.read(b))!=-1){
fos.write(b,0,len);// 复制到新的路径下
}
fis.close();
fos.close();// 复制完毕,关闭流
}
}
三、字符流
FileReader:字符输入流
FileWriter:字符输出流
这两个流的用法和上面的字节流基本类似,简单看下代码吧
public class WriterReaderDemo {
public static void main(String[] args) throws Exception{
// 读
FileReader fileReader = new FileReader("ddd.txt");// 不是纯文本文件输出的就是乱码
int b;
/* while ((b=fileReader.read())!=-1){
System.out.println((char)b);
}*/
int len;
char[] ch= new char[2]; // 一次读两个字符
while ((len=fileReader.read(ch))!=-1){
System.out.println(new String(ch,0,len));
}
fileReader.close();
// 写
FileWriter fileWriter = new FileWriter("ddd.txt",true);
fileWriter.write("沙师弟");
// 如果不用flush方法,则只有此fileWriter关闭时才可以将所有内容写入到文件内,
// 但是调用flush方法后,可以直接将之前write的内容写入到文件内。
fileWriter.flush();// 刷新缓冲区,流对象可以继续使用。
// 接着写
fileWriter.write("大师兄");
fileWriter.close();// 关闭流,此时“大师兄”也写到的文件内
}
}
四、缓冲流(常用)
缓冲流也叫高效流,是对上面所提到的几个流的增强。主要分为:
字节缓冲流:BufferedInputStream,BufferedOutputStream
字符缓冲流:BufferedReader,BufferedWriter
缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO 次数,从而提高读写的效率。
字节缓冲流:
public class BufferedTestDemo {
public static void main(String[] args) throws Exception{
// 构造方法需要穿FileInputStream参数
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("time2.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("time3.jpg"));
long start = System.currentTimeMillis();
/*int b;
while((b=bis.read())!=-1){
bos.write(b);
}*/
byte[] by = new byte[8*1024];
int len;
while ((len=bis.read(by))!=-1){
bos.write(by,0,len);
}
long end = System.currentTimeMillis();
System.out.println("用时:"+(end-start)+"毫秒");
bis.close();
bos.close();
}
}
字符缓冲流:
有两个特殊的方法可以记一下:
BufferedReader: public String readLine() : 读一行文字。
BufferedWriter: public void newLine() : 写一行行分隔符,由系统属性定义符号。
public class BufferedTestDemo2 {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new FileReader("fff.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("qqq.txt"));
long start = System.currentTimeMillis();
char[] ch = new char[2*1024];
int len;
while((len=br.read(ch))!=-1){
bw.write(ch);
}
long end = System.currentTimeMillis();
System.out.println("用时:"+(end-start)+"毫秒");
br.close();
bw.close();
}
}
总结:java中的IO分为2种流,字节流和字符流,每种流有对应的缓冲流,他们的方法都类似,read和write。(其实没啥难度,就几个API每种流都是来回用)