多种方式实现写文件

来源:互联网 发布:php登录注册完整代码 编辑:程序博客网 时间:2024/06/13 02:20
/* * 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 * 联系方式: 644143964@qq.com * 描述:多种方式写文件 * */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 blockSystem.out.println("写文件【" + file.getAbsolutePath() + "】失败");e.printStackTrace();} finally {if (out != null) {try {out.close(); //关闭输出文件流} catch (IOException e) {// TODO Auto-generated catch blockSystem.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 blockSystem.out.println("写文件【" + file.getAbsolutePath() + "】失败");e.printStackTrace();} finally {if (writer != null) {try {writer.close(); //关闭输出文件流} catch (IOException e) {// TODO Auto-generated catch blockSystem.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 blockSystem.out.println("写文件【" + file.getAbsolutePath() + "】失败");e.printStackTrace();} finally {if (writer != null) {writer.close(); //关闭输出文件流}}}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubString 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);}}

0 0