FileWriter和FileReader使用
package com.main.test; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileWriterTest { static FileWriter fw; static FileReader fr; public static void main(String[] args) throws IOException { // TODO Auto-generated method stub // fileWriter(); //FileReaderDemo(); //whileFileReader(); //whileFileReader2(); // shuZuReadDemo(); } private static void shuZuReadDemo() throws FileNotFoundException, IOException { fr = new FileReader("c:\田藩.txt"); char[] ch = new char[3]; fr.read(ch); for(int i= 0 ;i<ch.length;i++){ System.out.println(ch[i]+""); } } private static void whileFileReader2() throws FileNotFoundException, IOException { fr = new FileReader("c:\田藩.txt"); int ch =0; while((ch=fr.read())!=-1){ System.out.println((char)ch); } } private static void whileFileReader() throws FileNotFoundException, IOException { fr = new FileReader("c:\田藩.txt"); while(true){ int ch = fr.read(); if(ch == -1){ break; } System.out.println((char)ch); } fr.close(); } private static void FileReaderDemo() throws FileNotFoundException, IOException { fr = new FileReader("c:\田藩.txt"); int ch =fr.read(); int ch2 =fr.read(); System.out.println((char)ch); System.out.println((char)ch2); } private static void fileWriter() throws IOException { try { fw = new FileWriter("c:\田藩.txt",true); fw.write("#eoeoeo"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ fw.close(); } } }