java中读取文件的方式
经常遇到java中读取文件的方式,有时候需要指定编码,有时候不需要指定编码,被搞的挺晕的,抽时间整理了一下java读取文件的方式,主要是对字符型的处理,二进制的暂时不考虑。
1.readFile方法,主要采用FileReader来读取文件,不能指定编码
2.readFileByInputStream方法,采用InputStreamReader来读取文件,可以指定编码
3.readFileByBufferedReader方法,采用BufferedReader来读取文件,不可以指定编码
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
/**
* 测试读取文件
*
* @author rey2008
*
*/
public class TestReadFile {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String sFilePath = "D://test//test_read.txt";
readFileByBufferedReader(sFilePath);
}
/**
* 以字符读取文件,采取策略 File->FileReader->char[]<BR>
* FileReader的读取过程:FileReader->FileInputStream(不能指定编码,
* 指定编码是在InputStreamReader中指定的,FileReader正好跳过了指定编码的一步)
*
* @param _sFilePath
*/
private static void readFile(String _sFilePath) throws Exception {
File aFile = new File(_sFilePath);
if (!aFile.exists()) {
throw new Exception("路径[" + _sFilePath + "]对应的文件不存在!");
}
FileReader aFileReader = null;
相关文档:
1、Oracle8/8i/9i数据库(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
2、DB ......
import java.util.*;
import java.util.regex.Pattern;
public class StrTools {
/**
* 分割字符串
*
* @param str String 原始字符串
* @param splitsign String 分隔符
* @return String[] 分割后的字符串数组
*/
@SuppressWarnings("unchecked")
public static String[] split(Stri ......
import java.awt.image. * ;
import com.sun.image.codec.jpeg. * ;
public class poiReadDoc {
Image img = null;
int width = 0,height =0;
String destFile = "";
public void readImg(String fileName) throws IOException{
File _fil ......
public class InsertBlobData {
Connection con = null;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
InsertBlobData data = new InsertBlobData();
data.insertBlogInfo("002jpg", "sdsdfdf", "2007-02-1 ......