java中講講FileReader的用法
FileReader的用法
FileReader是Reader的繼承類,從字面上就可看出,它的主要功能就是能從磁盤上讀入文件。read方法會(huì)一個(gè)一個(gè)字符的從磁盤往回讀數(shù)據(jù)。
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
例:2.2.1
import java.io.*;
public class TestMark_to_win {
public static void main(String args[]) throws Exception {
/*public int read()
throws IOException
Reads a single character.
Overrides:
read in class Reader
Returns:
The character read, or -1 if the end of the stream has been reached
Throws:
IOException - If an I/O error occurs */
int ii;
FileReader in = new FileReader("c:/1.txt");
while ((ii = in.read()) != -1) {
System.out.println(ii);
}
in.close();
FileReader in1 = new FileReader("c:/1.txt");
while ((ii = in1.read()) != -1) {
System.out.println((char)ii);
}
in1.close();
}
}
結(jié)果:
97
98
25105
20204
97
98
a
b
我
們
a
b
例:2.2.2(一個(gè)簡(jiǎn)單的拷貝方法,初學(xué)者方便理解)
import java.io.*;
public class TestMark_to_win {
public static void main(String[] args) throws IOException {
File inputFile = new File("c:/1.txt");
File outputFile = new File("c:/1_c.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
}
}