import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; public class Iotetst { private String fileName="ioTest.txt"; private String basePath="D:/TestFile"; private String writeFileName="write.txt"; private String charset="gbk"; public static void main(String[] args) { Iotetst iotest = new Iotetst(); //測試InputStreamReader //iotest.readFile(); //測試BufferedReader // iotest.buff(); //測試寫OutputStreamWrite // iotest.writeFile(); // 測試BufferedWriter iotest.buffw(); } /** * read */ public void readFile(){ String AllPath=basePath+File.separatorChar+fileName; System.out.println(AllPath); try { //讀取位元組轉換成字元 FileInputStream fileInputStream = new FileInputStream(new File(AllPath)); InputStreamReader streamReader = new InputStreamReader(fileInputStream,charset); // StringBuffer sb=new StringBuffer(); char[] buff=new char[1024]; int count=0; try { while((count=streamReader.read(buff))!=-1){ sb.append(buff,0,count); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { System.out.println(buff); streamReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //BufferedReader @SuppressWarnings("resource") public void buff(){ String AllPath=basePath+File.separatorChar+fileName; try { FileInputStream inputStream = new FileInputStream(new File(AllPath)); BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream, charset)); String sb ; while((sb=bufferedReader.readLine())!=null){ System.out.println(sb); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * write */ public void writeFile(){ String writeToPath=basePath+File.separatorChar+writeFileName; try { FileOutputStream fileOutputStream = new FileOutputStream(new File(writeToPath)); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,charset); outputStreamWriter.write("i love you and i wait for you and i miss you ! where are you?"); outputStreamWriter.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void buffw(){ String writeToPath=basePath+File.separatorChar+writeFileName; try{ FileOutputStream fileOutputStream = new FileOutputStream(new File(writeToPath)); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream); BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); bufferedWriter.write("中式敵眾我寡實打實大啊啊奧術大師阿斯達奧術大師阿斯達"); bufferedWriter.close(); }catch(Exception e){ e.printStackTrace(); } } }