자바 추가 파일

1783 단어
package com.readfile;

import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

public class AppendToFile {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String fileName = "C:/Users/Administrator/Desktop/Noname1.txt";
        String content = "new append!";
        //   A    
        AppendToFile.appendMethodA(fileName, content);
        AppendToFile.appendMethodA(fileName, "append end. 
"); // ReadFromFile.readFileByLines(fileName); // B AppendToFile.appendMethodB(fileName, content); AppendToFile.appendMethodB(fileName, "append end.
"); // ReadFromFile.readFileByLines(fileName); } /** * A : RandomAccessFile */ public static void appendMethodA(String fileName, String content) { try { // , RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); // , long fileLength = randomFile.length(); // 。 randomFile.seek(fileLength); randomFile.writeBytes(content); randomFile.close(); } catch (IOException e) { e.printStackTrace(); } } /** * B : FileWriter */ public static void appendMethodB(String fileName, String content) { try { // , true FileWriter writer = new FileWriter(fileName, true); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }

좋은 웹페이지 즐겨찾기