多种方式实现写文件
来源:互联网 发布: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
- 多种方式实现写文件
- 多种方式实现文件读
- 多种方式实现文件下载功能
- 多种方式实现文件下载功能
- 文件的读与写的多种方式
- java中多种写文件方式的效率对比实验
- iOS中多种方式实现打电话、发短信、写邮件
- java多种方式实现1G文件复制
- Java 多种文件读写方式
- Java 多种文件读写方式
- java多种方式操作文件
- java 多种方式文件读取
- LRU的多种实现方式
- ligerUI ligerTree多种实现方式
- 多种方式实现控制器层
- MapReduce工作流多种实现方式
- 寻找文件下载源的多种方式
- java中多种方式读文件
- VS2008查看消息的响应函数
- 进程同步之临界区域问题及Peterson算法
- 似懂非懂的SynchronousQueue和长度为1的BlockingQueue
- Javascript面向对象编程(二):构造函数的继承
- Java Field 详解
- 多种方式实现写文件
- hdu 4961 Boring Sum(2014 Multi-University Training Contest 9)
- 我姐结婚 我去挣压箱钱你思想能正常点吗
- 和第一行被执行
- select模型详解
- iOS应用开发:什么是ARC?
- mysql数据库备份
- 使用pager-taglib分页案例
- HDOJ1002 A + B Problem II