java读取文件方法大全文档格式.docx
- 文档编号:20770790
- 上传时间:2023-01-25
- 格式:DOCX
- 页数:33
- 大小:25.37KB
java读取文件方法大全文档格式.docx
《java读取文件方法大全文档格式.docx》由会员分享,可在线阅读,更多相关《java读取文件方法大全文档格式.docx(33页珍藏版)》请在冰豆网上搜索。
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))!
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())!
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))!
91.//同样屏蔽掉\r不显示
92.if((charread==tempchars.length)
93.&
&
(tempchars[tempchars.length-1]!
)){
94.System.out.print(tempchars);
95.}else{
96.for(inti=0;
i<
charread;
i++){
97.if(tempchars[i]=='
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!
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())!
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!
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))!
174.System.out.write(bytes,0,byteread);
175.}
176.}catch(IOExceptione){
177.e.printStackTrace();
178.}finally{
179.if(randomFile!
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;
}
//一次读多个字节
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){
try{
in.close();
}catch(IOExceptione1){
}
}
*以字符为单位读取文件,常用于读文本,数字等类型的文件
*文件名
publicstaticvoidreadFileByChars(StringfileName){
Readerreader=null;
//一次读一个字符
reader=newInputStreamReader(newFileInputStream(file));
inttempchar;
while((tempchar=reader.read())!
//对于windows下,\r\n这两个字符在一起时,表示一个换行。
//但如果这两个字符分开显示时,会换两次行。
//因此,屏蔽掉\r,或者屏蔽\n。
if(((char)tempchar)!
){
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)
&
)){
System.out.print(tempchars);
}else{
for(inti=0;
i++){
if(tempchars[i]=='
continue;
}else{
System.out.print(tempchars[i]);
}
}
if(reader!
reader.close();
*以行为单位读取文件,常用于读面向行的格式化文件
publicstaticvoidreadFileByLines(StringfileName){
BufferedReaderreader=null;
reader=newBufferedReader(newFileReader(file));
StringtempString=null;
intline=1;
//一次读入一行,直到读入null为文件结束
while((tempString=reader.readLine())!
//显示行号
System.out.println("
line++;
*随机读取文件内容
publicstaticvoidreadFileByRandomAccess(StringfileName){
RandomAccessFilerandomFile=null;
//打开一个随机访问文件流,按只读方式
randomFile=newRandomAccessFile(fileName,"
//文件长度,字节数
longfileLength=randomFile.length();
//读文件的起始位置
intbeginIndex=(fileLength>
//将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[]bytes=newbyte[10];
//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
//将一次读取的字节数赋给byteread
while((byteread=randomFile.read(bytes))!
System.out.write(bytes,0,byteread);
if(randomFile
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 读取 文件 方法 大全