读取文件
1.传统IO文件读取方式
package com.lyj.test2;
import java.io.*;
/**
* Created by LYJ on 2016/10/9.
* 传统读取文件的方式(按行读取)
*/
public class TestIO {
public static void main(String... args) {
String filePath="E:\\JAVA\\javaPrj\\TestNIOStream1\\resources\\qq.txt";
String encoding = "utf-8";
try {
File file = new File(filePath);
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt + " --------- lineTxt --- TestIO.main\n");
}
read.close();
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
}catch (IOException e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
}
2.通过java NIO读取文件
package com.lyj.test2;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
/**
* Created by LYJ on 2016/10/8.
* 读写文件一般都使用该方法
*
* 分配空间(ByteBuffer buf = ByteBuffer.allocate(1024); 还有一种allocateDirector后面再陈述)
* 写入数据到Buffer(int bytesRead = fileChannel.read(buf);)
* 调用filp()方法( buf.flip();)
* 从Buffer中读取数据(System.out.print((char)buf.get());)
* 调用clear()方法或者compact()方法
*
*
*/
public class TestNIO
{
public static void main(String... args){
Charset charset=Charset.defaultCharset();
CharsetDecoder decoder=charset.newDecoder();
RandomAccessFile accessFile= null;
ByteBuffer buf= null;
CharBuffer charBuffer= null;
int byteRead= 0;
String path="E:\\JAVA\\javaPrj\\TestNIOStream1\\resources\\qq.txt";
try {
accessFile = new RandomAccessFile(path,"rw");
FileChannel channel = accessFile.getChannel();
buf = ByteBuffer.allocate((int)accessFile.length());
charBuffer = CharBuffer.allocate((int)channel.size());
byteRead = channel.read(buf);
while (byteRead!=-1){
buf.flip();
decoder.decode(buf,charBuffer,false);
charBuffer.flip();
while (charBuffer.hasRemaining()){
System.out.print(charBuffer.get());
}
System.out.println();
buf.compact();
charBuffer.compact();
try {
byteRead=channel.read(buf);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
}catch (IOException e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}finally {
try {
if (accessFile!=null){
accessFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.通过java8的StreamAPI读取文本文件
package com.lyj.test2
import java.io.IOException
import java.nio.charset.Charset
import java.nio.file.Files
import java.nio.file.NoSuchFileException
import java.nio.file.Path
import java.nio.file.Paths
import java.util.stream.Stream
public class TestStream {
public static void main(String... args){
//定义路径
Path path = Paths.get("E:\\JAVA\\javaPrj\\TestNIOStream1\\resources\\qq.txt")
String content=""
//通过Files.lines 读取 返回 Stream<Stream>
try (Stream<String> lines = Files.lines(path, Charset.defaultCharset())) {
//使用StreamAPI的归约操作 reduce(初始值,BinaryOperator<T>)
//BinaryOperator<T> (T,T)->T 将两个元素结合起来产生一个新值
//这里我们用的是lambda表达式 (a,b)->a+b
content=lines.reduce("",(line1,line2)->line1+"\n"+line2)
} catch (NoSuchFileException e) {
System.out.println("找不到指定文件")
}catch (IOException e) {
System.out.println("读取文件出错!")
e.printStackTrace()
}
System.out.println(content + " --------- content --- TestStream.main\n")
}
}