Skip to content

Commit e3a1567

Browse files
authored
Add files via upload
1 parent 6795abd commit e3a1567

8 files changed

Lines changed: 669 additions & 0 deletions

JavaCode/IO/FileBufferedTest.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package IO;
2+
3+
import org.junit.Test;
4+
5+
import java.io.*;
6+
7+
public class FileBufferedTest {
8+
@Test
9+
public void testBuffered(){
10+
FileInputStream fis = null;
11+
FileOutputStream fos = null;
12+
BufferedInputStream bis = null;
13+
BufferedOutputStream bos = null;
14+
try {
15+
File file1 = new File("香港图书馆.jpeg");
16+
File file2 = new File("香港图书馆buffered.jpeg");
17+
18+
fis = new FileInputStream(file1);
19+
fos = new FileOutputStream(file2);
20+
21+
bis = new BufferedInputStream(fis);
22+
bos = new BufferedOutputStream(fos);
23+
24+
byte[] buffer = new byte[10];
25+
int len;
26+
while ((len = bis.read(buffer)) != -1){
27+
bos.write(buffer,0,len);
28+
}
29+
} catch (IOException e) {
30+
e.printStackTrace();
31+
} finally {
32+
if(bis != null){
33+
try {
34+
bis.close();
35+
} catch (IOException e) {
36+
e.printStackTrace();
37+
}
38+
}
39+
40+
if(bos != null){
41+
try {
42+
bos.close();
43+
} catch (IOException e) {
44+
e.printStackTrace();
45+
}
46+
}
47+
48+
49+
// fis.close();
50+
// fos.close();
51+
}
52+
53+
}
54+
55+
public void copyFile1(String srcPath,String destPath){
56+
FileInputStream fis = null;
57+
FileOutputStream fos = null;
58+
BufferedInputStream bis = null;
59+
BufferedOutputStream bos = null;
60+
try {
61+
File file1 = new File(srcPath);
62+
File file2 = new File(destPath);
63+
64+
fis = new FileInputStream(file1);
65+
fos = new FileOutputStream(file2);
66+
67+
bis = new BufferedInputStream(fis);
68+
bos = new BufferedOutputStream(fos);
69+
70+
byte[] buffer = new byte[1024];
71+
int len;
72+
while ((len = bis.read(buffer)) != -1){
73+
bos.write(buffer,0,len);
74+
}
75+
} catch (IOException e) {
76+
e.printStackTrace();
77+
} finally {
78+
if(bis != null){
79+
try {
80+
bis.close();
81+
} catch (IOException e) {
82+
e.printStackTrace();
83+
}
84+
}
85+
86+
if(bos != null){
87+
try {
88+
bos.close();
89+
} catch (IOException e) {
90+
e.printStackTrace();
91+
}
92+
}
93+
94+
95+
// fis.close();
96+
// fos.close();
97+
}
98+
}
99+
100+
@Test
101+
public void test1(){
102+
long start = System.currentTimeMillis();
103+
104+
String srcPath = "香港图书馆.jpeg";
105+
String destPath = "香港图书馆testBuffered.jpeg";
106+
copyFile1(srcPath,destPath);
107+
108+
long end = System.currentTimeMillis();
109+
110+
System.out.println("复制所用的时间为:" + (end - start));//28
111+
}
112+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package IO;
2+
3+
import org.junit.Test;
4+
5+
import java.io.*;
6+
7+
public class FileInputOutputStreamTest {
8+
@Test
9+
public void testFileInputStream() {
10+
FileInputStream fis = null;
11+
FileOutputStream fos = null;
12+
try {
13+
File srcFile = new File("香港图书馆.jpeg");
14+
File destFile = new File("香港图书馆test.jpeg");
15+
16+
fis = new FileInputStream(srcFile);
17+
fos = new FileOutputStream(destFile);
18+
19+
byte[] bytes = new byte[5];
20+
int len;
21+
while ((len = fis.read(bytes)) != -1){
22+
fos.write(bytes,0,len);
23+
}
24+
} catch (IOException e) {
25+
e.printStackTrace();
26+
} finally {
27+
if(fos != null){
28+
try {
29+
fos.close();
30+
} catch (IOException e) {
31+
e.printStackTrace();
32+
}
33+
}
34+
35+
if(fis != null){
36+
try {
37+
fis.close();
38+
} catch (IOException e) {
39+
e.printStackTrace();
40+
}
41+
}
42+
43+
}
44+
45+
46+
47+
}
48+
49+
//指定路径下文件的赋值
50+
public void copyFile(String srcPath,String destPath){
51+
FileInputStream fis = null;
52+
FileOutputStream fos = null;
53+
try {
54+
File srcFile = new File(srcPath);
55+
File destFile = new File(destPath);
56+
57+
fis = new FileInputStream(srcFile);
58+
fos = new FileOutputStream(destFile);
59+
60+
byte[] bytes = new byte[1024];
61+
int len;
62+
while ((len = fis.read(bytes)) != -1){
63+
fos.write(bytes,0,len);
64+
}
65+
} catch (IOException e) {
66+
e.printStackTrace();
67+
} finally {
68+
if(fos != null){
69+
try {
70+
fos.close();
71+
} catch (IOException e) {
72+
e.printStackTrace();
73+
}
74+
}
75+
76+
if(fis != null){
77+
try {
78+
fis.close();
79+
} catch (IOException e) {
80+
e.printStackTrace();
81+
}
82+
}
83+
84+
}
85+
}
86+
87+
@Test
88+
public void test(){
89+
long start = System.currentTimeMillis();
90+
91+
String srcPath = "香港图书馆.jpeg";
92+
String destPath = "香港图书馆test.jpeg";
93+
copyFile(srcPath,destPath);
94+
95+
long end = System.currentTimeMillis();
96+
97+
System.out.println("复制所用的时间为:" + (end - start));//104
98+
}
99+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package IO;
2+
3+
import org.junit.Test;
4+
5+
import java.io.*;
6+
7+
public class FileReaderWriterTest {
8+
//测试FileReader中的read()方法
9+
@Test
10+
public void test() {
11+
File file = new File("hello.txt");
12+
13+
FileReader fr = null;
14+
try {
15+
fr = new FileReader(file);
16+
17+
int data;
18+
while ((data = fr.read()) != -1){
19+
System.out.print((char)data);
20+
}
21+
} catch (IOException e) {
22+
e.printStackTrace();
23+
} finally {
24+
try {
25+
fr.close();
26+
} catch (IOException e) {
27+
e.printStackTrace();
28+
}
29+
}
30+
31+
32+
}
33+
34+
35+
//测试FileReader中的read(cbuf[])方法
36+
@Test
37+
public void test1() {
38+
File file1 = new File("hello.txt");
39+
40+
FileReader fr = null;
41+
try {
42+
fr = new FileReader(file1);
43+
44+
char[] cbuf = new char[5];
45+
int len;
46+
while ((len = fr.read(cbuf)) != -1){
47+
for (int i = 0; i < len; i++) {
48+
System.out.print(cbuf[i]);
49+
}
50+
}
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
} finally {
54+
try {
55+
fr.close();
56+
} catch (IOException e) {
57+
e.printStackTrace();
58+
}
59+
}
60+
61+
}
62+
63+
64+
//测试FileWriter中的write()方法
65+
@Test
66+
public void testFileWriter(){
67+
File file = new File("hello1.txt");
68+
69+
FileWriter fw = null;
70+
try {
71+
fw = new FileWriter(file);
72+
73+
fw.write("I have a dream !\n");
74+
fw.write("Hello I know");
75+
} catch (IOException e) {
76+
e.printStackTrace();
77+
} finally {
78+
try {
79+
fw.close();
80+
} catch (IOException e) {
81+
e.printStackTrace();
82+
}
83+
}
84+
85+
86+
}
87+
88+
89+
//将文件读取,并写入另一个文件
90+
@Test
91+
public void testFileReaderFileWriter() {
92+
File file1 = new File("hello.txt");
93+
File file2 = new File("test.txt");
94+
95+
FileReader fr = null;
96+
FileWriter fw = null;
97+
try {
98+
fr = new FileReader(file1);
99+
fw = new FileWriter(file2);
100+
101+
char[] cbuf = new char[5];
102+
int len;
103+
while ((len = fr.read(cbuf)) != -1){
104+
fw.write(cbuf,0,len);
105+
}
106+
} catch (IOException e) {
107+
e.printStackTrace();
108+
} finally {
109+
110+
try {
111+
fw.close();
112+
} catch (IOException e) {
113+
e.printStackTrace();
114+
}
115+
116+
try {
117+
fr.close();
118+
} catch (IOException e) {
119+
e.printStackTrace();
120+
}
121+
}
122+
123+
}
124+
125+
}

JavaCode/IO/FileTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package IO;
2+
3+
import org.junit.Test;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.util.Date;
8+
9+
public class FileTest {
10+
@Test
11+
public void test(){
12+
File file1 = new File("Hello.txt");
13+
14+
System.out.println(file1.getAbsoluteFile());
15+
System.out.println(file1.getPath());
16+
System.out.println(file1.getName());
17+
System.out.println(file1.getParent());
18+
System.out.println(file1.length());
19+
System.out.println(new Date(file1.lastModified()));
20+
21+
System.out.println();
22+
23+
System.out.println(file1.isDirectory());
24+
System.out.println(file1.isFile());
25+
System.out.println(file1.exists());
26+
System.out.println(file1.canRead());
27+
System.out.println(file1.canWrite());
28+
System.out.println(file1.isHidden());
29+
}
30+
31+
@Test
32+
public void test1() throws IOException {
33+
File file2 = new File("hi.txt");
34+
if(!(file2.exists())){
35+
file2.createNewFile();
36+
System.out.println("Create Successful");
37+
}else {
38+
file2.delete();
39+
System.out.println("Delete Successful");
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)