java中的IO基础知识
来源:互联网 发布:淘宝皮草专卖 编辑:程序博客网 时间:2024/05/22 00:21
1.IO:输入与输出是相对计算机的内存而言的,Java 中的char类型是两个字节
2.File类:
File 类描述了文件的属性
File既可以描述目录,也可以描述文件
File类不能生成文件,可以生成目录
在windows中路径使用“\\”或者“/”来表示
3.Java中的流:
按照流的方向来划分:输入流,输出流
按照流的单位来划分:字节流,字符流
按照流的功能来划分:节点流,处理流
转换流:字节流与字符流之间互换
字节流:
InputStream/OutputStream抽象类
实现类:FileInputStream/FileOutputStream
包装类:DateInputStream/DateOutputStream
字符流:
Reader/Writer抽象类
4.编码方式
Unicode-->UTF-8 ,UTF-16全世界统一编码,国际化编程
中文2个字节,或者3个字节
Iso8859-1英文编码,所有的字符只占一个字节
Gbk -->gb2312 (gbk包含一些中国的繁体字)
Gbk一个汉字占两个字节
中文版的windows默认平台编码就是gbk
字节流的使用:
InputStream is ; OutputStream os ;try {is = new FileInputStream (new File ("d:/testDir/test.txt"));os = new FileOutputStream (new File ("d:/testDir/testW.txt"));//循环读取文件流中的数据,并打印到终端上,并判断是否的读到文件的末尾(返回值为-1)//读到文件的末尾,停止读取文件//把文件当中的数据读到内存之中/*int read =0;while ((read = is.read()) != -1) {System.out.print((char)read);os.write(read);}*///將管道中的数据刷入文件中byte buffer[] = new byte[1024];int length =0;//从文件中读取数据到buffer中,返回值是读取到的字节数//并把读到的长度赋值给length//判断length的值是否为-1,如果不是-1,则继续读,-1则停止读取数据//将读到的字节数组数据写入文件之中//把内存的数据写入了文件,这就是文件输出流while((length = is.read(buffer))!=-1){os.write(buffer, 0, length);}os.flush();is.close();os.close();} catch (Exception e) {e.printStackTrace();}
处理流的使用:
try {FileReader fr = new FileReader(new File("d:/testDir/test.txt"));FileWriter fw = new FileWriter(new File("d:/testDir/testbuf.txt"));BufferedReader br = new BufferedReader(fr);BufferedWriter bw = new BufferedWriter(fw);String temp = null;//把读到的字符串赋值给temp,并判断temp是否为空,为空则停止读取while((temp = br.readLine())!=null) {System.out.println(temp); bw.write(temp);//换行bw.newLine();}bw.flush();fr.close();fw.close();bw.close();br.close();} catch (Exception e) {}
编码的使用:
str.getBytes("gbk");//括号里面的是编码方式
System.out.println(baos.toString("gbk"));//解编码方式
public static void testByteArrayOutputStream() {String str = "欢迎你";try {byte b[] = str.getBytes("gbk");System.out.println(b.length); FileOutputStream fos = new FileOutputStream(new File("d:/testDir/testW.txt")); ByteArrayInputStream bais = new ByteArrayInputStream(b);ByteArrayOutputStream baos = new ByteArrayOutputStream() ;int read;while((read = bais.read())!=-1) {//System.out.print((char)read);baos.write(read); }System.out.println(baos.toString("gbk"));baos.writeTo(fos);baos.flush();baos.close();bais.close();} catch (Exception e) {e.printStackTrace();}}
对象流的使用:
public class TestObjectStream {public static void main(String[] args) {testObjectOutputStream();testObjectInputStream();}public static void testObjectOutputStream() {try {FileOutputStream fos = new FileOutputStream(new File("d:/person.bat"));ObjectOutputStream oos = new ObjectOutputStream(fos);Person person = new Person("张三" , 12); oos.writeObject(person);oos.flush();fos.flush();oos.close();fos.close();} catch (Exception e) {e.printStackTrace();}}public static void testObjectInputStream() {try {FileInputStream fis = new FileInputStream(new File("d:/person.bat"));ObjectInputStream ois = new ObjectInputStream(fis);Object obj = ois.readObject(); System.out.println(((Person)obj).toString());fis.close();ois.close();} catch (Exception e) {e.printStackTrace();}}}
- java中的IO基础知识
- java io基础知识
- java io基础知识
- java基础知识 ---io
- java基础知识----IO篇
- java基础知识IO流
- java io流基础知识
- Java IO基础知识(上)
- Java IO基础知识
- 【Java】IO流基础知识
- java基础知识---IO流
- Java基础知识-IO流
- java基础知识--IO篇
- Java基础知识-IO流
- Java基础知识梳理--IO
- java基础知识IO简单介绍
- Java基础知识回顾--IO流
- Java基础知识-IO流2
- 泛型
- paip.android环境搭建与开发事例
- poj 2586 Y2K Accounting Bug (贪心)
- 【读书笔记】《2030》阿尔伯特·布鲁克斯
- C 和 C++ 的速度相差多少,你知道吗?
- java中的IO基础知识
- hadoop权威指南mapreduce的一个程序
- iostat命令
- Uva10780 Again Prime? No Time.
- ubuntu 11.04安装vim
- jsp页面间传递的参数乱码的解决办法
- 插入排序
- 启动tomcat的 startup.bat屏幕一闪而过
- 项目管理的酸甜苦辣