读书人

io读取资料内容

发布时间: 2012-12-24 10:43:14 作者: rapoo

io读取文件内容
读取某个文件的内容

File file=new File("D:\\t.txt");try {InputStream in=new FileInputStream(file);BufferedReader  bf=new BufferedReader(new InputStreamReader(in));String str="";String line="";while((str=bf.readLine())!=null){System.out.println(str);line +=str;}System.out.println(line);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}


相同的效果;
public String readData(String url) { //     String url = "C:/data.txt";     StringBuffer sb = new StringBuffer();     try {          FileReader read = new FileReader(new File(url));          BufferedReader br=new BufferedReader(read);          String line;          while((line=br.readLine())!=null){                      sb.append(line);             sb.append("\n");                            }              } catch (FileNotFoundException e) {           e.printStackTrace();      } catch (IOException e) {         e.printStackTrace();      }      return sb.toString(); } 

读书人网 >编程

热点推荐