11、java读取文件的几种方式


package com.tij.io.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;

/**
* 读取文件
* @author guoyoujun
* @date 2014-3-17
*/
public class JavaReadFile {

/**
* 工作中涉及到文件的时候,有时候我们需要读取阅读它, 现在提供几种不同的方式去读取;
* <p>java.nio.file.Files:这是java7中使用的类库,这个类可以读取所有的内容到数组或者读取所有的行到一个列表
* <p>java.io.FileReader:可以通过FileReader得到BufferRead从而读取文件的每一行,不过FileReader不支持格式编码所以并不是一种好的方式
* <p>java.io.BufferedReader:这是个很好的处理文件读取的类,支持编码、线程安全、默认缓存为8KB
* <p>java.util.Scanner:使用这个扫描类可以根据一些正则表达式来读取相应的内容, 默认的是分割内容是空白,是不同步的非线程安全的类
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String fileName = "/Users/GYJ/webdefault.xml";
readUsingFileReader(fileName);
System.out.println("\n");
readUsingBufferedReader(fileName);
System.out.println("\n");
readUsingBufferedReader(fileName, StandardCharsets.UTF_8);
System.out.println("\n");
readUsingBufferedReaderJava7(fileName, StandardCharsets.UTF_8);
System.out.println("\n");
readUsingFiles(fileName);
System.out.println("\n");
readUsingScanner(fileName);

}

/**
* FileReader读取
* @param fileName
* @throws IOException
*/
private static void readUsingFileReader(String fileName) throws IOException {
File file = new File(fileName);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println("read result = " + line);
}
br.close();
fr.close();
}

/**
* BufferedReader读取(支持编码格式)
* @param fileName
* @param charsetName
* @throws IOException
*/
private static void readUsingBufferedReader(String fileName, Charset cs) throws IOException {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, cs);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println("read result = " + line);
}
br.close();
}

/**
* BufferedReaderd读取文件
* @param fileName
* @param cs
* @throws IOException
*/
private static void readUsingBufferedReaderJava7(String fileName, Charset cs) throws IOException {
Path path = Paths.get(fileName);
BufferedReader br = Files.newBufferedReader(path, cs);
String line;
while((line = br.readLine()) != null){
System.out.println("read result = " + line);
}
br.close();
}

/**
* BufferedReader读取
* @param fileName
* @throws IOException
*/
private static void readUsingBufferedReader(String fileName) throws IOException {
File file = new File(fileName);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println("read result = " + line);
}
br.close();
fr.close();
}

/**
* 扫描读取文件
* @param fileNme
* @throws IOException
*/
private static void readUsingScanner(String fileName) throws IOException {
Path path = Paths.get(fileName);
Scanner scanner = new Scanner(path);
while(scanner.hasNextLine()){
String line = scanner.nextLine();
System.out.println("read result = " + line);
}
}

/**
* 读取文件
* @param fileName
* @throws IOException
*/
private static void readUsingFiles(String fileName) throws IOException {
Path path = Paths.get(fileName);
byte[] bytes = Files.readAllBytes(path);
List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
System.out.println(bytes.length);
System.out.println(allLines.size());
}

}


//因为读取的是同一份文件输出结果就给出一遍的输出结果;
out put============
read result = <!-- dumped to a file? [false] -->
read result = <!-- False if suppressSmap is true -->
read result = <!-- -->
read result = <!-- scratchdir What scratch directory should we use when -->
read result = <!-- compiling JSP pages? [default work directory -->
read result = <!-- for the current web application] -->
read result = <!-- -->
read result = <!-- tagpoolMaxSize The maximum tag handler pool size [5] -->
read result = <!-- -->
read result = <!-- xpoweredBy Determines whether X-Powered-By response -->
read result = <!-- header is added by generated servlet [false] -->
read result = <!-- -->
read result = <!-- If you wish to use Jikes to compile JSP pages: -->
read result = <!-- Set the init parameter "compiler" to "jikes". Define -->
read result = <!-- the property "-Dbuild.compiler.emacs=true" when starting Jetty -->
read result = <!-- to cause Jikes to emit error messages in a format compatible with -->
read result = <!-- Jasper. -->
read result = <!-- If you get an error reporting that jikes can't use UTF-8 encoding, -->
read result = <!-- try setting the init parameter "javaEncoding" to "ISO-8859-1". -->
read result = <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
read result = <servlet
read result = id="jsp"
read result = >
read result = <servlet-name>jsp</servlet-name>
read result = <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
read result = <init-param>
read result = <param-name>logVerbosityLevel</param-name>
read result = <param-value>DEBUG</param-value>
read result = </init-param>
read result = <init-param>
read result = <param-name>fork</param-name>
read result = <param-value>false</param-value>
read result = </init-param>
read result = <init-param>
read result = <param-name>xpoweredBy</param-name>
read result = <param-value>false</param-value>
read result = </init-param>
read result = <!--
read result = <init-param>
read result = <param-name>classpath</param-name>
read result = <param-value>?</param-value>
read result = </init-param>
read result = -->
read result = <load-on-startup>0</load-on-startup>
read result = </servlet>
read result =
read result = <servlet-mapping>
read result = <servlet-name>jsp</servlet-name>
read result = <url-pattern>*.jsp</url-pattern>
read result = <url-pattern>*.jspf</url-pattern>
read result = <url-pattern>*.jspx</url-pattern>
read result = <url-pattern>*.xsp</url-pattern>
read result = <url-pattern>*.JSP</url-pattern>
read result = <url-pattern>*.JSPF</url-pattern>
read result = <url-pattern>*.JSPX</url-pattern>
read result = <url-pattern>*.XSP</url-pattern>
read result = </servlet-mapping>
read result =
read result = <!-- ==================================================================== -->
read result = <!-- Dynamic Servlet Invoker. -->
read result = <!-- This servlet invokes anonymous servlets that have not been defined -->
read result = <!-- in the web.xml or by other means. The first element of the pathInfo -->
read result = <!-- of a request passed to the envoker is treated as a servlet name for -->
read result = <!-- an existing servlet, or as a class name of a new servlet. -->
read result = <!-- This servlet is normally mapped to /servlet/* -->
read result = <!-- This servlet support the following initParams: -->
read result = <!-- -->
read result = <!-- nonContextServlets If false, the invoker can only load -->
read result = <!-- servlets from the contexts classloader. -->
read result = <!-- This is false by default and setting this -->
read result = <!-- to true may have security implications. -->
read result = <!-- -->
read result = <!-- verbose If true, log dynamic loads -->
read result = <!-- -->
read result = <!-- * All other parameters are copied to the -->
read result = <!-- each dynamic servlet as init parameters -->
read result = <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
read result = <!--
read result = Uncomment for dynamic invocation <servlet> <servlet-name>invoker</servlet-name> <servlet-class>org.eclipse.jetty.servlet.Invoker</servlet-class> <init-param> <param-name>verbose</param-name>
read result = <param-value>false</param-value> </init-param> <init-param> <param-name>nonContextServlets</param-name> <param-value>false</param-value> </init-param> <init-param>
read result = <param-name>dynamicParam</param-name> <param-value>anyValue</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>invoker</servlet-name>
read result = <url-pattern>/servlet/*</url-pattern> </servlet-mapping>
read result = -->
read result =
read result =
read result =
read result = <!-- ==================================================================== -->
read result = <session-config>
read result = <session-timeout>30</session-timeout>
read result = </session-config>
read result =
read result = <!-- ==================================================================== -->
read result = <!-- Default MIME mappings -->
read result = <!-- The default MIME mappings are provided by the mime.properties -->
read result = <!-- resource in the org.eclipse.jetty.server.jar file. Additional or modified -->
read result = <!-- mappings may be specified here -->
read result = <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
read result = <!-- UNCOMMENT TO ACTIVATE
read result = <mime-mapping>
read result = <extension>mysuffix</extension>
read result = <mime-type>mymime/type</mime-type>
read result = </mime-mapping>
read result = -->
read result =
read result = <!-- ==================================================================== -->
read result = <welcome-file-list>
read result = <welcome-file>index.html</welcome-file>
read result = <welcome-file>index.htm</welcome-file>
read result = <welcome-file>index.jsp</welcome-file>
read result = </welcome-file-list>
read result =
read result = <!-- ==================================================================== -->
read result = <locale-encoding-mapping-list>
read result = <locale-encoding-mapping>
read result = <locale>ar</locale>
read result = <encoding>ISO-8859-6</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>be</locale>
read result = <encoding>ISO-8859-5</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>bg</locale>
read result = <encoding>ISO-8859-5</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>ca</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>cs</locale>
read result = <encoding>ISO-8859-2</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>da</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>de</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>el</locale>
read result = <encoding>ISO-8859-7</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>en</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>es</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>et</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>fi</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>fr</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>hr</locale>
read result = <encoding>ISO-8859-2</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>hu</locale>
read result = <encoding>ISO-8859-2</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>is</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>it</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>iw</locale>
read result = <encoding>ISO-8859-8</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>ja</locale>
read result = <encoding>Shift_JIS</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>ko</locale>
read result = <encoding>EUC-KR</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>lt</locale>
read result = <encoding>ISO-8859-2</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>lv</locale>
read result = <encoding>ISO-8859-2</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>mk</locale>
read result = <encoding>ISO-8859-5</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>nl</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>no</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>pl</locale>
read result = <encoding>ISO-8859-2</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>pt</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>ro</locale>
read result = <encoding>ISO-8859-2</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>ru</locale>
read result = <encoding>ISO-8859-5</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>sh</locale>
read result = <encoding>ISO-8859-5</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>sk</locale>
read result = <encoding>ISO-8859-2</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>sl</locale>
read result = <encoding>ISO-8859-2</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>sq</locale>
read result = <encoding>ISO-8859-2</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>sr</locale>
read result = <encoding>ISO-8859-5</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>sv</locale>
read result = <encoding>ISO-8859-1</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>tr</locale>
read result = <encoding>ISO-8859-9</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>uk</locale>
read result = <encoding>ISO-8859-5</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>zh</locale>
read result = <encoding>GB2312</encoding>
read result = </locale-encoding-mapping>
read result = <locale-encoding-mapping>
read result = <locale>zh_TW</locale>
read result = <encoding>Big5</encoding>
read result = </locale-encoding-mapping>
read result = </locale-encoding-mapping-list>
read result =
read result = <security-constraint>
read result = <web-resource-collection>
read result = <web-resource-name>Disable TRACE</web-resource-name>
read result = <url-pattern>/</url-pattern>
read result = <http-method>TRACE</http-method>
read result = </web-resource-collection>
read result = <auth-constraint/>
read result = </security-constraint>
read result =
read result = </web-app>
read result =
=====================================================================
使用哪一种方式去读取文件取决于项目需求,希望能用到的同学自己决定;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值