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

Java输入输出流

2015-06-22 17:42 573 查看
Java的IO体系主要有5类1接口:InputStream、OutputStream、Reader、Writer、File 和Serializable接口。

InputStream、OutputStream、Reader、Writer是抽象类,InputStream和OutputStream是字节流的父类,Reader、Writer是字符流的父类。

找个图看一下IO体系结构:



字节流是一个字节一个字节的读取,这个针对单字节的文件还是可以的,但是文件中有中文,如果读取的位置不对,是会出现乱码的。而字符流就可以解决这一问题,它每次读取的是2个字节的。File类是针对文件系统操作,对文件进行操作。

字节流不能使用缓冲区,而字符流是可以使用缓冲区的。缓冲区的作用是将读取或写入的字符存放到一块内存区域,等到一定时候,则一并处理,这样可以加快处理的速度,字节流则是每次都和要读取的或者是要写入的文件进行交互,这样会增大系统的时间开销,所以速度会慢。

有很多的字节流类和字符流类继承的InputStream、OutputStream、Writer、Reader。

IO的输入输出源和目的地:将数据读入程序中,则使用read方法,程序将数据写入到文件中则使用write方法,这两个方法有很多的参数,根据需要使用。

Java的序列化是为了将对象流化,可以对其进行读写操作,对象都是在JVM中存在,如果JVM关闭则对象就消失,但是使用序列化之后对象就可以被写入到文件中,以后如果使用的话可以再进行读取文件,那么对象又可以重新使用,可以序列化的对象必须是实现了Serializable接口的。反序列化就是将序列化的对象重新取出来使用。

贴上几段代码:

这段代码主要是File类的使用

package cn.swpu;

import java.io.File;
import java.io.FilenameFilter;

class FileAccept implements FilenameFilter  //FilenameFilter是接口,只有一个accept方法
{

String str = null;
FileAccept(String s)
{
str = "."+s;
}
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(str);
}

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

File dir = new File("D:/ch1");

FileAccept acceptCondition = new FileAccept("java");

File fileName[] = dir.listFiles(acceptCondition);//调用listFiles方法时才会去调用accept方法。accept方法在创建对象时不会调用

for(int i = 0 ; i < fileName.length; i++)
{
System.out.printf("\n文件名称:%s,长度:%d",
fileName[i].getName(),fileName[i].length());
}

boolean boo = false;
if(fileName.length > 0)
{
boo = fileName[0].delete();

if(boo)
System.out.printf("\n文件:%s被删除:",fileName[0].getName());
}
}
}

这段代码是使用Java字节流,如果有中文,读写时可能会出现乱码
package cn.swpu;
import java.io.*;

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

File file = new File("D:/hello.txt");
byte[] b = "欢迎welcome".getBytes();

try
{
FileOutputStream out = new FileOutputStream(file);
out.write(b);
out.close();

FileInputStream in  = new FileInputStream(file);
//in.read(b, 1, 3);
int n = 0;
String str;
while((n = in.read(b,0,6)) != -1)
{
str = new String(b,0,n);
System.out.println(str);
}
in.close();
str = null;
}catch(IOException e)
{
System.out.println(e);
}
}
}


这段是使用字符流:

package cn.swpu;
import java.io.*;

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

File file = new File("D:/hello.txt");
char b[] = "欢迎welcome".toCharArray();

try{
FileWriter out = new FileWriter(file,true);
out.write(b);
out.write("来到北京");
out.flush();
out.close();

FileReader in = new FileReader(file);

int n = 0;
String str;

while((n = in.read(b, 0, 5)) != -1)
{
str = new String(b,0,n);
System.out.println(str);
}

in.close();
str = null;

}catch(IOException ex){System.out.println(ex);}
}
}


这段是使用了缓冲流,只能用在字符流中:

package cn.swpu;

import java.io.*;

public class Example9_5 {
public static void main(String[] args) {
File readFile = new File("D:/hello.txt");
File writeFile = new File("D:/Hello1.txt");

try
{
FileReader inOne = new FileReader(readFile);
BufferedReader in = new BufferedReader(inOne);

FileWriter outTwo = new FileWriter(writeFile);
BufferedWriter out = new BufferedWriter(outTwo);

String s = null;
int i = 0;
while((s = in.readLine() ) != null)
{
i++;
out.write(i+":"+s);
out.newLine();
}
out.flush();
out.close();
outTwo.close();

while((s = in.readLine()) != null)
{
System.out.println( s );
}

inOne.close();
in.close();

}catch(IOException ex)
{
System.out.println(ex);
}
}
}


这段是使用对象流:

package cn.swpu;
import java.io.*;

@SuppressWarnings("serial")
class Goods implements Serializable
{
String name ;
double unitPrice;

public Goods(String name,double unitPrice)
{
this.name = name;
this.unitPrice = unitPrice;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setUnitPrice(double unitPrice)
{
this.unitPrice = unitPrice;
}
public double getUnitPrice()
{
return this.unitPrice;
}

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

Goods TV1 = new Goods("HaierTV",3468);

try{
FileOutputStream fileOut  = new FileOutputStream("D:/a.txt");
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(TV1);
FileInputStream fileIn = new FileInputStream("D:/a.txt");
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
Goods TV2 = (Goods)objectIn.readObject();
//	TV2.setUnitPrice(888);
//	TV2.setName("GREATWALL");
System.out.printf("\nTV1:%s,%f",TV1.getName(),TV1.getUnitPrice());
System.out.printf("\nTV2:%s,%f",TV2.getName(),TV2.getUnitPrice());
}catch(IOException ex) {}
catch(ClassNotFoundException ex){}
}
}



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