Java读取与写入文件
import java.io.*;/** * 文件另存为的编码要选择UTF-8 */public class RWFile { /** * @param path 文件的路径 */ public void wFile(String path) { FileWriter fw = null; try { File file = new File(path); String content[] = {"englishchines","中国","烟台"}; fw = new FileWriter(file,true);//写入文件尾 BufferedWriter bufw = new BufferedWriter(fw); for(int i =0 ;i<content.length;i++) { bufw.write(content[i]); bufw.newLine(); } bufw.flush(); bufw.close(); fw.close(); } catch (IOException ex) { System.out.println("异常"); } finally { try { fw.close(); } catch (IOException ex) { System.out.println("异常"); } } } /** * @param path 文件的路径 */ public void rFile(String path){ FileReader fr = null; try { File file = new File(path); fr = new FileReader(file); BufferedReader bufr = new BufferedReader(fr); String s = null; int i =0; while((s=bufr.readLine())!=null) { i++; System.out.println("第"+i+"行:"+s); } bufr.close(); fr.close(); } catch (IOException ex) { System.out.println("异常"); } finally { try { fr.close(); } catch (IOException ex) { System.out.println("异常"); } } } public static void main(String[] args) { new RWFile().rFile("readme.txt"); new RWFile().wFile("word.txt"); }}