[Java]读取文件方法大全

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體:   |    |    |  超大  

转载处 http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html

1、按字节读取文件内容

2、按字符读取文件内容

3、按行读取文件内容

4、随机读取文件内容

publicclassReadFromFile {/*** 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。*/publicstaticvoidreadFileByBytes(String fileName) {

File file=newFile(fileName);

InputStream in=null;try{

System.out.PRintln("以字节为单位读取文件内容,一次读一个字节:");//一次读一个字节in =newFileInputStream(file);inttempbyte;while((tempbyte = in.read()) != -1) {

System.out.write(tempbyte);

}

in.close();

}catch(IOException e) {

e.printStackTrace();return;

}try{

System.out.println("以字节为单位读取文件内容,一次读多个字节:");//一次读多个字节byte[] tempbytes =newbyte[100];intbyteread = 0;

in=newFileInputStream(fileName);

ReadFromFile.showAvailableBytes(in);//读入多个字节到字节数组中,byteread为一次读入的字节数while((byteread = in.read(tempbytes)) != -1) {

System.out.write(tempbytes,0, byteread);

}

}catch(Exception e1) {

e1.printStackTrace();

}finally{if(in !=null) {try{

in.close();

}catch(IOException e1) {

}

}

}

}/*** 以字符为单位读取文件,常用于读文本,数字等类型的文件*/publicstaticvoidreadFileByChars(String fileName) {

File file=newFile(fileName);

Reader reader=null;try{

System.out.println("以字符为单位读取文件内容,一次读一个字节:");//一次读一个字符reader =newInputStreamReader(newFileInputStream(file));inttempchar;while((tempchar = reader.read()) != -1) {//对于windows下,\r\n这两个字符在一起时,表示一个换行。//但如果这两个字符分开显示时,会换两次行。//因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。if(((char) tempchar) != '\r') {

System.out.print((char) tempchar);

}

}

reader.close();

}catch(Exception e) {

e.printStackTrace();

}try{

System.out.println("以字符为单位读取文件内容,一次读多个字节:");//一次读多个字符char[] tempchars =newchar[30];intcharread = 0;

reader=newInputStreamReader(newFileInputStream(fileName));//读入多个字符到字符数组中,charread为一次读取字符数while((charread = reader.read(tempchars)) != -1) {//同样屏蔽掉\r不显示if((charread ==tempchars.length)&& (tempchars[tempchars.length - 1] != '\r')) {

System.out.print(tempchars);

}else{for(inti = 0; i < charread; i++) {if(tempchars[i] == '\r') {continue;

}else{

System.out.print(tempchars[i]);

}

}

}

}

}catch(Exception e1) {

e1.printStackTrace();

}finally{if(reader !=null) {try{

reader.close();

}catch(IOException e1) {

}

}

}

}/*** 以行为单位读取文件,常用于读面向行的格式化文件*/publicstaticvoidreadFileByLines(String fileName) {

File file=newFile(fileName);

BufferedReader reader=null;try{

System.out.println("以行为单位读取文件内容,一次读一整行:");

reader=newBufferedReader(newFileReader(file));

String tempString=null;intline = 1;//一次读入一行,直到读入null为文件结束while((tempString = reader.readLine()) !=null) {//显示行号System.out.println("line " + line + ": " +tempString);

line++;

}

reader.close();

}catch(IOException e) {

e.printStackTrace();

}finally{if(reader !=null) {try{

reader.close();

}catch(IOException e1) {

}

}

}

}/*** 随机读取文件内容*/publicstaticvoidreadFileByRandomaccess(String fileName) {

RandomAccessFile randomFile=null;try{

System.out.println("随机读取一段文件内容:");//打开一个随机访问文件流,按只读方式randomFile =newRandomAccessFile(fileName, "r");//文件长度,字节数longfileLength =randomFile.length();//读文件的起始位置intbeginIndex = (fileLength > 4) ? 4 : 0;//将读文件的开始位置移到beginIndex位置。randomFile.seek(beginIndex);byte[] bytes =newbyte[10];intbyteread = 0;//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。//将一次读取的字节数赋给bytereadwhile((byteread = randomFile.read(bytes)) != -1) {

System.out.write(bytes,0, byteread);

}

}catch(IOException e) {

e.printStackTrace();

}finally{if(randomFile !=null) {try{

randomFile.close();

}catch(IOException e1) {

}

}

}

}/*** 显示输入流中还剩的字节数*/privatestaticvoidshowAvailableBytes(InputStream in) {try{

System.out.println("当前字节输入流中的字节数为:" +in.available());

}catch(IOException e) {

e.printStackTrace();

}

}publicstaticvoidmain(String[] args) {

String fileName= "C:/temp/newTemp.txt";

ReadFromFile.readFileByBytes(fileName);

ReadFromFile.readFileByChars(fileName);

ReadFromFile.readFileByLines(fileName);

ReadFromFile.readFileByRandomAccess(fileName);

}

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航