本文转载自:http://www.blogjava.net/duduli/archive/2008/10/06/232662.html
java中很多关于文件读取的工作。
操作也很平凡。
所以java的文件读取是很重要的一部分。
首先是单字节的读取工作。
见源代码:
java中很多关于文件读取的工作。
操作也很平凡。
所以java的文件读取是很重要的一部分。
首先是单字节的读取工作。
见源代码:
1
package
ReadFile;
2![](/Images/OutliningIndicators/None.gif)
3
import
java.io.FileReader;
4
import
java.io.IOException;
5![](/Images/OutliningIndicators/None.gif)
6![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public
class
readFile
{
7![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void readfile(String file)
{
8![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
9
FileReader fr = new FileReader(file);
10
int ch = 0;
11![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while((ch=fr.read())!=-1)
{
12
System.out.println((char)ch);
13
}
14![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}catch(IOException e)
{
15
e.printStackTrace();
16
}
17
}
18![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args)
{
19
readfile("e:\\dd.txt");
20
}
21
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这里是但读取每个自己或字符。
如果要进行按行读取的话,必须采用BufferedReader BufferedReader中的readline();
1
package
ReadLime;
2![](/Images/OutliningIndicators/None.gif)
3
import
java.io.BufferedReader;
4
import
java.io.BufferedWriter;
5
import
java.io.FileReader;
6
import
java.io.FileWriter;
7
import
java.io.IOException;
8![](/Images/OutliningIndicators/None.gif)
9![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public
class
readLine
{
10![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void readline(String file,String writerfile)
{
11![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
12
FileReader fr = new FileReader(file);
13
BufferedReader br = new BufferedReader(fr);
14
15
FileWriter fw = new FileWriter(writerfile); //写文件操作,把得到的file对应的文件中内容写入,writerfile中去。
16
BufferedWriter bw = new BufferedWriter(fw);
17
String readoneline;
18
int l;
19![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while((l = br.read()) != -1)
{
20
readoneline = br.readLine();
21
bw.write(readoneline);
22
bw.newLine();
23
System.out.println(readoneline);
24
}
25
bw.flush();
26
br.close();
27
bw.close();
28
br.close();
29
fw.close();
30
fr.close();
31![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}catch(IOException e)
{
32
e.printStackTrace();
33
}
34
}
35![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String [] args)
{
36
readline("e:\\dd.txt","e:\\aa.txt");
37
}
38
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)