流:
什么是流:流是一种有序的数据序列,可以进行数据的传送!
根据操作类型可分为:输入流和输出流两种I/O(Input/Output,输入/输出)
根据数据传送类型分为:字节流/字符流
根据功能不同可以分为:节点流和处理流
Java语言定义了许多类专门负责各种方式的输入和输出,这些类都被放在java.io包中。其中所有输入流类都是抽象类
InputStream
(字节输入流)或抽象类
Reader
(字符输入流)的子类;而所有输出流都是抽象类
OutputStream
(字节输出流)或抽象类
Writer
(字符输出流)的子类。
下面是我的举例:
InputStream:
</pre><pre name="code" class="java"><span style="font-size:18px;">package com.sxt.stream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* @author HZ
* @category 字节输入流
*/
public class InputStreamDemoOne {
public static void main(String[] args) {
File file = new File("C:/new/mulu/a.txt");
InputStream input = null;
try {
input = new FileInputStream(file);
int i = 0;
while((i=input.read())!=-1){
System.out.print((char)i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
</span>
OutputStream:
package com.sxt.Stream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author HZ
* @category 字节输出流
*/
public class OutputStreamDemoOne {
public static void main(String[] args) {
File file = new File("C:" + File.separator + "new"
+ File.separator + "mulu" + File.separator + "b.txt");
OutputStream output = null;
try {
output = new FileOutputStream(file);
output.write(71);
output.write(65);
output.write(97);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
output.flush();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Reader:
package com.sxt.stream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/**
* @author HZ
* @category 字符输入流
*/
public class ReaderDemoOne extends SeparatorDemo {
public static void main(String[] args) {
File file = new File("C:" + File.separator + "new" + File.separator + "mulu" + File.separator + "b.txt");
Reader reader = null;
try {
reader = new FileReader(file);
int i;
while((i=reader.read())!=-1){
// i = reader.read();
System.out.print((char)i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Writer:
package com.sxt.stream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* @author HZ
* @category 字符输出流
*/
public class WriterDemoOne {
public static void main(String[] args) {
File file = new File("C:" + File.separator + "new"
+ File.separator + "mulu" + File.separator + "c.txt");
Writer writer = null;
try {
writer = new FileWriter(file);
writer.write("再不奋斗就老了!!!!!!!!!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}