import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
// 此處亦可使用命令行參數讀入,這裏用stdin讀入
Pattern pattern = Pattern.compile("\\n\\d{1,3}\\b");
while (sc.hasNext()) {
StringBuilder txtin = new StringBuilder();
String path = sc.next();
BufferedReader bfr = null;
try {
bfr = new BufferedReader(new InputStreamReader(new FileInputStream(path), "gbk"));
} catch (UnsupportedEncodingException e) {
System.out.println("不支持該文件的字符 文件路徑:" + path);
} catch (FileNotFoundException e) {
System.out.println("在該路徑下未找到文件 :" + path);
} finally {
if (bfr == null)
continue;
}
while (!bfr.ready()) {
// 沒準備好讀取就阻塞住
}
while (bfr.ready()) {
String temp = bfr.readLine();
txtin.append(temp+'\n');
}
Matcher m=pattern.matcher(txtin);
String result=m.replaceAll(System.getProperty("line.separator"));
String location=("C:\\Users\\51396\\Desktop\\"+new Date().getTime()+".txt");
System.out.println(location);
File file=new File(location);
file.createNewFile();
PrintStream ps=new PrintStream(file);
ps.println(result);
ps.close();
try {
bfr.close();
} catch (IOException e) {
System.out.println("文件關閉失敗");
}
}
sc.close();
}
}
文章內容尚待更新,在Windows下測試完畢,等考完試有空再來博客謝謝