簡體   English   中英

如何在Java中讀取和拆分txt文件的內容?

[英]How to read and split the contents of txt file in Java?

我想閱讀文件的全文,然后根據特定的分隔符將其拆分。 我怎么能在Java中這樣做?

try{
  // Open the file
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {
  // split the line on your splitter(s)
     String[] splitted = strLine.split("-"); // here - is used as the delimiter
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }

閱讀內容。 選擇要為其檢查每個行/單詞或短語以進行拆分的分隔符。 有一個開關盒設置。 無論您根據開關案例找到什么內容,都可以執行操作。 假設您可以編寫不同案例的不同文本文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM