您的位置:首页 > 职场人生

黑马程序员——I/O

2015-10-15 17:42 661 查看
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


I/O

在java编程中,I/O系统主要负责文件的读写。一般在运行程序时,java I/O程序将源磁盘,文件或网络上的数据通过输入流类的相应方法读入内存,然后通过输出流类的相应方法将处理完的数据写回目标文件,磁盘或网络资源指定的位置。

主要分为2大类:
1.字符流
两个抽象流类:
Writer  
Reader
其中FileWriter和FileReader可实现文件的读写操作

BufferedWriter和BufferedReader能够提供缓冲区功能,用以提高效率

2.字节流
两个抽象流类
InputStream   
OutputStream

其中FileInputStream和FileOutputStream实现文件读写操作

BufferedInputStream和BufferedOutputStream提供缓冲区功能

1
字符流

1.1 字符流的读取

public class Demo2 {
public static void main(String[] args) {

FileReader r = null;
try {
r = new FileReader(
"C:\\Users\\Administrator\\Workspaces\\MyEclipse Professional 2014\\Re\\src\\demo.txt");

while (true) {
int temp = r.read();
if (temp == -1) {
break;
}
System.out.print((char) temp);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (r != null) {
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}


结果:



1.2  字符流的写入

public class Demo {
public static void main(String[] args ) {

//创建要操作的文件路径和名称
//其中,File.separator表示系统相关的分隔符,
b503
Linux下为:/  Windows下为:\\
String path = File.separator + "home" + File.separator + "siu" +
File.separator + "work" + File.separator + "demo.txt";

//由于IO操作会抛出异常,因此在try语句块的外部定义FileWriter的引用
FileWriter w = null;
try {
//以path为路径创建一个新的FileWriter对象
//如果需要追加数据,而不是覆盖,则使用FileWriter(path,true)构造方法
w = new FileWriter(path);

//将字符串写入到流中,\r\n表示换行想有好的
w.write("Nerxious is a good boy\r\n");
//如果想马上看到写入效果,则需要调用w.flush()方法
w.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
//如果前面发生异常,那么是无法产生w对象的
//因此要做出判断,以免发生空指针异常
if(w != null) {
try {
//关闭流资源,需要再次捕捉异常
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
结果:



1.3 文本文件的复制

package com.zh;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Demo {
public static void main(String[] args ) {

String doc = File.separator + "home" + File.separator + "siu" +
File.separator + "work" + File.separator + "demo.txt";

String copy = File.separator + "home" + File.separator + "siu" +
File.separator + "life" + File.separator + "lrc.txt";

FileReader r = null;
FileWriter w = null;
try {
r = new FileReader("C:\\Users\\Administrator\\Workspaces\\MyEclipse Professional 2014\\Re\\src\\demo.txt");
w = new FileWriter("C:\\Users\\Administrator\\Workspaces\\MyEclipse Professional 2014\\Re\\src\\com\\zh\\lrc.txt");

//单个字符写入
int temp = 0;
while((temp = r.read()) != -1) {
w.write(temp);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
//分别判断是否空指针引用,然后关闭流
if(r != null) {
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(w != null) {
try {
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

结果:



复制成功

2 字节流

2.1 字节流的读取

package com.zh;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Demo {
public static void main(String[] args) {

String path = File.separator + "home" + File.separator + "siu"
+ File.separator + "work" + File.separator + "demo.txt";

FileInputStream i = null;

try {
i = new FileInputStream(
"C:\\Users\\Administrator\\Workspaces\\MyEclipse Professional 2014\\Re\\src\\demo.txt");

// 单个字符读取
// 需要注意的是,此处我用英文文本测试效果良好
// 但中文就悲剧了,不过下面两个方法效果良好
int ch = 0;
while ((ch = i.read()) != -1) {
System.out.print((char) ch);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (i != null) {
try {
i.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
}

结果:



读取成功!!

2.2 字节流的写入

package com.zh;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo {
public static void main(String[] args ) {

String path = File.separator + "home" + File.separator + "siu" +
File.separator + "work" + File.separator + "demo.txt";

FileOutputStream o = null;

try {
o = new FileOutputStream("C:\\Users\\Administrator\\Workspaces\\MyEclipse Professional 2014\\Re\\src\\demo.txt");
String str = "zhangwen is a good boy,and is a good student!!!\r\n";
byte[] buf = str.getBytes();
//也可以直接使用o.write("String".getBytes());
//因为字符串就是一个对象,能直接调用方法
o.write(buf);

} catch (IOException e) {
e.printStackTrace();
} finally {
if(o != null) {
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
}
结果:



写入成功!!


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: