java讀寫文件常用類

在上面一篇文章裏,我抽出了常用的讀寫方法,貼在這裏,我經常用它來拼寫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) {
				}
			}
		}
	}
}


發佈了25 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章