一、文件
1.文件流:
2.常用的文件操作
a.创建文件:
相关的构造器和方法:
b.获取文件信息 :
c.目录操作:
mkdir 创建一级目录,mkdirs 创建多级目录,delete 删除空目录或文件
package File;
import java.io.File;
import java.io.IOException;
public class FileInformation {
public static void main(String[] args) {
FileInformation d = new FileInformation();
d.info();
//d.m1();
// d.m2();
d.m3();
}
//获取文件信息
public void info(){
File file = new File("e:\\news.txt");
try {
file.createNewFile();
} catch (IOException e) {
System.out.println(e);
}
//1.文件名字
System.out.println("文件名字="+file.getName());
}
public void m1(){
String fp = "e:\\news.txt";
File f = new File(fp);
if(f.exists()){
if(f.delete()){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}else {
System.out.println("文件不存在");
}
}
//在Java中目录也是一种文件!!
public void m2(){
String mulu = "e:\\demo02";
File f = new File(mulu);
if(f.exists()){
if(f.delete()){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}else {
System.out.println("目录(文件夹)不存在");
}
}
public void m3(){
String muluPath = "e:\\demo03\\a\\b\\c";
File f = new File(muluPath);
if(f.exists()){
System.out.println("该目录存在");
}else {
//如果该目录不存在的话 用mkdirs创建目录
//f.mkdirs() 返回布尔值
if(f.mkdirs()){
System.out.println("创建成功");
}else {
System.out.println("创建失败");
}
}
}
}
二、IO流
Java中,对数据的输入和输出以“流”的方式进行,java.io包下提供了各种流的类和接口,用以获取不同种类的数据,并通过方法输入或者输出
将不同类型的输入和输出都抽象为流
按照流的方向,可以分为输入流和输出流
按照流的角色不同:结点流,处理流/包装流
根据操作数据单位可以分为:字节流( 一个字节八位 8bit)和字符流(文本文件最好用字符流处理)(一个字符对应几个字节按照文件编码来看)
在Java中一个字符是2个字节,Unicode编码
1.InputStream类
InputStream抽象类是所有类字节输入流的超类
read() 方法 逐字节地以二进制的原始方式读取数据
package File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStream_ {
public static void main(String[] args) {
FileInputStream_ a = new FileInputStream_();
a.readFile1();
}
public void readFile(){
String fp = "e:\\hello.txt";
//字节输入流 文件-->程序
int readDate = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(fp);
//从该输入流读取一个字节的数据,如果没有输入课用,该方法将被阻止
//如果返回-1,表示读取完毕
while ( ( readDate= fileInputStream.read())!=-1){
System.out.println((char)readDate);
}
} catch (Exception e) {
System.out.println(e);
}finally {
//关闭文件 释放资源
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public void readFile1(){
String fp = "e:\\hello.txt";
//字节输入流 文件-->程序
int readLen = 0;
//字节数组 效率更高
byte[] buf = new byte[8];//一次读取8个字节
//读的次数更少了 第一次读hello,wo 第二次读rld
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(fp);
//从该输入流读最多b.length字节的数据到字节数组
//如果返回-1,表示读取完毕
//fileInputStream.read(buf) 如果读取正常,返回实际读取的字节数
while ( ( readLen= fileInputStream.read(buf))!=-1){
System.out.println(new String(buf,0, readLen));//转成字符串显示
}//用这个字节数组构建字符串
} catch (Exception e) {
System.out.println(e);
}finally {
//关闭文件 释放资源
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
2.OutputStream 类
write() 将字节写入流中
package File;
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputStream {
public static void main(String[] args) {
}
//将数据写入文件中 如果文件不存在 就创建该文件
@Test
public void writeFile(){
FileOutputStream fileOutputStream =null;
String fp = "e:\\a.txt";
try {
//得到fileOutputStream对象
fileOutputStream = new FileOutputStream(fp);
fileOutputStream.write('w');//写入一个字节
//写入字符串
String str = "Hello,World";
//str.getBytes() 可以把字符串转化为字符数组
fileOutputStream.write(str.getBytes());
//write(byte[],int off,int len)将len字节从位于偏移量off的指定字节数组写入次文件输入流
fileOutputStream.write(str.getBytes(),0,str.length());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
flush() 刷新缓存,实际写入到文件、网络
close() 关闭字节流
3.Reader类
与InputStream类相似,都是输入流,但是Reader类读取的是char,而不是字节
相关方法:
read()方法
4.Writer类
与OutputStream 类相似,都是输出流,但是Writer类写入的的是char,而不是字节
相关方法:
三、节点流和处理流
处理流:是对一个已经存在的流的连接和封装
常用的节点流:
常用的处理流:
BufferedReader类中,有属性Reader,可以封装一个节点流,该节点流可以是任意的,只要是Reader子类 其他也一样
1.节点流与处理流的联系
节点流是底层流/低级流,直接跟数据源相接处理流(包装流)包装节点流,既可以消除不同节点流的实现差异,也可以提供更方便的方法来完成输入输出
「源码理解]处理流(也叫包装流)对节点流进行包装,使用了修饰器设计模式,不会直接与数据源相连[模拟修饰器设计模式]
处理流的功能主要体现在以下两个方面:
性能的提高:主要以增加缓冲的方式来提高输入输出的效率。操作的便捷:处理流可能提供了一系列便捷的方法来一次输入输出大批量的数据,使用更加灵活方便
2.BufferedReader 和BufferdeWriter
属于字符流,按照字符读取数据 关闭时,只需要关闭外层流
字符流操作文本文件 不可以操作二进制文件
package File;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReader_ {
public static void main(String[] args) throws IOException {
String fp = "E:\\a.txt";
BufferedReader bufferedReader = new BufferedReader(new FileReader(fp));
//读取
String line;//按行读取 .readLine(); 当返回null时,表示文件读取完毕
line =bufferedReader.readLine();
while ((line = bufferedReader.readLine())!=null){
System.out.println(line);
}
//关闭流 只需要关闭BufferedReader
//因为底层会自动关闭节点流
bufferedReader.close();
}
}
class BufferedWriter_ {
public static void main(String[] args) throws IOException {
String fp = "e:\\b.txt";
BufferedWriter bufferedWriter;
//表示以覆盖的方式写入
bufferedWriter = new BufferedWriter(new FileWriter(fp));
//如果是追加写入的话:
// bufferedWriter = new BufferedWriter(new FileWriter(fp,true));
bufferedWriter.write("hello!");
//换行
bufferedWriter.newLine();
bufferedWriter.write("hello!");
bufferedWriter.write("hello!");
//因为底层会自动关闭节点流
bufferedWriter.close();
}
}
3.BufferedInputStream 和BufferdeOutputStream
字节流操作二进制文件,也可以操作文本文件
package File;
import java.io.*;
public class BufferedInputStream_ {
public static void main(String[] args) throws IOException {
String srcFilePath = "e:\\11.jpg";
String destFilePath = "e:\\22.jpg";
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream (new FileInputStream(srcFilePath));
bos = new BufferedOutputStream( new FileOutputStream (destFilePath));
byte[] buff = new byte[1024];
int readLine = 0;
while ((readLine = bis.read(buff))!=-1){
bos.write(buff,0,readLine);
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if(bis !=null){
bis.close();
}
if(bos !=null){
bos.close();
}
}
}
}
4.转换流 InputStreamReader和OuputStreamWriter
package File;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class InputStreamReader_ {
public static void main(String[] args) throws Exception {
String p = "e:\\a.txt";
//将FileInputStream 转换成InputStreamreader
InputStreamReader isr = new InputStreamReader(new FileInputStream(p),"gbk");
//将FileInputStream 转换为BufferedReader
BufferedReader bufferedReader = new BufferedReader(isr);
//读取
String s = bufferedReader.readLine();
System.out.println(s);
//
bufferedReader.close();
}
}