I/O流读写文件详解

文件类

文件中路径的写法:

    1》String filePath = "C:\\AAA\\a.txt";  \\ 第一个‘\’表示转义字符

    2》filePath = "C:" + File.pathSeparator+ "AAA" + File.pathSeparator+ "a.txt"; \\使用路径分割符可以实现跨平台,推荐使用

    3》filePath = "C:/AAA/a.txt"; \\不需要转义字符

相对路径:

                String parentPath = "C:/AAA";
String childPath = "a.jpg";

File src = new File(parentPath,childPath);

绝对路径:

                String absolutePath = "C:/AAA/a.txt";

File src = new File(absolutePath);

特例:      String path = "a.txt";

File src = new File(path); //这种方式创建的文件在你目前的workspace\项目名\a.txt

常用的文件构建方法:

    1.File(File parent, String child)

          根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。

    2.File(String pathname)

          通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。

    3.File(String parent, String child)

          根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。 

创建目录:

    1.使用mkdir()必须确保父目录存在

    String str = "E:/IO_test/CCC/DDD";
    File src = new File(str);

    src.mkdir();

    2.使用mkdirs()方法,不需要确保父目录存在

    String str = "E:/IO_test/CCC/DDD";
    File src = new File(str);

    src.mkdirs();

I/O流

1、概念

流:流动 流向 从一端移动到另一端 源头与目的地

程度 与 文件|数组|网络连接|数据库, 以程序为中心

2.分类

流向:输入流和输出流

数据:字节流 二进制 可以处理一切文件,包括纯文本,doc,音频,视频等

           字符流 文本文件 只能处理纯文本

3.字节流

输入流:InputStream()此抽象类是表示字节输入流的所有类的超类。需要定义InputStream子类的应用程序必须总是提供返回下一个输入字节的方法

read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中

read(byte[] b, int off, int len)将输入流中最多len个数据字节读入byte数组

FileInputStream 从文件系统中的某个文件中获得输入字节,哪些文件可用取决于主机环境

/**
 * 读取一个目录中的文件,并打印在控制台
 * 1.建立连接对象
 * 2.选择流  文件输入流  InputStream  FileInputStream
 * 3.操作    不断读取  缓冲数组
 *   byte[] car = new byte[1024];  +read+读取大小  输出
 * 4.释放资源
 * @author Kermit
 *
 */
public class Demo01 {
public static void main(String[] args){
//1.建立连接 File对象
String path = "E:/IO_test/AAA/a.txt";
File src = new File(path);
System.out.println(src.exists());
//2.选择流
InputStream is = null;
try {
is = new FileInputStream(src);
//3.操作  不断读取 缓冲数组
byte[] car = new byte[10];
int len = 0;//接受实际读取文件大小
//循环读取
while(-1 != (len=is.read(car))){//每读取10个字节输出一次
//输出   字节数组转换成字符串
String str = new String(car,0,len);
//System.out .println("10Byte");
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件不存在!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取文件失败!");
}finally{
if(is != null){
try {//释放资源
is.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭文件流失败!");
}
}
}
}
}

输出流:OutputStream 此抽象类表示输出字节流的所有类的超类,输出流接收输出字节并将这些字节发送到某个接收器。

需要定义OutputStream子类的应用程序必须始终提供至少一种可写入一个输出字节的方法

write(byte[] b) 将b.length个字节从指定的byte数组写入此输出流

write(byte[] b, int off, int len)将指定byte数组中从偏移量off开始的len个字节写入此输出流

close()关闭此输出流并释放于此流有关的所有系统资源

flush()刷新此输出流并强制写出所有缓冲的输出字节

FileOutputStream 文件输出流是用于将数据写入File或FileDescriptor的输出流

/**
 * 写出文件
* 1.建立连接  File对象  目的地
 * 2.选择流  文件输出流  OutputStream  FileOutputStream
 * 3.操作    write()  +   flush
 * 4.释放资源: 关闭
 * 
 * ======此程序功能=======
 * 将自己定义的字符串,装入字符数组,
 * 然后添加到指定的目标文件中
 * @author Kermit
 *
 */
public class Demo02 {
public static void main(String[] args) throws IOException{
File src = new File("E:/IO_test/AAA/export.txt");
OutputStream os = null;//字节流
try {
os = new FileOutputStream(src,true);//已追加的形式写出文件,将true改为false则追加功能变为覆盖功能
String str = "this is a cat\r\n";
byte[] data = str.getBytes();//字符串转字节数组

os.flush();//强制刷新出去
try {
os.write(data, 0, data.length);
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件写出失败!");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("没有找到文件!");
}finally{
//关闭输出资源
if(null != os){
os.close();
}
}
}

}


4.字符流

输入流:Reader 创建一个新的字符流reader,其重要的方法

read(char[] cbuf) 将字符读入数组

read(char[] cbuf, int off, int len) 将字符读入数组的某一部分

/**
 * 纯文本读取
 * 1.建立联系
 * 2.选择流
 * 3操作
 * 4.关闭
 * @author Kermit
 */
public class Demo01 {
public static void main(String[] args){
//1.建立联系 创建File 对象
File src = new File("E:/test/a.txt");
//2.选择流
Reader reader = null;
try {
reader = new FileReader(src);

//3.操作
char[] flush = new char[10];
int len;
try {
while(-1 != (len = reader.read(flush))){
String str = new String(flush);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取文本失败!");
}
} catch (FileNotFoundException e) {

e.printStackTrace();
System.out.println("源文件不存在!");
}finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("文本关闭失败!");
}
}
}
}

}

输出流:Writer 写入字符流的抽象类,子类必须实现的方法仅仅有write (char[], int off, int len)和close()。但是,多数子类重写此处定义的一些方法,以提高效率

write(char[] cbuf) 写入字符数组

write(String str,int off, int len) 写入字符串的某一部分

 /**
 * 纯文本的写出
 * 1.建立联系
 * 2.选择流
 * 3.操作
 * 4.关闭
 * @author Kermit
 *
 *times:2016-10-11 10:00:43
 */
public class Demo02 {
public static void main(String[] args){
//1.建立联系
File dest = new File("E:/test/practice.txt");
//2.选择流
Writer writer = null;
try {
writer = new FileWriter(dest,true);
//3.操作
String str = "追加--------\r\n锄禾日当午\r\n码农真辛苦\r\n一本小破书\r\n一看一上午\r\n";

writer.write(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("目标文件不存在!");
}finally{
if(writer != null){
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("关闭文本失败!");
}
}
}

}

}

总结:

字节读取操作

一、读取文件

 * 1.建立连接对象  源头

 * 2.选择流  文件输入流  InputStream  FileInputStream

 * 3.操作    不断读取  缓冲数组

 *   byte[] car = new byte[1024];  +read+读取大小  输出

 * 4.释放资源: 关闭

 

二、写出文件

* 1.建立连接  File对象  目的地

 * 2.选择流  文件输出  OutputStream  FileOutputStream

 * 3.操作    write()  +   flush

 * 4.释放资源: 关闭


字符读取操作

一、纯文本的读取

1.建立联系

2.选择流Reader FileReader

3.操作 char[] flush = new char[1024];

4.释放资源

二、纯文本的写出

1.建立联系

2.选择流 Wirter FileWirter

3.操作 char[] flush = new char[1024];

4.释放资源


注:原创不易,转载请标明出处:https://blog.csdn.net/Kermit_father





猜你喜欢

转载自blog.csdn.net/kermit_father/article/details/79766000