一、文件字节输入流和字符输入流
四步骤:1.创建源 2.选择流 3.操作 4.释放
输入流在创建源时:文件必须存在。
字节输入流:InputStream
package cn.rou.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* 文件字节输入流 理解操作步骤 (批量读取)
* 1.创建源
* 2.选择流
* 3.操作
* 4.释放资源
*
* @author : RouBao
*
*
*/
public class IOtest03 {
public static void main(String[] args) {
// 创建源
File file = new File("abc.txt");// 文件必须存在,因为要读取内容
// 选择流
InputStream is = null;
try {
// 选择流
is = new FileInputStream(file);
byte[] flush = new byte[1024]; // 缓冲容器(1024=1K)
int len; // 接受长度
// 操作(分段读取),读取到最后没有了返回-1
while ((len = is.read(flush)) != -1) {
// 字节数组-->字符串(解码)
String str = new String(flush, 0, len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null)
try {
if (is != null) {
// 释放
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
二、文件字节输出流
输出流在创建源时文件可以不存在(自动创建),操作最后需加上刷新。
字节输出流:OutputStream
package cn.rou.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* 文件字节输出流
* 1.创建源
* 2.选择流
* 3.操作
* 4.释放
*
*
* @author : RouBao
*
*
*/
public class IOtest04 {
public static void main(String[] args) {
// 创建源
File file = new File("dest.txt");// 文件可以本来不存在,会自动创建
OutputStream os = null;
try {
// 选择流
// 有标识时,true标识在后面重复写,false表示重新写
os = new FileOutputStream(file, false);
// 操作
// 字符串-->字节数组(编码)
String str = "change";
byte[] by = str.getBytes();
os.write(by, 0, by.length);
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放
try {
if (os != null) {
os.close();
}
} catch (Exception e) {
}
}
}
}
三、文件字符输入流
字符输入流与字节输入流大同小异,无非是选择的流不同。
选择缓冲容器时因为是字符流,所以应选择字符数组。
字符输入流:Reader
package cn.rou.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
/**
* 文件字符输入流
* 1.创建源
* 2.选择流
* 3.操作
* 4.释放资源
*
* @author : RouBao
*
*
*/
public class IOtest05 {
public static void main(String[] args) {
// 创建源
File file = new File("abc.txt");// 文件必须存在,因为要读取内容
// 选择流
Reader reader = null;
try {
// 选择流
reader = new FileReader(file);
char[] flush = new char[1024]; // 缓冲容器(1024=1K)
int len; // 接受长度
// 操作(分段读取),读取到最后没有了返回-1
while ((len = reader.read(flush)) != -1) {
// 字符数组-->字符串
String str = new String(flush, 0, len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null)
try {
if (reader != null) {
// 释放
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、文件字符输出流
字符输出流:Writer
package cn.rou.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* 文件字符输出流 不能操作图片
* 1.创建源
* 2.选择流
* 3.操作
* 4.释放
*
*
* @author : RouBao
*
*
*/
public class IOtest06 {
public static void main(String[] args) {
// 创建源
File file = new File("dest.txt");// 文件可以本来不存在,会自动创建
Writer writer = null;
try {
// 选择流
// 有标识时,true标识在后面重复写,false表示重新写
writer = new FileWriter(file);
// 操作
// 写法一
// String str = "give me a beer\n给我一杯忘情水\n";
// char[] by = str.toCharArray();
// writer.write(by, 0, by.length);
// 写法二
// String str = "give me a beer\n给我一杯忘情水\n";
// writer.write(str);
// 写法三
writer.append("give me a beer\n").append("给我一杯忘情水\n");
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放
try {
if (writer != null) {
writer.close();
}
} catch (Exception e) {
}
}
}
}