Java读写文件文本文件的示例

/**
 * 读/写文本文件
 * 1. 将文件读入到StringBuffer,在控制台输出
 * 2. 将内容中的文本写到文件
 * 3. 将一个文件的内容读出来写入另一个文件中 
 * 4. 展示了如何从输入流中读出来内容写入输出流中(仅限文本流)
 * 5. 如何判断命名管道及文件是否存在,若不存在即创建
 */
public class IOFile {

	public static void main(String[] args) {
		// 实例化,可以调用类中的方法
		IOFile demo = new IOFile();
		// 声明键盘输入
		Scanner scanner = new Scanner(System.in);
		boolean isNext = true;
		while(isNext) {
			System.out.println("===========================================");
			System.out.println("1. 读\n" + "2. 写\n" + "3. 拷贝");
			System.out.println("**********选择数字1/2/3**********");
			int choice = scanner.nextInt();
			switch (choice) {
			case 1: // 测试读文件
			{
				// 声明输入流
				InputStream is = null;
				// 定义文件目录(路径)
				File fileCon = new File("E:/test/");
				// 判断该路径是否存在,若不存在,创建
				if (!fileCon.exists()) {
					// 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录
					fileCon.mkdirs();
				}
				try {
					// 定义文件的File类
					File file = new File("E:/test/test.txt");
					// 判断文件是否存在,若不存在,创建新文件
					if (!file.exists()) {
						file.createNewFile();
					}
					is = new FileInputStream(file);
					StringBuffer buffer = new StringBuffer();
					// 调用readToBuffer()方法将is流中的内容读到buffer中
					demo.readToStringBuffer(buffer, is);
					// 将读到 buffer 中的内容写出来
					System.out.println(buffer);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					try {
						// 关闭流
						is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				break;
			}
			case 2: // 测试写,写到控制台
			{
				StringBuffer buffer = new StringBuffer("Only a test\n");
				// System.out本身就是PrintStream对象
				demo.writeFromStringBuffer(buffer, System.out);
				break;
			}
			case 3: // 测试拷贝
			{
				try {
					// 调用copyTextFile()方法
					demo.copyFile("E:/test/test.txt", "E:/test/r.txt");
				} catch (IOException e) {
					e.printStackTrace();
				}
				break;
			}
			default: // 退出
			{
				isNext = false;
			}
			}
		}
		System.out.println("退出。。。");
	}

	/**
	 * 1. 演示将流中的文本读入一个 StringBuffer 中,控制台打印
	 * 
	 * @throws IOException
	 */
	public void readToStringBuffer(StringBuffer strBuf, InputStream is)
			throws IOException {
		String line; // 用来保存每行读取的内容
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		//按行读取内容
		while ((line = reader.readLine()) != null) {//如果 line为空说明读完了
			// 将读到的内容添加到buffer中
			strBuf.append(line);
			// 添加换行符
			strBuf.append("\n");
		}
	}

	/**
	 * 2. 演示将 StringBuffer中的内容读出到流中
	 */
	public void writeFromStringBuffer(StringBuffer strBuf, OutputStream os) {
		// 用 PrintStream 可以方便的把内容输出到输出流中
		PrintStream ps = new PrintStream(os);
		ps.print(strBuf.toString());
	}

	/**
	 * 3. 拷贝文本文件
	 */
	public void copyFile(String inFilename, String outFilename)
			throws IOException {
		// 先根据输入/输出文件生成相应的输入/输出流
		InputStream is = new FileInputStream(inFilename);
		OutputStream os = new FileOutputStream(outFilename);
		// 调用 copyStream(InputStream, OutputStream)方法
		copyToStream(is, os); // 用 copyStream 拷贝内容
		is.close(); // is 是在这里打开的,所以需要关闭
		os.close(); // os 是在这里打开的,所以需要关闭
	}

	/**
	 * 3.1 从输入流中拷贝内容到输入流中
	 * 
	 * @throws IOException
	 */
	public void copyToStream(InputStream is, OutputStream os) throws IOException {
		String line;
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		PrintWriter writer = new PrintWriter(new OutputStreamWriter(os));
		while ((line = reader.readLine()) != null) {
			writer.println(line);
		}
		writer.flush(); // 刷新缓冲区
	}
}


猜你喜欢

转载自blog.csdn.net/sdsky1987/article/details/7054338