Java文件读写简单方法
本文章提供一个简单的文件读取方法:
具体代码如下:
package com.wrh.java.microblg.io;
/*
* FileOperate.java 2012/03/20
* Copyright (c) wrh
* Beijing, Chaoyang, 100101
* China
* All rights reserved
*
*/
import java.io.*;
import java.util.ArrayList;
/**
* The interface of file operate include read files and write files etc.
*
* @version 1.0 2011/12
* @author wrh
*
*/
public interface FileOperate {
/* the interface of the file operate */
/**
* read the directories under the file path.
*
* @param filePath
* the path you want to read from
* @return ArrayList<String> to store the directories
* @throws FileNotFoundException
* if the file does not find
* @throws IOException
* if io has some exceptions
*/
public ArrayList<String> readDirs(String filePath)
throws FileNotFoundException, IOException;
/**
* read the file content from file path
*
* @param file
* like D:\\a.txt
* @return the file content
* @throws UnsupportedEncodingException
* if the encoding exception
* @throws FileNotFoundException
* if the file does not find
* @throws IOException
* if io has some exceptions
*/
public String readFileContent(String file)
throws UnsupportedEncodingException, FileNotFoundException,
IOException;
/**
* read the file content from file path
*
* @param file
* like D:\\a.txt
* @return the file content
* @throws UnsupportedEncodingException
* if the encoding exception
* @throws FileNotFoundException
* if the file does not find
* @throws IOException
* if io has some exceptions
*/
public ArrayList<String> readFileContentByLine(String file)
throws UnsupportedEncodingException, FileNotFoundException,
IOException;
/**
* save content to the file in path
*
* @param content
* the be saved content
* @param path
* the saved path
* @param b
* append or not
* @throws IOException
*/
public void saveToFile(String content, String path, boolean b) throws IOException;
/**
* save content to the ../className/keyword.txt
*
* @param content
* the saved file content
* @param className
* the part of the file directory
* @param keyWords
* the file name
* @throws FileNotFoundException
*/
public void saveToFile(String content, String className, String keyWord)
throws FileNotFoundException;
}
其实现代码为:
package com.wrh.java.microblog.io.impl;
import com.wrh.java.microblg.io.FileOperate;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
public class FileOperateImpl implements FileOperate{
/**
* the implement of com.wrh.java.study.fileoperate.FileOperate
*
* @version 1.0 Mar 2012
* @author wrh
*
*/
private ArrayList<String> fileList = new ArrayList<String>();
/**
* read the directories of file path
*/
@Override
public ArrayList<String> readDirs(String filePath)
throws FileNotFoundException, IOException {
File file = new File(filePath);
if (!file.isDirectory()) {
System.out.println("The parameter you give is not a directory!");
} else {
String[] list = file.list();
for (int i = 0; i < list.length; i++) {
File readFile = new File(filePath + "\\" + list[i]);
if (!readFile.isDirectory()) {
fileList.add(readFile.getAbsolutePath());
} else {
readDirs(filePath + "\\" + list[i]);
}
}
}
return fileList;
}
/**
* read the file content and return
*/
@Override
public String readFileContent(String file)
throws UnsupportedEncodingException, FileNotFoundException,
IOException {
StringBuffer sb = new StringBuffer();
InputStreamReader is = new InputStreamReader(new FileInputStream(file),
"utf-8");
BufferedReader br = new BufferedReader(is);
String line = br.readLine();
while (line != null) {
sb.append(line).append("\r\n");
line = br.readLine();
}
br.close();
is.close();
return sb.toString();
}
/**
* read the content and save to arrayList by line
*/
@Override
public ArrayList<String> readFileContentByLine(String file)
throws UnsupportedEncodingException, FileNotFoundException,
IOException {
ArrayList<String> content = new ArrayList<String>();
InputStreamReader is = new InputStreamReader(new FileInputStream(file),
"gb2312");
BufferedReader br = new BufferedReader(is);
String line = br.readLine();
while (line != null) {
content.add(line);
line = br.readLine();
}
br.close();
is.close();
return content;
}
/**
* save content to the specified path
*/
@Override
public void saveToFile(String content, String path, boolean b) throws IOException {
// get the path
String dir = path.substring(0, path.lastIndexOf("\\"));
File file = new File(dir);
if (!file.exists()) {
file.mkdir();
}
file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, b)));
bw.write(content);
bw.flush();
bw.close();
}
@Override
public void saveToFile(String content, String className, String keyWord)
throws FileNotFoundException {
}
public static void main(String[] args) throws IOException {
FileOperateImpl f = new FileOperateImpl();
System.out.println(f.readDirs("D:\\result"));
//f.saveToFile("aaa", "D:\\a.txt");
System.out.println(f.readFileContent("D:\\a.txt"));
}
}
跟我走啊~~