前提:記事本里面一共有605個字
1.使用BufferedReader和FileReader來讀取txt里面的內容,用時相對短。讀完記得關閉流br.close()
2.指定UTF-8輸出格式,使用FileInputStream,InputStreamReaderBufferedReader,時間也是瞬間,讀完記得關閉流isr.close()和bf.close()
3.使用commons.io里面的FileUtils.lineIterator來讀取文件,時間也是一秒。。lineIterator.close()
可到這里下載需要的commons.io文件:https://commons.apache.org/proper/commons-io/download_io.cgi
4將EXAMPLE_TXT_PATH_WRINT的內容寫到EXAMPLE_TXT_PATH_READ文件里面
通過ByteArrayInputStream、ByteArrayOutputStream、TeeInputStream直接對字節來進行讀寫。。
XmlStreamReader來讀取文件的字符格式
api:https://commons.apache.org/proper/commons-io/javadocs/api-2.0.1/index.html?org/apache/commons/io/LineIterator.html
.IOCase判斷某個字符串中是否以另一個字符串結尾,並且區分大小寫
api:https://commons.apache.org/proper/commons-io/javadocs/api-2.0.1/index.html?org/apache/commons/io/LineIterator.html
package content.demo; import org.apache.commons.io.IOCase; import org.junit.Test; import java.io.IOException; /** * Created by 70486 on 2017/6/28 on 21:59. */ public class testDemo3 { @Test public void setStart() throws IOException { String str1 = "This is a new String."; String str2 = "This is another new String, yes!"; //SYSTEM :由當前操作系統確定的區分大小寫的常數。 //checkEndsWith:使用區分大小寫的規則檢查一個字符串是否以另一個字符串結尾。 System.out.println("以字符串結尾(由系統區分大小寫)Ends with string (case sensitive): " + IOCase.SYSTEM .checkEndsWith(str1, "string")); System.out.println("以字符串結尾(由系統區分大小寫)Ends with string (case sensitive): " + IOCase.SYSTEM .checkEndsWith(str2, "yes")); //SENSITIVE:無論操作系統如何,區分大小寫的常數。 //checkEndsWith:使用區分大小寫的規則檢查一個字符串是否以另一個字符串結尾。 System.out.println("以字符串結尾(區分大小寫)Ends with string (case sensitive): " + IOCase.SENSITIVE.checkEndsWith(str1, "string.")); System.out.println("以字符串結尾(區分大小寫)Ends with string (case sensitive): " + IOCase.SENSITIVE.checkEndsWith(str2, "yes!")); //INSENSITIVE:無論操作系統如何,不區分大小寫的常數。 //checkEndsWith:使用區分大小寫的規則檢查一個字符串是否以另一個字符串結尾。 System.out.println("以字符串結尾(不區分大小寫)Ends with string (case insensitive): " + IOCase.INSENSITIVE.checkEndsWith(str1, "string.")); System.out.println("以字符串結尾(不區分大小寫)Ends with string (case insensitive): " + IOCase.INSENSITIVE.checkEndsWith(str2, "yes!")); } }