例題讀入字串,包括換行,然後用#f分割字串

anhuibozhoushatu發表於2019-01-11
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;



public class ReadStrug {
	public static void main(String args[]) {
		String str=new String(";\\.*");
		StringBuffer res=new StringBuffer();
		int cnt=0;
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		while(true) {
			try {
				char ch=(char)(in.read());
				if(ch==str.charAt(cnt) && cnt==3) {
					int len=res.length();
					res.deleteCharAt(len-1);
					res.deleteCharAt(len-2);
					res.deleteCharAt(len-3);
					break;
				}else if(ch==str.charAt(cnt)) {
					res.append(ch);
					cnt++;
				}
				else {
					res.append(ch);
					cnt=0;
				}
				}catch(Exception e) {
				e.printStackTrace();
			}
			
		}
		String str2[]=res.toString().split("#");
		Arrays.sort(str2);
		try {
			File file=new File("data.txt");
			BufferedWriter out=new BufferedWriter(new FileWriter(file,true));
			for(int i=0;i<str2.length;i++) {
				out.write(str2[i]);
				out.newLine();
				out.flush();
				}
			out.flush();
			out.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		Arrays.sort(str2, new Comparator<String>() {
			public int compare(String s1,String s2) {
				return s1.length()-s2.length();
			}
		});
		System.out.println(str2[0]);
		System.out.println(str2[str2.length-1]);
		System.out.println(str2.length);
	}
}

 

相關文章