多种方式实现写文件

/*
 * Copyright 2014 Chencp.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.chencp.demo.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;

/**
 * WriteToFile.java
 * 作者: changping chen
 * 联系方式: [email protected]
 * 描述:多种方式写文件
 *
 */
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 = "文件内容:\n1,The first line;\n2,The second line.";
			byte[] bytes = content.getBytes();
			//写入文件
			out.write(bytes);
			System.out.println("写文件【" + file.getAbsolutePath() + "】成功");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("写文件【" + file.getAbsolutePath() + "】失败");
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close(); //关闭输出文件流
				} catch (IOException e) {
					// TODO Auto-generated catch block
					System.out.println("关闭输出文件流失败");
					e.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 content = "文件内容:\n1,The first line;\n2,The second line.";
			writer.write(content);
			System.out.println("写文件【" + file.getAbsolutePath() + "】成功");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("写文件【" + file.getAbsolutePath() + "】失败");
			e.printStackTrace();
		} finally {
			if (writer != null) {
				try {
					writer.close(); //关闭输出文件流
				} catch (IOException e) {
					// TODO Auto-generated catch block
					System.out.println("关闭输出文件流失败");
					e.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(123);
			writer.println();
			writer.flush();//写入文件
			System.out.println("写文件【" + file.getAbsolutePath() + "】成功");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("写文件【" + file.getAbsolutePath() + "】失败");
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.close(); //关闭输出文件流
			}
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String fileName = null;
		WriteToFile test = new WriteToFile();
		fileName = "d:/temp/tempfile0.txt";
		test.writeFileByBytes(fileName);
		fileName = "d:/temp/tempfile1.txt";
		test.writeFileByChars(fileName);
		fileName = "d:/temp/tempfile2.txt";
		test.writeFileByLines(fileName);
	}

}

猜你喜欢

转载自blog.csdn.net/changpingchen/article/details/38684843
今日推荐