用户名: 密   码:
   飞诺网 加入收藏
飞诺网 软件编程 C C++ Java VB Delphi Foxpro 汇编语言 游戏开发 移动开发 软件工程师 软工与管理 VC shell编程 C#
Java系列教程 Java实例 Java技术文档 lucene J2ME

您当前的位置:飞诺网 >> java >> Java系列教程

java IO 输入输出(九)

www.diybl.com    时间 : 2010-12-03  作者:佚名   编辑:fnw 点击:   [ 评论 ]

九、写文件
    用java写文件有多种方法,对于不同类型的数据,有不同的写入方法,写文件的关键技术点如下:
    1、FileOutputStream打开文件输出流,通过write方法以字节为单位写入文件,是写文件最通用的方法,能写入任何类型的文件,特别适合写二进制数据文件
    2、OutputStreamWriter打开文件输出流,通过write方法以字符为单位写入文件,能够将字符数组和字符串写入文件。
    3、PrintWriter打开文件输出流,通过print和println方法写字符串到文件,与System.out的用法相似,常用于写入格式化的文本。
    
    注意:当文件写完后关闭输出流。
演示实例 

package book.io; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
import java.io.Writer; 

/** *//** 
 * 多种方式写文件 
 * @author joe 
 * 
 */ 

public class WriteToFile ...{ 
    /** *//** 
     * 以字节为单位写文件。适合于写二进制文件,如图片等 
     * @param fileName    文件名 
     */ 
    public static void writeFileByBytes(String fileName) ...{ 
        File file = new File(fileName); 
        OutputStream out = null; 
        try ...{ 
            //打开文件输出流 
            out = new FileOutputStream(file); 
            String content = "文件内容: 1,The First line; 2,The second line."; 
            byte[] bytes = content.getBytes();    //读取输出流中的字节 
            out.write(bytes); 
            System.out.println("写文件" + file.getAbsolutePath() + "成功!"); 
        } catch (IOException e) ...{ 
            System.out.println("写文件" + file.getAbsolutePath() + "失败!"); 
            e.printStackTrace(); 
        } finally ...{ 
            if (out != null) ...{ 
                try ...{ 
                    out.close(); 
                } catch(IOException e1) ...{ 
                    e1.printStackTrace(); 
                } 
            } 
        } 
    } 
     
    /** *//** 
     * 以字符为单位写文件 
     * @param fileName    文件名 
     */ 
    public static void writeFileByChars(String fileName) ...{ 
        File file = new File(fileName); 
        Writer writer = null; 
        try ...{ 
            //打开文件输出流 
            writer = new OutputStreamWriter(new FileOutputStream(file)); 
            String counter = "文件内容  1,The First line; 2,The second line."; 
            writer.write(counter); 
            System.out.println("写文件 " + file.getAbsolutePath() + "成功!"); 
        } catch (IOException e) ...{ 
            System.out.println("写文件 " + file.getAbsolutePath() + "失败!"); 
            e.printStackTrace(); 
        } finally ...{ 
            if(writer != null) ...{ 
                try...{ 
                    writer.close(); 
                } catch (IOException e1) ...{ 
                    e1.printStackTrace(); 
                } 
            } 
        } 
    } 
     
    /** *//** 
     * 以行为单位写文件 
     * @param fileName    文件名 
     */ 
    public static void writeFileByLines(String fileName) ...{ 
        File file = new File(fileName); 
        PrintWriter writer = null; 
        try ...{ 
            writer = new PrintWriter(new FileOutputStream(file)); 
            writer.println("文件内容:");    //写字符串 
            //写入各种基本类型数据 
            writer.print(true); 
            writer.print(155); 
            writer.println();    //换行 
            writer.flush();    //写入文件 
            System.out.println("写文件 " + file.getAbsolutePath() + "成功!"); 
        } catch (IOException e) ...{ 
            System.out.println("写文件 " + file.getAbsolutePath() + "失败!"); 
            e.printStackTrace(); 
        } finally ...{ 
            if (writer != null) ...{ 
                writer.close();    //关闭输出文件流 
            } 
        } 
    } 
     
    public static void main(String[] args) ...{ 
        String fileName = "d:\work\temp\newTemp.txt"; 
        WriteToFile.writeFileByBytes(fileName); 
        fileName = "d:\work\temp\newTemp1.txt"; 
        WriteToFile.writeFileByChars(fileName); 
        fileName = "d:\work\temp\newTemp2.txt"; 
        WriteToFile.writeFileByLines(fileName); 
    } 

}  

 

 

 输出结果:
写文件d:\work\temp\newTemp.txt成功!
写文件 d:\work\temp\newTemp1.txt成功!
写文件 d:\work\temp\newTemp2.txt成功!

如果图片或页面不能正常显示请点击这里
Java系列教程推荐文章

文章评论