io读取文件内容 Java
程序员文章站
2024-02-26 08:56:22
...
读取某个文件的内容
相同的效果;
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 block e.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(); }
上一篇: Json读写本地文件实现代码