在上面一篇文章裏,我抽出了常用的讀寫方法,貼在這裏,我經常用它來拼寫sql語句,按行讀,然後輸出到文件.
public class FileTools {
static List<String> list = new ArrayList<String>();
public static void main(String[] args) throws IOException {
readFileByLines("/tmp/os");
System.out.println(list);
out();
System.out.println("ok");
}
private static void out() throws IOException {
FileWriter fileWriter = new FileWriter(new File("/tmp/os1"));
for (String str : list) {
fileWriter.append(str);
}
fileWriter.flush();
fileWriter.close();
}
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
list.add("insert into mobilebrandname ( cnbrand ) values ( '"+tempString.trim()+"' );\n");
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
}