最近寫一些網(wǎng)頁撬统,這些網(wǎng)頁里面有部分內(nèi)容是相同的巩剖,網(wǎng)頁文件名要求也是有規(guī)律的铝穷。一看這些需求就覺得可以用程序?qū)崿F(xiàn)。一般做法是重復(fù)復(fù)制粘貼的動作佳魔,但是文件多起來就有點煩了曙聂。所以要用程序解放生產(chǎn)力。
程序代碼
public class NewFile {
private static int range1;
private static int range2;
private static int len;
static String fname;
static String ename;
static String writestr="<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>Title</title>\n" +
"</head>\n" +
"<body>\n" +
"\n" +
"</body>\n" +
"</html>";
public static void main(String[] args) throws IOException {
System.out.println("請輸入文件前綴名字:");
Scanner sc=new Scanner(System.in);
fname=sc.next();
System.out.println("請輸入范圍,空格分開");//
range1=sc.nextInt();
range2=sc.nextInt();
System.out.println("請輸入文件后綴名");
ename=sc.next();
len=range2-range1; //文件個數(shù)
List<String> namelist;
while (len<0) {
System.out.println("范圍不合法鞠鲜,請重新輸入:");
range1 = sc.nextInt();
range2 = sc.nextInt();
len = range2 - range1;
}
namelist=getfilelistname();
for (String name:namelist) {
File file=new File(name);
file.createNewFile();
writesome(file,writestr);
System.out.println(name);
}
}
public static List<String> getfilelistname(){
List<String> filelistname;
filelistname=new ArrayList<>();
String filename;
for (int i = range1; i <=range2; i++) {
filename=fname+i+"."+ename;
filelistname.add(filename);
}
return filelistname;
}
public static void writesome(File file,String str){
try {
FileWriter fileWriter=new FileWriter(file);
fileWriter.write(str);
fileWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
運行結(jié)果: