IO是JAVASE中很是重要的一塊,是面向對象的完美體現,深刻學習IO,你將能夠領略到不少面向對象的思想。
在公司沒活幹,複習了一下IO,發現不少都忘記了,因此寫的很差,只夠初學用。我把我複習過程當中寫的代碼貼出來,你們共同窗習,並請多指教指教哈。順便一塊兒討論IO
一、文件拷貝
java
Java code?git
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
try
{
File inputFile =
new
File(args[
0
]);
if
(!inputFile.exists()) {
System.out.println(
"源文件不存在,程序終止"
);
System.exit(
1
);
}
File outputFile =
new
File(args[
1
]);
InputStream in =
new
FileInputStream(inputFile);
OutputStream out =
new
FileOutputStream(outputFile);
byte
date[] =
new
byte
[
1024
];
int
temp =
0
;
while
((temp = in.read(date)) != -
1
) {
out.write(date);
}
in.close();
out.close();
}
catch
(FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
|
二、java讀文件:實現統計某一目錄下每一個文件中出現的字母個數、數字個數、空格個數及行數,除此以外沒有其餘字符
數組
Java code?app
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
String fileName =
"D:/date.java.bak"
;
// String fileName = "D:/test.qqq";
String line;
int
i =
0
, j =
0
, f =
0
, k =
0
;
try
{
BufferedReader in =
new
BufferedReader(
new
FileReader(fileName));
line = in.readLine();
while
(line !=
null
) {
// System.out.println(line);
char
c[] = line.toCharArray();
for
(
int
i1 =
0
; i1 < c.length; i1++) {
// 若是是字母
if
(Character.isLetter(c[i1]))
i++;
// 若是是數字
else
if
(Character.isDigit(c[i1]))
j++;
// 是空格
else
if
(Character.isWhitespace(c[i1]))
f++;
}
line = in.readLine();
k++;
}
in.close();
System.out
.println(
"字母:"
+ i +
",數字:"
+ j +
",空格:"
+ f +
",行數:"
+ k);
}
catch
(IOException e) {
e.printStackTrace();
}
|
三、 從文件(d:\test.txt)中查出字符串」aa」出現的次數
學習
Java code?ui
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
try
{
BufferedReader br =
new
BufferedReader(
new
FileReader(
"D:\\test.txt"
));
StringBuilder sb =
new
StringBuilder();
while
(
true
) {
String str = br.readLine();
if
(str ==
null
)
break
;
sb.append(str);
}
Pattern p = Pattern.compile(
"aa"
);
Matcher m = p.matcher(sb);
int
count =
0
;
while
(m.find()) {
count++;
}
System.out.println(
"\"aa\"一共出現了"
+ count +
"次"
);
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
|
四、 三種方法讀取文件
spa
Java code?.net
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
try
{
// 方法一
BufferedReader br =
new
BufferedReader(
new
FileReader(
new
File(
"D:\\1.xls"
)));
// StringBuilder bd = new StringBuilder();
StringBuffer bd =
new
StringBuffer();
while
(
true
) {
String str = br.readLine();
if
(str ==
null
) {
break
;
}
System.out.println(str);
bd.append(str);
}
br.close();
// System.out.println(bd.toString());
// 方法二
InputStream is =
new
FileInputStream(
new
File(
"d:\\1.xls"
));
byte
b[] =
new
byte
[Integer.parseInt(
new
File(
"d:\\1.xls"
).length()
+
""
)];
is.read(b);
System.out.write(b);
System.out.println();
is.close();
// 方法三
Reader r =
new
FileReader(
new
File(
"d:\\1.xls"
));
char
c[] =
new
char
[(
int
)
new
File(
"d:\\1.xls"
).length()];
r.read(c);
String str =
new
String(c);
System.out.print(str);
r.close();
}
catch
(RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch
(FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
|
五、三種方法寫文件
code
Java code?對象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
try
{
PrintWriter pw =
new
PrintWriter(
new
FileWriter(
"d:\\1.txt"
));
BufferedWriter bw =
new
BufferedWriter(
new
FileWriter(
new
File(
"d:\\1.txt"
)));
OutputStream os =
new
FileOutputStream(
new
File(
"d:\\1.txt"
));
// 1
os.write(
"ffff"
.getBytes());
// 2
// bw.write("ddddddddddddddddddddddddd");
// 3
// pw.print("你好sssssssssssss");
bw.close();
pw.close();
os.close();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
|
六、讀取文件,並把讀取的每一行存入double型數組中
Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
try
{
BufferedReader br =
new
BufferedReader(
new
FileReader(
new
File(
"d:\\2.txt"
)));
StringBuffer sb =
new
StringBuffer();
while
(
true
) {
String str = br.readLine();
if
(str ==
null
) {
break
;
}
sb.append(str +
"、"
);
}
String str = sb.toString();
String s[] = str.split(
"、"
);
double
d[] =
new
double
[s.length];
for
(
int
i =
0
; i < s.length; i++) {
d[i] = Double.parseDouble(s[i]);
}
for
(
int
i =
0
; i < d.length; i++) {
System.out.println(d[i]);
}
br.close();
}
catch
(FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
|