几种读取文件的方法Word文件下载.docx
《几种读取文件的方法Word文件下载.docx》由会员分享,可在线阅读,更多相关《几种读取文件的方法Word文件下载.docx(8页珍藏版)》请在冰豆网上搜索。
inttempbyte;
while((tempbyte=in.read())!
=-1){
System.out.write(tempbyte);
}
in.close();
}catch(IOExceptione){
e.printStackTrace();
return;
以字节为单位读取文件内容,一次读多个字节:
//一次读多个字节
byte[]tempbytes=newbyte[100];
intbyteread=0;
in=newFileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
//读入多个字节到字节数组中,byteread为一次读入的字节数
while((byteread=in.read(tempbytes))!
System.out.write(tempbytes,0,byteread);
}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(in!
=null){
}catch(IOExceptione1){
*以字符为单位读取文件,常用于读文本,数字等类型的文件
publicstaticvoidreadFileByChars(StringfileName){
Readerreader=null;
以字符为单位读取文件内容,一次读一个字节:
//一次读一个字符
reader=newInputStreamReader(newFileInputStream(file));
inttempchar;
while((tempchar=reader.read())!
//对于windows下,\r\n这两个字符在一起时,表示一个换行。
//但如果这两个字符分开显示时,会换两次行。
//因此,屏蔽掉\r,或者屏蔽\n。
否则,将会多出很多空行。
if(((char)tempchar)!
='
\r'
){
System.out.print((char)tempchar);
reader.close();
}catch(Exceptione){
以字符为单位读取文件内容,一次读多个字节:
//一次读多个字符
char[]tempchars=newchar[30];
intcharread=0;
reader=newInputStreamReader(newFileInputStream(fileName));
//读入多个字符到字符数组中,charread为一次读取字符数
while((charread=reader.read(tempchars))!
//同样屏蔽掉\r不显示
if((charread==tempchars.length)
&
&
(tempchars[tempchars.length-1]!
)){
System.out.print(tempchars);
}else{
for(inti=0;
i<
charread;
i++){
if(tempchars[i]=='
continue;
System.out.print(tempchars[i]);
if(reader!
*以行为单位读取文件,常用于读面向行的格式化文件
publicstaticvoidreadFileByLines(StringfileName){
BufferedReaderreader=null;
以行为单位读取文件内容,一次读一整行:
reader=newBufferedReader(newFileReader(file));
StringtempString=null;
intline=1;
//一次读入一行,直到读入null为文件结束
while((tempString=reader.readLine())!
//显示行号
line"
+line+"
:
"
+tempString);
line++;
*随机读取文件内容
publicstaticvoidreadFileByRandomAccess(StringfileName){
RandomAccessFilerandomFile=null;
随机读取一段文件内容:
//打开一个随机访问文件流,按只读方式
randomFile=newRandomAccessFile(fileName,"
r"
//文件长度,字节数
longfileLength=randomFile.length();
//读文件的起始位置
intbeginIndex=(fileLength>
4)?
4:
0;
//将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[]bytes=newbyte[10];
//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
//将一次读取的字节数赋给byteread
while((byteread=randomFile.read(bytes))!
System.out.write(bytes,0,byteread);
if(randomFile!
randomFile.close();
*显示输入流中还剩的字节数
privatestaticvoidshowAvailableBytes(InputStreamin){
当前字节输入流中的字节数为:
+in.available());
publicstaticvoidmain(String[]args){
StringfileName="
C:
/temp/newTemp.txt"
;
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
publicclassAppendToFile{
*A方法追加文件:
使用RandomAccessFile
publicstaticvoidappendMethodA(StringfileName,Stringcontent){
//打开一个随机访问文件流,按读写方式
RandomAccessFilerandomFile=newRandomAccessFile(fileName,"
rw"
//将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
*B方法追加文件:
使用FileWriter
publicstaticvoidappendMethodB(StringfileName,Stringcontent){
//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriterwriter=newFileWriter(fileName,true);
writer.write(content);
writer.close();
Stringcontent="
newappend!
//按方法A追加文件
AppendToFile.appendMethodA(fileName,content);
AppendToFile.appendMethodA(fileName,"
appendend.\n"
//显示文件内容
//按方法B追加文件
AppendToFile.appendMethodB(fileName,content);
AppendToFile.appendMethodB(fileName,"