读取365.txt 判断每行第一个字符为数字就写入 e365.txt 中,否则写入c365.txt中
我用的是 startswith ,但太麻烦,写的太长,有没更简单的办法import java.io.*;
public class readfile { public static void main(String[] args) {
try {
File read = new File ("365.txt");
File write1 = new File("e365.txt");
File write2 = new File("c365.txt");
BufferedReader br = new BufferedReader(new FileReader(read));
BufferedWriter bw1 = new BufferedWriter(new FileWriter(write1));
BufferedWriter bw2 = new BufferedWriter(new FileWriter(write2));
String temp = null;
temp = br.readLine();
while (temp != null ) {
if (temp.startsWith("0")||
temp.startsWith("1")||
temp.startsWith("2")||
temp.startsWith("3")||
temp.startsWith("4")||
temp.startsWith("5")||
temp.startsWith("6")||
temp.startsWith("7")||
temp.startsWith("8")||
temp.startsWith("9")
) {
bw1.write(temp + "\r\n");
} else {
if (temp.length() != 0) {
bw2.write(temp + "\r\n");
}
}
temp = br.readLine();
}
bw1.close();
bw2.close();
br.close();
}catch(FileNotFoundException e){ //文件未找到
System.out.println (e);
}catch(IOException e){
System.out.println (e);
}
}
}
我用的是 startswith ,但太麻烦,写的太长,有没更简单的办法import java.io.*;
public class readfile { public static void main(String[] args) {
try {
File read = new File ("365.txt");
File write1 = new File("e365.txt");
File write2 = new File("c365.txt");
BufferedReader br = new BufferedReader(new FileReader(read));
BufferedWriter bw1 = new BufferedWriter(new FileWriter(write1));
BufferedWriter bw2 = new BufferedWriter(new FileWriter(write2));
String temp = null;
temp = br.readLine();
while (temp != null ) {
if (temp.startsWith("0")||
temp.startsWith("1")||
temp.startsWith("2")||
temp.startsWith("3")||
temp.startsWith("4")||
temp.startsWith("5")||
temp.startsWith("6")||
temp.startsWith("7")||
temp.startsWith("8")||
temp.startsWith("9")
) {
bw1.write(temp + "\r\n");
} else {
if (temp.length() != 0) {
bw2.write(temp + "\r\n");
}
}
temp = br.readLine();
}
bw1.close();
bw2.close();
br.close();
}catch(FileNotFoundException e){ //文件未找到
System.out.println (e);
}catch(IOException e){
System.out.println (e);
}
}
}
....