java读写文件常用类

来源:互联网 发布:通用接口软件 编辑:程序博客网 时间:2024/06/02 17:35

在上面一篇文章里,我抽出了常用的读写方法,贴在这里,我经常用它来拼写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) {}}}}}


原创粉丝点击