Java追加文件内容的三种方法
来源:互联网 发布:疯狂的美工助手要钱吗 编辑:程序博客网 时间:2024/04/27 17:30
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.io.RandomAccessFile;
- /**
- *
- * @author malik
- * @version 2011-3-10 下午10:49:41
- */
- public class AppendFile {
- public static void method1(String file, String conent) {
- BufferedWriter out = null;
- try {
- out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
- out.write(conent);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if(out != null){
- out.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 追加文件:使用FileWriter
- *
- * @param fileName
- * @param content
- */
- public static void method2(String fileName, String content) {
- FileWriter writer = null;
- try {
- // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
- writer = new FileWriter(fileName, true);
- writer.write(content);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if(writer != null){
- writer.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 追加文件:使用RandomAccessFile
- *
- * @param fileName 文件名
- * @param content 追加的内容
- */
- public static void method3(String fileName, String content) {
- RandomAccessFile randomFile = null;
- try {
- // 打开一个随机访问文件流,按读写方式
- randomFile = new RandomAccessFile(fileName, "rw");
- // 文件长度,字节数
- long fileLength = randomFile.length();
- // 将写文件指针移到文件尾。
- randomFile.seek(fileLength);
- randomFile.writeBytes(content);
- } catch (IOException e) {
- e.printStackTrace();
- } finally{
- if(randomFile != null){
- try {
- randomFile.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static void main(String[] args) {
- try{
- File file = new File("d://text.txt");
- if(file.createNewFile()){
- System.out.println("Create file successed");
- }
- method1("d://text.txt", "123");
- method2("d://text.txt", "123");
- method3("d://text.txt", "123");
- }catch(Exception e){
- System.out.println(e);
- }
- }
- }
0 0
- Java追加文件内容的三种方法
- Java追加文件内容的三种方法 .
- Java追加文件内容的三种方法
- Java追加文件内容的三种方法
- Java追加文件内容的三种方法
- Java追加文件内容的三种方法
- Java追加文件内容的三种方法
- 【大汇总】Java追加文件内容的三种方法
- Java追加文件内容的三种方法
- Java追加文件内容的三种方法
- Java追加文件内容的三种方法
- Java追加文件内容的三种方法
- Java追加文件内容的三种方法
- Java追加文件内容的三种方法
- Java追加文件内容的三种方法
- Java追加文件内容的三种方法
- Java追加文件内容的三种方法
- 追加文件内容的三种方法
- JSON和JS对象
- LINK - Windows Universal App in-app purchase
- 抓取网页
- OMG 接口定义语言
- Statement 和 PreparedStatement CallableStatement及hibernate中的session
- Java追加文件内容的三种方法
- nike air max pas cher also had to apologize this contact with the passengers. But he admitted that t
- escarpins louboutin five available Jiabao left handed down to future generations
- eclipse config 5 括号匹配插件
- HDU_2192_背包问题
- scarpe hogan the old lady had an idea
- toms outlet online and the captured
- 强大的vim配置文件,让编程更随意
- nike factory is expected to increase to 10 by the end of this year