今天整理了一下java中IO类中的一些基本的知识点和一些类和方法的使用。
其中也借鉴了别人的博客:
Java中IO操作通常指的是使用java 对数据进行读写操作,机制就是通过数据流进行读写。
何谓数据流呢,数据流就是表示字节或者字符的流动序列
简而言之:数据流就是包括输入流和输出流的有序的有起点和有终点 的字节的数据序列
当程序需要读取数据时就要创建一个通向数据源的连接,这个数据源可以是 文件,也可以是内存,也可以是网络连接,当程序需要写入数据的,也需要创建一个通向目的地i的连接。
流序列中的数据既可以是未经加工的原始二进制数据,也可以是经过一定编码的符合某种格式的特定数据,Java中的流分为两种
1.字节流: 数据流中最小的数据单元是字节
2.字符流: 数据流中最小的数据单元是字符, java中的字符是Unicode编码,一个字符占用连个字节
基于字节的输入输出流
比较常用的字节的输入流有:
1. FileInputStream(文件输入流)
2. ByteArrayInputStream (字节数组输入流)
3. BufferedInputStream (缓冲输入流)
以上三个字节输入流都是继承自 InputStream
下面是字节流的练习
public static void test01(){
String rootPath = System.getProperty("user.dir");
File file1 = new File(rootPath+File.separator+"template"+File.separator+"json"+File.separator+"1.json");
File file2 = new File(rootPath+File.separator+"template"+File.separator+"json"+File.separator+"2.json");
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(file1);
boolean flag = file2.exists();
out = new FileOutputStream(file2);
byte[] buff = new byte[2048];
while(in.read(buff)!=-1){
out.write(buff);
}
System.out.println("写入完毕");
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void test02(){
ByteArrayInputStream in = null;
ByteArrayOutputStream out = null;
String str = "我爱你 nsi";
byte[] buff = new byte[20480];
try {
in = new ByteArrayInputStream(str.getBytes());
out = new ByteArrayOutputStream();
while(in.read(buff)!=-1){
out.write(buff);
}
System.out.println("写入完毕了");
System.out.println(out.toString());
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void test03(){
String rootPath = System.getProperty("user.dir");
File file1 = new File(rootPath+File.separator+"template"+File.separator+"json"+File.separator+"1.json");
File file2 = new File(rootPath+File.separator+"template"+File.separator+"json"+File.separator+"2.json");
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(file1));
out = new BufferedOutputStream(new FileOutputStream(file2));
byte[] buff = new byte[1024];
while(in.read(buff)!=-1){
out.write(buff);
}
System.out.println("写入完毕");
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
下面是字符流的练习
/**
* 基于字符的输入输出流
* 输入流:
* 1. FileReader
* 2. InputStreamReader
* 3. StringReader
* 输出流
* 1. FileWriter
* 2. OutputStreamWriter
* 3. StringWriter
*/
public static void test04(){
String rootPath = System.getProperty("user.dir");
File file1 = new File(rootPath+File.separator+"template"+File.separator+"json"+File.separator+"1.json");
File file2 = new File(rootPath+File.separator+"template"+File.separator+"json"+File.separator+"2.json");
FileReader reader = null;
FileWriter writer = null;
char[] chaff = new char[2048];
try {
reader = new FileReader(file1);
writer = new FileWriter(file2);
while(reader.read(chaff)!=-1){
writer.write(chaff);
}
System.out.println("写入完毕");
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void test05(){
String rootPath = System.getProperty("user.dir");
File file1 = new File(rootPath+File.separator+"template"+File.separator+"json"+File.separator+"1.json");
File file2 = new File(rootPath+File.separator+"template"+File.separator+"json"+File.separator+"3.json");
InputStreamReader isr = null;
OutputStreamWriter osw = null;
char[] chaff = new char[2048];
try {
//多种实现
// isr = new InputStreamReader(new FileInputStream(file1));
// isr = new InputStreamReader(new BufferedInputStream(new FileInputStream(file1)));
isr = new InputStreamReader(new ByteArrayInputStream(new String("三块经销商njkk214 &&%%NJKBAS 寸你看电视剧").getBytes()));
// osw = new OutputStreamWriter(new FileOutputStream(file2));
osw = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file2)));
// osw = new OutputStreamWriter(new ByteArrayOutputStream());
if(isr.read(chaff)!=-1){
osw.write(chaff);
}
System.out.println("写入成功");
osw.flush();
System.out.println(osw.toString());
isr.close();
osw.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(isr!=null){
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(osw!=null){
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void test06(){
StringReader reader = null;
StringWriter writer = null;
try {
reader = new StringReader("我爱你中华 Ilove you !!!");
writer = new StringWriter();
char[] chaff = new char[2048];
if(reader.read(chaff)!=-1){
writer.write(chaff);
}
System.out.println("写入完毕");
System.out.println(writer.toString());
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(reader!=null){
reader.close();
}
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
总的来说学习IO类,就要学习,InputStream,OutputStream, Reader, Writer 这几个抽象类,以及继承他们的类
输入流和输出流都要注意创建一个有起始端和 终点端的连接
不积跬步,无以至千里
不积小流,无以成江海