想法由来:有时查看网页源代码的css文件内容,竟是恼人的压缩后代码(不换行),如下图所示——
它的可读性很差,所以写了下面这个简单的程序,实现自动换行。
适用条件:遇到指定字符换行(本例中遇到‘}’换行)。
源代码:
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class test {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(new File("C:\\Users\\hehe\\Desktop\\source.txt"));
//读取该地址下的input.txt文件
PrintWriter out = new PrintWriter(new File("C:\\Users\\hehe\\Desktop\\result.txt"));
//将处理后的文件output.txt创建到该地址下
while(in.hasNext()){
String str = in.nextLine();
//按行读取,遇到换行符停止。将读取到的内容赋值到str中
str.replace(" ","");
//慎用!将文本中的空格去掉,本例操作的是css代码,单句中的空格非必要,故可去掉
String []ss = str.spl