java文件 四种方法.docx
- 文档编号:4307680
- 上传时间:2022-11-29
- 格式:DOCX
- 页数:32
- 大小:23.90KB
java文件 四种方法.docx
《java文件 四种方法.docx》由会员分享,可在线阅读,更多相关《java文件 四种方法.docx(32页珍藏版)》请在冰豆网上搜索。
java文件四种方法
一、多种方式读文件内容。
1、按字节读取文件内容
2、按字符读取文件内容
3、按行读取文件内容
4、随机读取文件内容
Java代码
1.importjava.io.BufferedReader;
2.importjava.io.File;
3.importjava.io.FileInputStream;
4.importjava.io.FileReader;
5.importjava.io.IOException;
6.importjava.io.InputStream;
7.importjava.io.InputStreamReader;
8.importjava.io.RandomAccessFile;
9.importjava.io.Reader;
10.
11.publicclassReadFromFile{
12./**
13.*以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
14.*
15.*@paramfileName
16.*文件的名
17.*/
18.publicstaticvoidreadFileByBytes(StringfileName){
19.Filefile=newFile(fileName);
20.InputStreamin=null;
21.try{
22.System.out.println("以字节为单位读取文件内容,一次读一个字节:
");
23.//一次读一个字节
24.in=newFileInputStream(file);
25.inttempbyte;
26.while((tempbyte=in.read())!
=-1){
27.System.out.write(tempbyte);
28.}
29.in.close();
30.}catch(IOExceptione){
31.e.printStackTrace();
32.return;
33.}
34.try{
35.System.out.println("以字节为单位读取文件内容,一次读多个字节:
");
36.//一次读多个字节
37.byte[]tempbytes=newbyte[100];
38.intbyteread=0;
39.in=newFileInputStream(fileName);
40.ReadFromFile.showAvailableBytes(in);
41.//读入多个字节到字节数组中,byteread为一次读入的字节数
42.while((byteread=in.read(tempbytes))!
=-1){
43.System.out.write(tempbytes,0,byteread);
44.}
45.}catch(Exceptione1){
46.e1.printStackTrace();
47.}finally{
48.if(in!
=null){
49.try{
50.in.close();
51.}catch(IOExceptione1){
52.}
53.}
54.}
55.}
56.
57./**
58.*以字符为单位读取文件,常用于读文本,数字等类型的文件
59.*
60.*@paramfileName
61.*文件名
62.*/
63.publicstaticvoidreadFileByChars(StringfileName){
64.Filefile=newFile(fileName);
65.Readerreader=null;
66.try{
67.System.out.println("以字符为单位读取文件内容,一次读一个字节:
");
68.//一次读一个字符
69.reader=newInputStreamReader(newFileInputStream(file));
70.inttempchar;
71.while((tempchar=reader.read())!
=-1){
72.//对于windows下,\r\n这两个字符在一起时,表示一个换行。
73.//但如果这两个字符分开显示时,会换两次行。
74.//因此,屏蔽掉\r,或者屏蔽\n。
否则,将会多出很多空行。
75.if(((char)tempchar)!
='\r'){
76.System.out.print((char)tempchar);
77.}
78.}
79.reader.close();
80.}catch(Exceptione){
81.e.printStackTrace();
82.}
83.try{
84.System.out.println("以字符为单位读取文件内容,一次读多个字节:
");
85.//一次读多个字符
86.char[]tempchars=newchar[30];
87.intcharread=0;
88.reader=newInputStreamReader(newFileInputStream(fileName));
89.//读入多个字符到字符数组中,charread为一次读取字符数
90.while((charread=reader.read(tempchars))!
=-1){
91.//同样屏蔽掉\r不显示
92.if((charread==tempchars.length)
93.&&(tempchars[tempchars.length-1]!
='\r')){
94.System.out.print(tempchars);
95.}else{
96.for(inti=0;i 97.if(tempchars[i]=='\r'){ 98.continue; 99.}else{ 100.System.out.print(tempchars[i]); 101.} 102.} 103.} 104.} 105. 106.}catch(Exceptione1){ 107.e1.printStackTrace(); 108.}finally{ 109.if(reader! =null){ 110.try{ 111.reader.close(); 112.}catch(IOExceptione1){ 113.} 114.} 115.} 116.} 117. 118./** 119.*以行为单位读取文件,常用于读面向行的格式化文件 120.* 121.*@paramfileName 122.*文件名 123.*/ 124.publicstaticvoidreadFileByLines(StringfileName){ 125.Filefile=newFile(fileName); 126.BufferedReaderreader=null; 127.try{ 128.System.out.println("以行为单位读取文件内容,一次读一整行: "); 129.reader=newBufferedReader(newFileReader(file)); 130.StringtempString=null; 131.intline=1; 132.//一次读入一行,直到读入null为文件结束 133.while((tempString=reader.readLine())! =null){ 134.//显示行号 135.System.out.println("line"+line+": "+tempString); 136.line++; 137.} 138.reader.close(); 139.}catch(IOExceptione){ 140.e.printStackTrace(); 141.}finally{ 142.if(reader! =null){ 143.try{ 144.reader.close(); 145.}catch(IOExceptione1){ 146.} 147.} 148.} 149.} 150. 151./** 152.*随机读取文件内容 153.* 154.*@paramfileName 155.*文件名 156.*/ 157.publicstaticvoidreadFileByRandomAccess(StringfileName){ 158.RandomAccessFilerandomFile=null; 159.try{ 160.System.out.println("随机读取一段文件内容: "); 161.//打开一个随机访问文件流,按只读方式 162.randomFile=newRandomAccessFile(fileName,"r"); 163.//文件长度,字节数 164.longfileLength=randomFile.length(); 165.//读文件的起始位置 166.intbeginIndex=(fileLength>4)? 4: 0; 167.//将读文件的开始位置移到beginIndex位置。 168.randomFile.seek(beginIndex); 169.byte[]bytes=newbyte[10]; 170.intbyteread=0; 171.//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。 172.//将一次读取的字节数赋给byteread 173.while((byteread=randomFile.read(bytes))! =-1){ 174.System.out.write(bytes,0,byteread); 175.} 176.}catch(IOExceptione){ 177.e.printStackTrace(); 178.}finally{ 179.if(randomFile! =null){ 180.try{ 181.randomFile.close(); 182.}catch(IOExceptione1){ 183.} 184.} 185.} 186.} 187. 188./** 189.*显示输入流中还剩的字节数 190.* 191.*@paramin 192.*/ 193.privatestaticvoidshowAvailableBytes(InputStreamin){ 194.try{ 195.System.out.println("当前字节输入流中的字节数为: "+in.available()); 196.}catch(IOExceptione){ 197.e.printStackTrace(); 198.} 199.} 200. 201.publicstaticvoidmain(String[]args){ 202.StringfileName="C: /temp/newTemp.txt"; 203.ReadFromFile.readFileByBytes(fileName); 204.ReadFromFile.readFileByChars(fileName); 205.ReadFromFile.readFileByLines(fileName); 206.ReadFromFile.readFileByRandomAccess(fileName); 207.} 208.} importjava.io.BufferedReader; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileReader; importjava.io.IOException; importjava.io.InputStream; importjava.io.InputStreamReader; importjava.io.RandomAccessFile; importjava.io.Reader; publicclassReadFromFile{ /** *以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 * *@paramfileName *文件的名 */ 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){ } } } } /** *以字符为单位读取文件,常用于读文本,数字等类型的文件 * *@paramfileName *文件名 */ 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;i if(tempchars[i]=='\r'){ continue; }else{ System.out.print(tempchars[i]); } } } } }catch(Exceptione1){ e1.printStackTrace(); }finally{ if(reader! =null){ try{ reader.close(); }catch(IOExceptione1){ } } } } /** *以行为单位读取文件,常用于读面向行的格式化文件 * *@paramfileName *文件名 */ publicstaticvoidreadFileByLines(StringfileName){ Filefile=newFile(fileName); BufferedReaderreader=null; try{ System.out.println("以行为单位读取文件内容,一次读一整行: "); reader=newBufferedReader(newFileReader(file)); StringtempString=null; intline=1; //一次读入一行,直到读入null为文件结束 while((tempString=reader.readLine())! =null){ //显示行号 System.out.println("line"+line+": "+tempString); line++; } reader.close(); }catch(IOExceptione){ e.printStackTrace(); }finally{ if(reader! =null){ try{ reader.close(); }catch(IOExceptione1){ } } } } /** *随机读取文件内容 * *@paramfileName *文件名 */ publicstaticvoidreadFileByRandomAccess(StringfileName){ RandomAccessFilerandomFile=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个字节,则读剩下的字节。 //将一次读取的字节数赋给byteread while((byteread=randomFile.read(bytes))! =-1){ System.out.write(bytes,0,byteread); } }catch(IOExceptione){ e.printStackTrace(); }finally{ if(randomFile! =null){ try{
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java文件 四种方法 java 文件 方法