文件读写操作

临时写的,还没调试过,明天再调试吧,现在要帮Chris买东西。

package net.impjq.androidjson;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
import android.app.Activity;
import android.os.Bundle;
 
public class AndroidJson extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}
 
	public StringBuffer readFileContent(String path) {
		File file = new File(path);
 
		if (!file.exists()) {
			return null;
		}
 
		StringBuffer stringBuffer = new StringBuffer();
 
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			InputStreamReader bufferedInputStream = new InputStreamReader(
					fileInputStream);
			BufferedReader bufferedReader = new BufferedReader(
					bufferedInputStream);
			String line;
			line = bufferedReader.readLine();
			while (null != line) {
				stringBuffer.append(line);
				stringBuffer.append('\n');
 
				line = bufferedReader.readLine();
			}
			bufferedReader.close();
			bufferedInputStream.close();
			fileInputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		return stringBuffer;
	}
 
	public boolean writeFileContent(String path, String toBeWriteContent) {
		File file = new File(path);
 
		if (!file.exists()) {
			try {
				if (!file.createNewFile()) {
					return false;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
 
		try {
			BufferedWriter bufferedWriter = new BufferedWriter(
					new OutputStreamWriter(new FileOutputStream(file)));			
			bufferedWriter.write(toBeWriteContent);
			bufferedWriter.close();		
			return true;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
 
	}
 
}