您的位置:首页 > 编程语言 > Java开发

java读写文件的几种方式(不断完善),方便日后使用

2016-08-03 12:04 597 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">一、读文件</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1、递归方式获取文件夹下所有文件路径</span>


/**
* 获取文件夹下所有文件的路径
* @param folder
* @return
*/
public static void getFilePath(String folder){

File files = new File(folder);
if(files.isFile()){
String parent = files.getParent();
int i = parent.lastIndexOf("\\");
if("url_worlds".equals(parent.substring(i+1))) {
pathList.add(folder);
}
}else {
File[] files1 = files.listFiles();
for(File file : files1){
getFilePath(file.getPath());
}
}
}
2、读文件的几种方式
2.1、读取小文件

public static List<String> readFiles(String filePath){
List<String> textList = new ArrayList<String>();
try {
String encoding="utf-8";
File file=new File(filePath);
if(file.isFile() && file.exists()){
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
textList.add(lineTxt);
}
read.close();
}else{
System.out.println("文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
}
return textList;
}


2.2 读取大文件(G级以上)
使用 RandomAccessFile
public static void largeRead(String inFile, String outFile){
RandomAccessFile read = null;
try {
read = new RandomAccessFile(inFile,"r");
RandomAccessFile writer = new RandomAccessFile(outFile,"rw");
byte[] b = new byte[200*1024*1024];
while(read.read(b)!=-1){

writer.write(b);
}
writer.close();
read.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}


<pre name="code" class="html">使用 LineIterator


public static void largeRead1(String inFile, String outFile){

try {
LineIterator li = FileUtils.lineIterator(new File(inFile), "utf-8");
FileWriter fw = new FileWriter(outFile,true);
BufferedWriter bw = new BufferedWriter(fw);
while (li.hasNext()){
String line = li.nextLine();
String[] split = line.split("\t");
if(split.length > 1){
//写入outFile
bw.write(line);
bw.newLine();
bw.flush();
count++;
}
}
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
}
}


二、写文件

常用方法:

public static void writeFile(String destinationPath, String line) throws Exception {
FileWriter fileWriter = new FileWriter(destinationPath,true);
BufferedWriter writer = new BufferedWriter(fileWriter);
writer.write(line);
writer.newLine();
writer.flush();

writer.close();
fileWriter.close();
}

三、多个文件合并为一个文件
3.1 普通方式(小文件)

先读出文件的内容,然后以追加的方式写入新的文件中,结合以上的读和写即可实现。

3.2 快速合并

public static void fastMerge(String outFile){
FileChannel outChannel = null;
try {
outChannel = new FileOutputStream(outFile).getChannel();
for (String f : pathList){
System.out.println("Merge " + f + " into " + outFile);
FileChannel fc = new FileInputStream(f).getChannel();
ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);
while (fc.read(bb) != -1){
bb.flip();
outChannel.write(bb);
bb.clear();
count++;
}
}
} catch (IOException e){
e.printStackTrace();
}finally {
try {
if(outChannel != null){
outChannel.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 文件操作