1. 程式人生 > 實用技巧 >後端呼叫其他人提供的介面,把得到的JSON字串資料反序列化成物件或陣列

後端呼叫其他人提供的介面,把得到的JSON字串資料反序列化成物件或陣列

這篇文章主要介紹了Java(springboot) 讀取txt文字內容程式碼例項,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

程式碼如下

public class TxtTest {
  private static final Logger logger = LoggerFactory.getLogger(TxtTest.class);
  public static String readTxt(File file) throws IOException {
    String s = "";
    InputStreamReader in 
= new InputStreamReader(new FileInputStream(file),"UTF-8"); BufferedReader br = new BufferedReader(in); StringBuffer content = new StringBuffer(); while ((s=br.readLine())!=null){ content = content.append(s); } return content.toString(); } public static void main(String[] args) {
try {         //通過絕對路徑獲取檔案 String s1 = TxtTest.readTxt(new File("C:\\Users\\....\\du.txt")); logger.info(s1);         //spring boot中檔案直接放在resources目錄下 String s2 = TxtTest.readTxt(ResourceUtils.getFile("classpath:du.txt")); logger.info(s2); } catch (IOException e) { e.printStackTrace(); } } }

轉載於:https://www.jb51.net/article/180411.htm