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(); } } } } }
相关文章推荐
- jdk卸载64位安装32位出现的问题
- Java基础复习(一)
- Eclipse and IDEA 快捷键对比
- Java中的继承
- maven项目在eclipse中debug时查看源码的办法
- Java中不引入第三个变量实现两个变量值的交换
- myeclipse修改web项目名称后,主界面访问不到
- Spring上传时报Invalid CSRF Token错误解决方案
- 20145107 《Java程序设计》第二次实验报告
- 简单Java 多表联查分页显示(初)
- 20145107 《Java程序设计》第七周学习总结
- Ubuntu 14.04 eclipse 提示框背景色更改
- Java_oop_继承
- 20145304 Java第七周学习报告
- spring + myBatis 常见错误:@Autowired注解失败
- Java-单机版的书店管理系统(练习设计模块和思想_系列 六 )
- Java-单机版的书店管理系统(练习设计模块和思想_系列 六 )
- java web 分页实现的补充
- JAVA第五次作业
- com.domain.*_$$_javassist_6 cannot be cast to javassist.util.proxy.Proxy问题