Java 文件读写简单实例

import java.io.*;

public class ReadWrite {

	public void writeFile() {
		String str = "this  is a program"; // 要写入的内容
		try {
			FileOutputStream out = new FileOutputStream("d:/程序/ReadWriteshuchu.txt"); // 输出文件路径
			out.write(str.getBytes());
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void readFile() {
		try {
			FileInputStream in = new FileInputStream("d:/程序/ceshi.txt"); // 读取文件路径
			byte bs[] = new byte[in.available()];
			in.read(bs);
			System.out.println("file content=\n" + new String(bs));
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		ReadWrite t = new ReadWrite();
		t.readFile();
		t.writeFile();
	}
}

你可能感兴趣的:(Java)