java的I/O操作

  关于java中读写文件的几种情况的代码:

一.读取本地文件为字符串

    File file = new File("D://test.txt");                 //文件路径

    BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream));

    StringBuffer strBuffer = new StringBuffer("");

    String strLine = bufferedReader.readLine();

    while(strLine!=null){

        strBuffer.append(strLine);

        strLine = bufferedReader.readLine();

    }

二.读取网络文件为字符串

    java.net.URL url = new URL(http://www.sohu.com/home.html);   

    java.net.HttpUrlConnection connection= (HttpUrlConnection)url.openConnection();

    connection.connect();

    InputStream inputStream = connection.getInputStream();

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

    StringBuffer strBuffer = new StringBuffer("");

    String strLine = bufferedReader.readLine();

    while(strLine!=null){

        strBuffer.append(strLine);

        strLine = bufferedReader.readLine();

    }

 

阅读更多
个人分类: java
上一篇dojo框架中ajax与struts的完美结合
下一篇poi 操作excel , 合并单元格后加边框
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭