导出

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * FileWriter是字符流写入字符到文件中。默认情况,如WriteToFileExample里面那样,会用新的内容来替换现有的内容
 * 这个里面,FileWriter fileWritter = new FileWriter(file.getPath(), true);
 * 用了true,不会替换以前的,会接着以前的内容写
 * 
 * 和FileOutStream的区别在于,后者是通过字节流将字节保存到文件中。
 * 
 * FileWriter适用于文本文件。
 * FileOutStream用来处理二进制格式的文件
 * 
 * 二进制文件和文本文件的区别:文本文件是二进制文件的特例
 * 
 * @author hu.sun
 *
 */
public class AppendToFileExample {
	public static void main(String[] args) {
		try {
			String data = " This content will append to the end of the file";

			File file = new File("F:/javaio-appendfile.txt");
			
			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
			}

			// true = append file
			FileWriter fileWritter = new FileWriter(file.getPath(), true);
			BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
			bufferWritter.write(data);
			bufferWritter.close();

			System.out.println("Done");

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class WriteFileExample {
	
	
	public static void main(String[] args){
		File file = new File("F:/javaio-appendfile.txt");
		try {
			FileOutputStream stream = new FileOutputStream(file);
			String content = "by FileOutStream!";
			try {
				stream.write(content.getBytes());
				stream.flush();
				stream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
	
	
}

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFileExample {
	public static void main(String[] args) {
		try {

			String content = "This is the content to write into file";

			File file = new File("F:/javaio-appendfile.txt");

			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
			}

			FileWriter fw = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(content);
			bw.close();

			System.out.println("Done");

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


你可能感兴趣的:(导出)