【JAVA】Java文件的读写

1. 写文件(PrintWriter)

读与写都要有 File 关键字的配合,如下例:

File file = new File(filepath/name);
if (file.exists()) {
<span style="white-space:pre">	</span>file.delete();
<span style="white-space:pre">	</span>file.createNewFile();
}
else
<span style="white-space:pre">	</span>file.createNewFile();
PrintWriter output = new PrintWriter(file);
StringBuilder sb = new StringBuilder(); 
sb = "This is a test."
output.println(sb);
output.close();

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">注意 StringBuilder 和 StringBuffer 是差不多的,另外,文件打开后一定配合关闭操作。另,Java 包含 I/O 操作要强制加上 throw Exception ,否则会报错。</span>

2.读文件(Scanner)

File file = new File(filepath/name);
Scanner input = new Scanner(file);
StringBuilder Str = new StringBuilder();
while (input.hasNext()) {
  Str.append(input.NextLine)
}
input.close();

3.例子(读Java文件并写入html文件)

Java文件:

// This application displays welcome to JAVA!
public class Welcome {
	public static void main(String[] args) {
		System.out.println("Welome to JAVA!!!");
	}
}
处理程序:

package Test;

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class Ex10 {
	public static void main(String[] args) throws Exception {
//		String string = "//";
//		String string2 = "//";
//		System.out.println(string.equals("//"));
		File readFile = new File("Welcome.java");
		Scanner input = new Scanner(readFile);
		
		StringBuilder output = new StringBuilder();
		output.append("<html>");
		output.append("<head>");
		output.append("<title>This is a test.</title>");
		output.append("<meta http-equiv='Content-Type' content='text/html; charset=utf-8 /'>");
		output.append("<style>");
		output.append(".zhushi{color: grey;}");
		output.append(".guanjianzi{color: red;}");
		output.append(".hanshu{color: blue;}");
		output.append("</style></head>");
		output.append("<body>");
		
		while (input.hasNextLine()) {
			String buffer = input.nextLine();
			String[] strings = buffer.split("[ \n\t\r;,.:?!{()]");
			output.append("<p>");
			for (int i = 0; i < strings.length; i++) {
				System.out.println(strings[i]);
				if (strings[i].equals("//")) {
					output.delete(output.length() - 4, output.length() - 1);
					output.append("<p class='zhushi'>");
				}
				else if (strings[i].equals("public") || strings[i].equals("class")
						|| strings[i].equals("void")) {
					output.append(" <span class='guanjianzi'>" + strings[i] + "</span> ");
				}
				else if (strings[i].equals("main") || strings[i].equals("Wellcome")) {
					output.append(" <span class='hanshu'>" + strings[i] + "</span> ");
				}
				else {
					output.append(" " + strings[i] + " ");
				}
			}
			output.append("</p>");
		}
		output.append("</body></html>");
		input.close();
		
		File writeFlie = new File("Welcome.html");
		if (writeFlie.exists()) {
			writeFlie.delete();
			writeFlie.createNewFile();
		}else {
			writeFlie.createNewFile();
		}
		PrintWriter wriFile = new PrintWriter(writeFlie);
		wriFile.print(output);
		wriFile.close();
		System.out.println("Done");
	}
}
处理结果:





你可能感兴趣的:(【JAVA】Java文件的读写)