IO
1 流
流可以认为是一条通道,它可以将数据从源端传送到目的地。
例如将程序中的某些数据写入文件,或将文件中的某些数据读入程序。
Java中数据的操作是以“流”的方式进行。
Java中的“流”是一个具体的Java对象,该对象提供一些方法进行数据的传递。
Java中用来创建流对象的类有很多,这些类创建出来的对象所应用的场景是不同的。
Java中的流都声明再java.io 这个包中。
流的分类:
- 按照输入输出分类:
- 输入流
- 输出流
- 按照传输数据单元分类:
- 字节流
- 字符流
- 按照功能分类:
- 节点流
- 处理流
流的类都是从一下四个抽象类派生的
2 文件流
程序读写文件的流,功能属于节点流。
文件字节流 | 文件字符流 | |
---|---|---|
文件输入流 | FileInputStream | FileReader |
文件输出流 | FileOutputStream | FileWriter |
2.1 FileOutputStream
public class File_io_v1 {
public static void main(String[] args) throws IOException {
FileOutputStream fo = new FileOutputStream("D:\\Java\\eclipsefile\\Java-study\\IO_test\\out.txt" );
String str = "hello world!";
byte[] bstr = str.getBytes();
fo.write(bstr);
fo.flush();
fo.close();
}
}
sout(fo)
的输出是java.io.FileOutputStream@16b98e56
public class File_io_v2 {
public static void main(String[] args) {
FileOutputStream fo = null;
try{
fo = new FileOutputStream("D:\\Java\\eclipsefile\\Java-study\\IO_test\\out2.txt");
String str = "skdjhjkshdf";
byte[] bstr = str.getBytes();
fo.write(bstr);
} catch (FileNotFoundException e1) {
System.out.println("文件无法打开");
e1.printStackTrace();
} catch (IOException e2) {
System.out.println("IO错误");
e2.printStackTrace();
}catch(Exception e3){
System.out.println("危险危险危险!");
e3.printStackTrace();
}finally {
try{
if(fo != null){
fo.close();
}
} catch (IOException e) {
System.out.println("危险危险危险!");
e.printStackTrace();
}
}
}
}
2.2 FileReader
public class File_io_v3 {
public static void main(String[] args) throws IOException {
FileReader fi = new FileReader("D:\\Java\\eclipsefile\\Java-study\\IO_test\\out2.txt");
int r = fi.read();
String str = "";
while(r != -1){
str = str + (char) r;
System.out.println((char) r);
r = fi.read();
}
System.out.println(str);
fi.close();
}
}
public class File_io_v4 {
public static void main(String[] args) {
FileReader fr1 = null;
FileReader fr2 = null;
FileWriter fw1 = null;
FileWriter fw2 = null;
try {
fr1 = new FileReader("D:\\Java\\eclipsefile\\Java-study\\IO_test\\in.txt");
fr2 = new FileReader("D:\\Java\\eclipsefile\\Java-study\\IO_test\\in2.txt");
fw1 = new FileWriter("D:\\Java\\eclipsefile\\Java-study\\IO_test\\out.txt");
fw2 = new FileWriter("D:\\Java\\eclipsefile\\Java-study\\IO_test\\out2.txt");
int r1;
int r2;
while ((r1 = fr1.read()) != -1){
System.out.println((char) r1);
r1 = fr1.read();
}
fw1.write("衬布不易");
fw1.flush();
while((r2 = fr2.read()) != -1){
fw2.write((char) r2);
}
fw2.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try{
if(fr1 != null)fr1.close();
if(fr2 != null)fr2.close();
if(fw1 != null)fw1.close();
if(fw2 != null)fw2.close();
}catch(Exception e){e.printStackTrace();}
}
}
}
3 缓冲流
是对节点流进行包装的流。功能上属于处理流,提供了增强节点流功能的方法。
最常用是提供了缓冲字符流的读写一行的功能
缓冲字节流 | 缓冲字符流 | |
---|---|---|
缓冲输入流 | BufferedInputStream | BufferedReader |
缓冲输出流 | BufferedOutputStream | BufferedWriter |
public class Buffer_io_v1 {
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader("D:\\Java\\eclipsefile\\Java-study\\IO_test\\in2.txt");
fw = new FileWriter("D:\\Java\\eclipsefile\\Java-study\\IO_test\\out2.txt");
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
String str = null;
while ((str = br.readLine()) != null){
System.out.println(str);
}
System.out.println("输入流结束");
bw.write("庆历四年春,");
bw.newLine();
bw.write("滕子京谪守巴陵郡。");
bw.newLine();
bw.write("越明年,政通人和,百废具兴。");
bw.newLine();
bw.write("乃重修岳阳楼,增其旧制,");
bw.newLine();
bw.write("刻唐贤今人诗赋于其上。");
bw.newLine();
bw.write("属予作文以记之");
bw.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
if (fr != null)fr.close();
if (fw != null)fw.close();
if (br != null)br.close();
if (bw != null)bw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
4 转换流
将字节流转换为字符流,属于是处理流
其中字节流可以操作所有类型的文件包括音频视频和图片,而字符流只能操作纯文本文件,如java文件,txt文件等。
输入转换流 | InputStreamReader |
输出转换流 | OutputStreamWriter |
public class Convert_io_v1 {
public static void main(String[] args) {
FileInputStream fi = null;
FileOutputStream fo = null;
InputStreamReader ir = null;
OutputStreamWriter ow = null;
try {
fi = new FileInputStream("D:\\Java\\eclipsefile\\Java-study\\IO_test\\in2.txt");
fo = new FileOutputStream("D:\\Java\\eclipsefile\\Java-study\\IO_test\\out2.txt");
ir = new InputStreamReader(fi);
ow = new OutputStreamWriter(fo);
int r;
while ((r = ir.read()) != -1){
System.out.println((char) r);
ow.write(r);
}
ow.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try{
if(fi != null)fi.close();
if(fo != null)fo.close();
if(ir != null)ir.close();
if(ow != null)ow.close();
}catch(Exception e){e.printStackTrace();}
}
}
}
5 数据流
可以对机器无关的原始数据类型(int,double)进行读写。不需要关心数据的字节数。属于处理流
输入数据流 | DataInputStream |
---|---|
输出数据流 | DataOutputStream |
public class Data_io_v1 {
public static void main(String[] args) {
FileOutputStream fo = null;
DataOutputStream doo = null;
try {
fo = new FileOutputStream("D:\\Java\\eclipsefile\\Java-study\\IO_test\\out.txt");
doo = new DataOutputStream(fo);
fo.write(65);
fo.write(65+256);
fo.flush();
doo.write(65);
doo.write(65+256);
doo.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try{
if(fo != null)fo.close();
if (doo != null)doo.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
6 打印流
打印形式的输出流,属于处理流
打印字节流 | 打印字符流 |
---|---|
PrintStream | PrintWrite |
public class Print_io_v1 {
public static void main(String[] args) {
FileOutputStream fo = null;
DataOutputStream doo = null;
PrintWriter pw = null;
try {
fo = new FileOutputStream("D:\\Java\\eclipsefile\\Java-study\\IO_test\\out.txt");
doo = new DataOutputStream(fo);
pw = new PrintWriter(fo);
fo.write(65);
fo.flush();
doo.write(65);
doo.flush();
pw.write(65);
pw.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
if (fo != null)fo.close();
if (doo != null)doo.close();
if(pw != null)pw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
public class Print_io_v2 {
public static void main(String[] args) {
FileOutputStream fo = null;
PrintWriter pw = null;
try {
fo = new FileOutputStream("D:\\Java\\eclipsefile\\Java-study\\IO_test\\out.txt");
pw = new PrintWriter(fo);
pw.println("hello world");
pw.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
if (fo != null)fo.close();
if(pw != null)pw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
7 对象流
可以进行对象的写入和读出。进行读写的对象要是可序列化的。属于处理流。
如果创建对象的类是实现了Serializable接口的类,则对象是可序列化的。
对象输出流 | 对象输入流 |
---|---|
ObjectInputStream | ObjectInputStream |
public class Object_io_v1 {
public static void main(String [] args){
FileOutputStream fo = null;
FileInputStream fi = null;
ObjectOutputStream oo = null;
ObjectInputStream oi = null;
try{
fo = new FileOutputStream("D:\\Java\\eclipsefile\\Java-study\\IO_test\\at.txt");
fi = new FileInputStream("D:\\Java\\eclipsefile\\Java-study\\IO_test\\at.txt");
oo = new ObjectOutputStream(fo);
oi = new ObjectInputStream(fi);
oo.writeObject(new Rect(3,5));
oo.flush();
Rect r = (Rect)oi.readObject();
System.out.println(r.area());
}catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(oo != null)oo.close();
if(oi != null)oi.close();
if(fo != null)fo.close();
if(fi != null)fi.close();
}catch(Exception e){e.printStackTrace();}
}
}
}
class Rect implements Serializable{
public int width;
public int length;
public Rect(int w,int l){
width = w;
length = l;
}
public int area(){
return width * length;
}
}
8 控制台的输入输出
标准输入输出流
- System.in :标准输入流
- System.out:标准输出流
- System.err:标准错误输出流
public class System_io_v1 {
public static void main(String[] args) throws IOException {
int a;
a = System.in.read();
System.out.println(a);
}
}
c Rect(int w,int l){
width = w;
length = l;
}
public int area(){
return width * length;
}
}
## 8 控制台的输入输出
标准输入输出流
* System.in :标准输入流
* System.out:标准输出流
* System.err:标准错误输出流
```java
public class System_io_v1 {
public static void main(String[] args) throws IOException {
int a;
a = System.in.read();
System.out.println(a);
}
}