io流对数据的读写
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(); } } }