1、读取Classpath中的文本文件
ClassLoader classLoader = getClass().getClassLoader(); InputStream inputStream = classLoader.getResourceAsStream("fileTest.txt"); StringBuilder resultStringBuilder = new StringBuilder(); try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) { String line; while ((line = br.readLine()) != null) { resultStringBuilder.append(line).append("\n"); } } catch (IOException e) { e.printStackTrace(); } String result = resultStringBuilder.toString();
2、使用commons-io读取文件
将File对象传递给FileUtils类的readFileToString()方法。commons-io程序类管理加载内容。
ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("fileTest.txt").getFile()); String data = FileUtils.readFileToString(file, "UTF-8");
3、使用BufferedReader读取文件
String file ="src/test/resources/fileTest.txt"; BufferedReader reader = new BufferedReader(new FileReader(file)); String currentLine = reader.readLine(); reader.close();
4、读取小文件和大文件
可以使用新的Files类读取小文件,如果我们想用Files
类读取大文件,可以使用BufferedReader
。
例如,
下面的代码使用新的Files类和BufferedReader读取文件:
//小文件 Path path = Paths.get("src/test/resources/fileTest.txt"); String read = Files.readAllLines(path).get(0); //大文件 Path path = Paths.get("src/test/resources/fileTest.txt"); BufferedReader reader = Files.newBufferedReader(path); String line = reader.readLine();
5、使用Files.lines()读取文件
JDK8在Files
类中提供了lines()
方法。它返回一个String
元素的Stream
。
Path path = Paths.get(getClass().getClassLoader() .getResource("fileTest.txt").toURI()); Stream<String> lines = Files.lines(path); String data = lines.collect(Collectors.joining("\n")); lines.close();
6、使用DataInputStream读取文件
可以使用DataInputStream
从文件中读取二进制或原始数据类型。
String file ="src/test/resources/fileTest.txt"; String result = null; DataInputStream reader = new DataInputStream(new FileInputStream(file)); int nBytesToRead = reader.available(); if(nBytesToRead > 0) { byte[] bytes = new byte[nBytesToRead]; reader.read(bytes); result = new String(bytes); }
7、使用BufferedReader读取文件
读取一个大文件,FileChannel
可以比标准IO更快。
String result = null; String file = "src/test/resources/fileTest.txt"; RandomAccessFile reader = new RandomAccessFile(file, "r"); FileChannel channel = reader.getChannel(); int bufferSize = 1024; if (bufferSize > channel.size()) { bufferSize = (int) channel.size(); } ByteBuffer buff = ByteBuffer.allocate(bufferSize); channel.read(buff); buff.flip(); String result =new String(buff.array()); channel.close(); reader.close();
8、读取UTF-8编码文件
使用BufferedReader可以读取UTF-8编码的文件。
例如,
String file = "src/test/resources/fileTestUtf8.txt"; BufferedReader reader = new BufferedReader (new InputStreamReader(new FileInputStream(file), "UTF-8")); String currentLine = reader.readLine(); reader.close();
9、读取Jar中文件
String jarName="D://cjavapy.jar";//Jar包所在的位置 String fileName="com/cjavapy/path/java.txt";//文件在jar包里的路径 JarFile jarFile = new JarFile(jarName);//读入jar文件 JarEntry entry = jarFile.getJarEntry(fileName); InputStream input = jarFile.getInputStream(entry);//读入需要的文件 StringBuilder resultStringBuilder = new StringBuilder(); try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) { String line; while ((line = br.readLine()) != null) { resultStringBuilder.append(line).append("\n"); } } catch (IOException e) { e.printStackTrace(); } jarFile.close(); String result = resultStringBuilder.toString();