Java IO(二)

来源:互联网 发布:淘宝买家注册时间查询 编辑:程序博客网 时间:2024/06/06 08:32
三、字节流与字符流的操作
在Java.io包中,流的操作主要有字节流、字符流两大类。
在字节流中,输入输出使用的是InputStream类和OutputStream类
在字符流中,输入输出使用的是Reader类和Writer类




字节流:
字节流主要操作的是byte类型数据,以byte数组为准。主要操作类是OutputStream类和InputStream类。


字节输出流:OutputStream
OutputStream类是整个IO包中字节输出流的最大父类,此类的定义如下:
public abstract class OutputStream extends Object implements Closeable,Flushable
从上面的定义可以发现,OutputStream类是一个抽象类,因此如果想要使用此类,则必须通过子类实例化对象。


OutputStream类中常用的方法
public void close() throws IOException //关闭输出流


public void flush() throws IOException //刷新缓冲区


public void write(byte[] b) throws IOException //将一个byte数组写入数据流




public void write(byte[] b,int off,int len) throws IOException//将指定范围的byte数字写入数据流


public abstract viod write(int b) throws IOException//将一个字节数据写入数据流


OutputStream类的一个子类:FileOutputStream类
FileOutputStream类的构造方法:
public FileOutputStream(File file) throws FileNotFoundException
范例一:
向文件中写入字符串
package com.zgy.outputstreamoperation;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;




public class FileOutputStreamTest01 
{


public static void main(String[] args) 
{
   File f = new File("D:"+File.separator+"test.txt");//给出路径
   OutputStream os = null;
   try 
   {
os = new FileOutputStream(f);//实例化对象
String str = "Hello World!!!";
   os.write(str.getBytes());//将字符串转化为byte数组进行写入
   os.close();

   catch (FileNotFoundException e) 
   {
e.printStackTrace();

   catch (IOException e) 
   {
e.printStackTrace();
}
   
}


}




范例二:
将上述字符串按照字节,一个个的写入文件
package com.zgy.outputstreamoperation;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;




public class FileOutputStreamTest02
{


public static void main(String[] args) 
{
   File f = new File("D:"+File.separator+"test.txt");
   OutputStream os = null;
   try 
   {
os = new FileOutputStream(f);
String str = "Hello World!!!";
   byte[] by = str.getBytes();
   for(int i = 0 ; i < by.length ; i++)
   {
    os.write(by[i]);
   }
   os.close();

   catch (FileNotFoundException e) 
   {
e.printStackTrace();

   catch (IOException e) 
   {
e.printStackTrace();
}
   
}


}






范例三:
对文件进行追加内容操作
package com.zgy.outputstreamoperation;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;




public class FileOutputStreamTest02
{


public static void main(String[] args) 
{
   File f = new File("D:"+File.separator+"test.txt");
   OutputStream os = null;
   try 
   {
os = new FileOutputStream(f);
String str = "Hello World!!!";
   byte[] by = str.getBytes();
   for(int i = 0 ; i < by.length ; i++)
   {
    os.write(by[i]);
   }
   os.close();

   catch (FileNotFoundException e) 
   {
e.printStackTrace();

   catch (IOException e) 
   {
e.printStackTrace();
}
   
}


}




范例三:
对文件的内容进行追加
package com.zgy.outputstreamoperation;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;




public class FileOutputStreamTest03 
{


public static void main(String[] args) 
{
   File f = new File("D:"+File.separator+"test.txt");
   OutputStream os = null;
   try
   {
    os = new FileOutputStream(f,true);
    String str = "Java IO";
    os.write(str.getBytes());
    os.close();
   }
   catch(FileNotFoundException e)
   {
    e.printStackTrace();
   }
   catch(IOException e)
   {
    e.printStackTrace();
   }
   
   
}


}




字节输入流InputStream
InputStream类的定义:
public abstract class InputStream extends Object implements Closeable
InputStream本身也是一个抽象类,必须依靠其子类。
InputStream类中的常用方法
public int available() throws IOException //取得输入文件的大小


public void close() throws IOException //关闭输入流


public abstract int read() throws IOException //读取内容,以数字的方式读取


public int read(byte[] b) throws IOException //将内容读取到byte数组中,同时返回读入的个数


InputStream类的一个子类FileInputStream类的构造方法
public FileInputStream(File file) throws FileNotFoundException




范例四:
从文件中读取内容
package com.zgy.fileintputstreamoperation;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;


public class FileInputStreamTest01 
{


public static void main(String[] args) 
{
   File f = new File("D:"+File.separator+"test.txt");
   InputStream is = null;
   try
   {
    is = new FileInputStream(f);
    byte[] by = new byte[(int)f.length()];
    is.read(by);
    is.close();
    System.out.println(new String(by));
   }
   catch(FileNotFoundException e)
   {
    e.printStackTrace();
   }
   catch(IOException e)
   {
    e.printStackTrace();
   }
}


}






字符流:
字符输出流Writer
Writer类的定义:
public abstract class Writer extends Object implements Appendable,Closeable,Flushable
Writer 类是抽象类,如果要使用此类,则要使用其子类
Writer类的常用方法
public abstract void close() throws IOException //关闭输出流


public void write(String str) throws IOException //将字符串输出


public void writer(char[] cbuf) throws IOException //将字符数组输出


public abstract void flush() throws IOException //强制性清空缓存


Writer类的一个子类:FileWriter
FileWriter类的构造方法:
public FilterWriter(File file) throws IOException


范例五:
向文件中写入数据
package com.zgy.writeroperation;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;


public class FileWriterOperation 
{


public static void main(String[] args) 
{
File f = new File("D:"+File.separator+"test.txt");
Writer wr = null;
try
{
wr = new FileWriter(f);
String str = "Hello Java!!!";
wr.write(str);
wr.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}


}




范例六:
向指定文件追加内容
package com.zgy.writeroperation;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;


public class FileWriterOperation02 
{


public static void main(String[] args) 
{
File f = new File("D:"+File.separator+"test.txt");
Writer wr = null;
try
{
wr = new FileWriter(f,true);
String str = "Hello Java!!!";
wr.write(str);
wr.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}


}






字符输入流Reader
public abstract class Reader extends Object implements Readable,Closeable
Reader本身也是抽象类,如果要使用该类,则必须使用其子类。
Reader类的常用方法
public abstract void close() throws IOException //关闭输出流


public int read() throws IOException //读取单个字符


public int read(char[] cbuf) throws IOException //将内容读到字符数组,返回读入的长度


Reader类的一个子类:FileReader
public FileReader(File file) throws FileNotFoundException




范例七:
从文件中读取内容
package com.zgy.readeroperation;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;


public class ReaderOperation 
{


public static void main(String[] args) 
{
File f = new File("D:"+File.separator+"test.txt");
Reader reader = null;
try
{
reader = new FileReader(f);
char[] ch = new char[(int)f.length()];
reader.read(ch);
reader.close();
System.out.println(new String(ch));
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}


}










字节流与字符流的区别:
字节流在操作时本身不会使用到缓冲区,是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作文件。








转换流:
字节字符转换流:OutputStreamWriter类与InputStreamReader类
OutputStreamWriter:是Writer的子类,将输出的字符流转变为字节流。将字符流的输出对象变成字节流输出对象。
InputStreamReader:是Reader的子类,将输入的字节流转变为字符流。将字节流的输入对象变成字符流输入对象。


OutputStreamWriter的构造方法
public OutputStreamWriter(OutputStream out)
范例:
将字符输出流转变为字节输出流
package com.zgy.outputstreamwriter;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;


public class OutputStreamWriterOperation 
{


public static void main(String[] args) 
{
File f = new File("D:"+File.separator+"test.txt");
Writer out = null;
try
{
out = new OutputStreamWriter(new FileOutputStream(f));
out.write("Hello Hello Hello");
out.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}


}






范例:
将字节输入流转换为字符输入流
package com.zgy.inputstreamreader;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;




public class InputStreamReaderOperation 
{


public static void main(String[] args) 
{
        File f = new File("D:"+File.separator+"test.txt");
        Reader in = null;
        
        try
        {
        in = new InputStreamReader(new FileInputStream(f));
        char[] ch = new char[(int)f.length()];
        in.read(ch);
        System.out.println(new String(ch));
        }
        catch(FileNotFoundException e)
        {
        e.printStackTrace();
        }
        catch(IOException e)
        {
        e.printStackTrace();
        }
}


}
0 0
原创粉丝点击