android 寫txt文件

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

private static FileWriter writer = null;

	// 判斷文件是否存在
	private static boolean FileIsExists(String strFile) {
		try {
			File f = new File(strFile);
			if (!f.exists()) {
				return false;
			}

		} catch (Exception e) {
			return false;
		}

		return true;
	}

	private static void SetTxtPath(String path) {
		if (path != "") {
			String fileName = path + "/tmp.txt";
			if (FileIsExists(fileName)) {
				File f = new File(fileName);
				long size = f.length();
				if (size / 1024 > 100)// 大於100M
				{
					f.delete();
					try {
						f.createNewFile();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				OpenFile(f);
			} else {
				File f = new File(fileName);
				try {
					f.createNewFile();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				OpenFile(f);
			}
		}
	}
	private static void WriteTxtData(FileWriter writer, String content) {
		if (writer != null) {
			try {
				writer.write(content);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private static void OpenFile(File file) {
		try {
			writer = new FileWriter(file, true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void CloseFile() {
		if (writer != null) {
			try {
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
相關文章
相關標籤/搜索