FileOutputStream FileInputStream FileWriter FileReader

是输入输出流对文件的接口,stream是对字节(byte[])的操作,writer是对字符(string)的操作。

 

OutputStreamWriter InputStreamReader

构造函数参数为OutputStream和InputStream,也就是说用于把字节转为字符(byte[] to String)

DataOutputStream dout= new DataOutputStream(new FileOutputStream(file));
dout.writeInt(1);
OutputStreamWriter osw = new OutputStreamWriter(dout);
PrintWriter pw= new PrintWriter(osw);

 

PrintWriter 和 BufferedWriter

PrintWriter和BufferedWriter都是继承java.io.Writer,所以很多功能都一样。不过PrintWriter提供println()方法可以写不同平台的换行符,而BufferedWriter可以任意设定缓冲大小。

 

PrintWriter有含有OutputStream、File、Writer的构造方法。BufferedWriter只接受writer。

 

默认是清空文件

PrintWriter pwriter = new PrintWriter(new File("f:/test.txt"););

BufferedWriter bwriter = new BufferedWriter(pwriter);

如果需要不清空文件

PrintWriter pwriter = new PrintWriter(new FileWriter(new File("f:/test.txt"),true));

BufferedWriter bwriter = new BufferedWriter(new FileWriter(new File("f:/test.txt"),true));

 

PrintWriter提供方法为各个平台换行,println()

BufferedWriter的换行则为newLine()

 

基本上这两个类都差不多,用哪个都可以,PrintWriter稍好

 

 

ByteArrayOutputStream

将各种对象写入内存缓冲区,然后写入文件,

ByteArrayOutputStream array = new ByteArrayOutputStream();
int a = 1;
array.write(a);
byte[] bts = array.toByteArray();

array.writeTo(new FileOutputStream(bfile, true));//通过outputstream写入文件

 

DataOutputStream

可以写各种基本类型,可以用它来封一层ByteArrayOutputStream来写各种基本类型,也可以直接向各种outputstream中写,例如文件

ByteArrayOutputStream array = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(array);//向array中输出
out.writeBytes("sasdfsafsdf");
out.writeChar('A');
out.writeInt(123);
DataOutputStream to = new DataOutputStream(new FileOutputStream(bfile, true));//向文件中输出
array.writeTo(to);

 

//直接向文件中写

DataOutputStream ssss = new DataOutputStream(new FileOutputStream(bfile));
ssss.writeInt(1);

 

ObjectOutputStream

写入对象 

 

 

 

Reader与Writer差不多

主要用FileInputStream,BufferedReader, BufferedInputStream, GZIPInputStream

BufferedReader reader = new BufferedReader(freader);
String line = null;
while ((line = reader.readLine()) != null)
{
      System.out.println(line);
}
reader.close();

 

//直接读进byte[]

byte[] data = new byte[(int)bfile.length()];
fis.read(data);
fis.close();
           
//从二进制文件读出,然后转成reader。           
FileInputStream fileInputStream   = new FileInputStream(bfile);
//GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
//InputStreamReader inputStreamReader = new InputStreamReader(gzipInputStream);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bfreader = new BufferedReader(inputStreamReader);

 

 

 

读写

BufferedReader reader = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new File("f:/writerTest.txt"));
String line = null;
while ((line = reader.readLine()) != null)
{
       System.out.println(line);
       pw.println(line);
}
reader.close();
pw.close();

 

//字节读写

BufferedInputStream in = new BufferedInputStream(new FileInputStream(bfile));
ByteArrayOutputStream array = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(new FileOutputStream("f:/bwriterTest.txt"));
byte[] buffer = new byte[4096];
int numBytesRead;
while ((numBytesRead = in.read(buffer, 0, 4096)) != -1)
{
      array.write(buffer, 0 ,numBytesRead);

      //dos.write(buffer, 0 ,numBytesRead);
}
array.writeTo(dos);
dos.close();
in.close();
array.close();

 

 

 

 

posted on 2012-06-14 11:51  清沁  阅读(842)  评论(0编辑  收藏  举报