您的位置:首页 > 编程语言 > Java开发

JAVA IO(二)

2016-04-16 18:43 417 查看

一,IO流的分类

重点 四个抽象类

分类 ————字节 ———————字符 ——–方法

输入 ————InputStream ————Reader —–read()

输出 ————OutputStream ——— Writer——write()

1.字节读

InputStream实现类FileInputStream的read方法一个字节一个字节的读文件。

public class TestInputStream {
public static void main(String[] args) {
int b;//用来接收 读入的一个字节  那一滴水
FileInputStream fis = null;
//1.建立通道
try {
fis = new FileInputStream("c:\\IO\\HelloIO.txt");
} catch (FileNotFoundException e) {
System.out.println("文件没有找到");
e.printStackTrace();
System.exit(-1);
}
//2.利用read循环读
//read() 读一个字节  返回值 int  把读入的1个字节 存到int类型的低八位
try {
while((b=fis.read())!=-1){
System.out.print((char)b);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
//3.关闭通道
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
System.out.println("通道关闭失败");
e.printStackTrace();
}
}
}
}
}


注:了解一下JDK7.0的新特性(可以自动释放资源的try)

字节读每次读一个字节英文没什么问题,但读中文一个字占两个字节导致每次读一半产生乱码,解决方法是一次性把文件全部读完

byte[] bytes=new byte[fis.available()];
fis.read(bytes);
String str=new String(bytes,"utf-8");


2.字符读

FileReader的read()方法每次读一个字符 英文是一个字符 占1个字节 中文是一个字符 占2个字节 。文字占多少个字节 由编码方式决定 UTF-8一个字符 占3个字节。

public class TestFileReader {
public static void main(String[] args) {
FileReader fr = null;
try {
// 1,建立通道
fr = new FileReader("IO/aaa.txt");
int b;
// 2,循环读文件
while ((b = fr.read()) != -1) {
System.out.println((char) b);
}
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 3,关闭文件
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


注:目标文件是gbk时,程序读进来的 gbk字节 解码也要使用gbk解码编码方式必须保持一致

3,字节写

FileOutputStream需要注意的是 写文件的时候 即使文件不存在 也不会报错 会先帮你创建出这个文件来。

FileNotFoundException 不是文件没有找到 ,而是文件夹没有找到或者没有写入权限。

利用write写使用本地系统默认字符编码 编码的。如果中文操作系统 gbk编码

public class TestFileOutputStream {
public static void main(String[] args) {
//一.建立通道
String s = "你好世界";
FileOutputStream fow = null;
//第二个参数true追加
try {
fow = new FileOutputStream("HelloIO.txt",true);
} catch (FileNotFoundException e) {
System.out.println("文件夹没有找到");
e.printStackTrace();
System.exit(-1);
}
try {
//二.
//调了控制台的编码  相当于  -Dfile.encoding=utf-8
byte[] byteArr = s.getBytes("gbk");
fow.write(byteArr);
} catch (IOException e) {
System.out.println("文件写入失败!");
e.printStackTrace();
}finally{
//三.关闭
if(fow!=null){
try {
fow.close();
} catch (IOException e) {
System.out.println("输出流关闭失败!");
e.printStackTrace();
}
}
}
}
}


4.字符写

FileWriter加true追加

public class TestFileWriter {
public static void main(String[] args) {
//char c ='我';
//char[] charArr = new char[]{'你','好','啊','!'};
String s = "我喜欢学java";
//1.建立通道
FileWriter fw = null;
try {
//不加true会覆盖
fw = new FileWriter("c:\\io\\testFileWriter.txt");//加true追加
} catch (IOException e) {
e.printStackTrace();
System.out.println("目录未找到");
System.exit(-1);
}
//2.利用read循环读  利用write写
try {
fw.write(s);
} catch (IOException e) {
System.out.println("写入失败");
e.printStackTrace();
}finally{
//3.关闭
if(fw!=null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: