indentwriter.java

来自「一个java操作xml的完整示例」· Java 代码 · 共 68 行

JAVA
68
字号
package org.jdom.test.generate;

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

public  class IndentWriter extends PrintWriter {

		/** The number of indents to use per line */
		int indent;
		
	
		/**
		 * Create a new instance using the specified file as the 
		 * target.
		 *
		 * @param f	The target file.
		 */
		IndentWriter(File f) throws IOException {
			super(new BufferedWriter(new FileWriter(f)));
		}
		/**
		 * Create a new instance using the specified file as the 
		 * target. and append if set
		 *
		 * @param f	The target file.
		 */
		IndentWriter(File f, boolean append) throws IOException {
			super(new BufferedWriter(new FileWriter(f.getAbsolutePath(), append)));
		}
		/**
		 * Decrement the indent.
		 */
		void decr() {
			indent--;
		}
		/**
		 * Increment the indent.
		 */
		void incr() {
			indent++;
		}
		/**
		 * Print a line of text with the appropriate indent.
		 *
		 * @param s The String to print.
		 */
		public void println(String s) {
			StringBuffer out = new StringBuffer();
			if (indent>0) {
				for(int i=0; i<indent; i++) {
					out.append("    ");
				}
			}

			out.append(s);
			super.println(out.toString());
		}
		/**
		 * Set the indent back to 0.
		 */
		void reset() {
			indent = 0;
		}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?