读取磁盘文件txt,并输入String

public static void main(String[] args) throws IOException {

 

 String fileContent = readFileContent("d:/aaa.txt");

 

 System.out.println(fileContent);

  

}

 

            //参数string为你的文件名

private static String readFileContent(String fileName) throws IOException {

 

 File file = new File(fileName);

 

 BufferedReader bf = new BufferedReader(new FileReader(file));

 

 String content = "";

 StringBuilder sb = new StringBuilder();

 

 while(content != null){

  content = bf.readLine();

  

  if(content == null){

   break;

  }

  

  sb.append(content.trim());

 }

 

bf.close();

 return sb.toString();

}

你可能感兴趣的:(String)