java基础——文件(IO)操作1

来源:互联网 发布:cdn和云计算 编辑:程序博客网 时间:2024/05/16 14:40

一、流类

         Java的流式输入/输出建立在四个抽象类的基础上:InputStreamOutputStreamReaderWriter。它们用来创建具体流式子类。尽管程序通过具体子类执行输入/输入,但是顶层的类定义了所有流类的基本通用功能。

        InputStreamOutputStream设计成字节流类。ReaderWriter为字符流设计。字节流和字符流类形成分离的层次结构。一般,处理字符和字符串时应使用字符流类,处理字节或二进制对象时应用字节流类。

 

在操作文件流时,按以下方式进行:

        1.使用File类找到一个文件

        2.通过File类的对象去实例化字节流或字符流的子类

        3.进行字节或字符的读、写操作

       4.关闭文件流

 

二、字节流

         FileInputStream类:能从文件读取字节,常用的两个构造方法:

//参数为文件的绝对路径FileInputStream(String filepath)//参数为文件File的对象FileInputStream(File fileObj)

          FileOutputStream类:能向文件写入字节,常用的两个构造方法:

//参数为文件的绝对路径FileOutputStream(String filePath)//参数为文件File的对象FileOutputStream(File fileObj)//如果append为true,文件则以设置搜索路径模式打开FileOutputStream(String filePath,boolean append)

示例:

import java.io.*;public class StreamDemo{public static void main(String args[]){File f = new File("c:\\temp.txt") ;OutputStream out = null ;try{out = new FileOutputStream(f) ;}catch (FileNotFoundException e){e.printStackTrace();}// 将字符串转成字节数组byte b[] = "Hello World!!!".getBytes() ;try{// 将 byte 数组写入到文件之中out.write(b) ;}catch (IOException e1){e1.printStackTrace();}try{out.close() ;}catch (IOException e2){e2.printStackTrace();}// 以下为读文件操作InputStream in = null ;try{in = new FileInputStream(f) ;}catch (FileNotFoundException e3){e3.printStackTrace();}// 开辟一个空间用于接收文件读进来的数据byte b1[] = new byte[1024] ;int i = 0 ;try{// 将 b1 的引用传递到 read()方法之中,同时此方法返回读入数据的个数i = in.read(b1) ;}catch (IOException e4){e4.printStackTrace();}try{in.close() ;}catch (IOException e5){e5.printStackTrace();}//将 byte 数组转换为字符串输出System.out.println(new String(b1,0,i)) ;}}

执行结果:

                  

 

三、字符流

        FileReader类:可以读取文件内容,常用的构造方法同FileInputStream

        FileWriter类:可以写文件,常用的构造方法同FileOutputStream

示例:

import java.io.*;public class CharDemo{public static void main(String args[]){File f = new File("c:\\temp.txt") ;Writer out = null ;try{out = new FileWriter(f) ;}catch (IOException e){e.printStackTrace();}// 声明一个 String 类型对象String str = "Hello World!!!" ;try{// 将 str 内容写入到文件之中out.write(str) ;}catch (IOException e1){e1.printStackTrace();}try{out.close() ;}catch (IOException e2){e2.printStackTrace();}// 以下为读文件操作Reader in = null ;try{in = new FileReader(f) ;}catch (FileNotFoundException e3){e3.printStackTrace();}// 开辟一个空间用于接收文件读进来的数据char c1[] = new char[1024] ;int i = 0 ;try{// 将 c1 的引用传递到 read()方法之中,同时此方法返回读入数据的个数i = in.read(c1) ;}catch (IOException e4){e4.printStackTrace();}try{in.close() ;}catch (IOException e5){e5.printStackTrace();}//将字符数组转换为字符串输出System.out.println(new String(c1,0,i)) ;}}

输出结果:

             

三、字节流与字符流的转换

 

字节流和字符流之间转换的类:

InputStreamReader类:将一个字节流中的字节解码成字符。主要的构造函数:

    // 用默认字符集创建一个 InputStreamReader 对象    InputStreamReader(InputStream in)    // 接受已指定字符集名的字符串,并用该字符集创建对象    InputStreamReader(InputStream in,String CharsetName)

OuputStreamWriter类:将写入的字符编码成字节后写入一个字节流。主要的构造函数:

   // 用默认字符集创建一个 OutputStreamWriter 对象   OutputStreamWriter(OutputStream in)   // 接受已指定字符集名的字符串,并用该字符集创建 OutputStreamWriter 对象   OutputStreamWriter(OutputStream in,String CharsetNarme)

 

为了达到最高的效率,避免频繁地进行字符与字节间的相互转换,最好不要直接使用这两个类来进行读写,应尽量使用 BufferedWriter类包装 OutputStreamWriter类, BufferedReader类包装 InputStreamReader类。

例如:

BufferedWriter out=new BufferedWriter(newOutputStreamWriter(System.out));BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

示例:

import java.io.*;public class BufferDemo{public static void main(String args[]){BufferedReader buf = null;//System.in是inputStream类型//先讲字节流转换成字符流,再将字符流放入到了BufferedReader中buf = new BufferedReader(new InputStreamReader(System.in));String str = null;while (true){System.out.print("请输入数字:");try{//等待键盘输入数据str = buf.readLine();} catch (IOException e){e.printStackTrace();}int i = -1;try{//将输入的字符串转换成基本数据类型中的整型i = Integer.parseInt(str);i++;//将数据+1后输出System.out.println("输入的数字修改后为:" + i);break;}catch (Exception e){System.out.println("输入的内容不正确,请重新输入!");}}}}

执行结果:

                 

0 0
原创粉丝点击