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

java中的IO基础知识

2013-08-05 22:19 387 查看
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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: