java 读取文件方法的总结
java读取文件方法的总结
1、按字节读取文件内容
2、按字符读取文件内容
3、按行读取文件内容
4、随机读取文件内容
publicclassReadFromFile{
/**
*以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/
publicstaticvoidreadFileByBytes(StringfileName){
Filefile=newFile(fileName);
InputStreamin=null;
try{
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
//一次读一个字节
in=newFileInputStream(file);
inttempbyte;
while((tempbyte=in.read())!=-1){
System.out.write(tempbyte);
}
in.close();
}catch(IOExceptione){
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(Exceptione1){
e1.printStackTrace();
}finally{
if(in!=null){
try{
in.close();
}catch(IOExceptione1){
}
}
}
}
/**
*以字符为单位读取文件,常用于读文本,数字等类型的文件
*/
publicstaticvoidreadFileByChars(StringfileName){
Filefile=newFile(fileName);
Readerreader=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(Exceptione){
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;i4)?4:0;
//将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[]bytes=newbyte[10];
intbyteread=0;
//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
//将一次读取的字节数赋给byteread
while((byteread=randomFile.read(bytes))!=-1){
System.out.write(bytes,0,byteread);
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(randomFile!=null){
try{
randomFile.close();
}catch(IOExceptione1){
}
}
}
}
/**
*显示输入流中还剩的字节数
*/
privatestaticvoidshowAvailableBytes(InputStreamin){
try{
System.out.println("当前字节输入流中的字节数为:"+in.available());
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
StringfileName="C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}
5、将内容追加到文件尾部
publicclassAppendToFile{
/**
*A方法追加文件:使用RandomAccessFile
*/
publicstaticvoidappendMethodA(StringfileName,Stringcontent){
try{
//打开一个随机访问文件流,按读写方式
RandomAccessFilerandomFile=newRandomAccessFile(fileName,"rw");
//文件长度,字节数
longfileLength=randomFile.length();
//将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
/**
*B方法追加文件:使用FileWriter
*/
publicstaticvoidappendMethodB(StringfileName,Stringcontent){
try{
//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriterwriter=newFileWriter(fileName,true);
writer.write(content);
writer.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
StringfileName="C:/temp/newTemp.txt";
Stringcontent="newappend!";
//按方法A追加文件
AppendToFile.appendMethodA(fileName,content);
AppendToFile.appendMethodA(fileName,"appendend.\n");
//显示文件内容
ReadFromFile.readFileByLines(fileName);
//按方法B追加文件
AppendToFile.appendMethodB(fileName,content);
AppendToFile.appendMethodB(fileName,"appendend.\n");
//显示文件内容
ReadFromFile.readFileByLines(fileName);
}
}
以上就是java读取文件的方法总结,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!