Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java-training
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jiewen.li
java-training
Commits
dfde1296
Commit
dfde1296
authored
Nov 01, 2018
by
jiewen.li
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add Tests
parent
848e639d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
8 deletions
+81
-8
demo1/src/test/java/cn/freemud/demo/IOTests.java
+81
-8
No files found.
demo1/src/test/java/cn/freemud/demo/IOTests.java
View file @
dfde1296
...
@@ -11,6 +11,8 @@ import org.junit.Assert;
...
@@ -11,6 +11,8 @@ import org.junit.Assert;
import
org.junit.Test
;
import
org.junit.Test
;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.nio.charset.Charset
;
import
java.nio.charset.Charset
;
import
java.util.List
;
import
java.util.List
;
...
@@ -23,21 +25,15 @@ public class IOTests {
...
@@ -23,21 +25,15 @@ public class IOTests {
public
void
Test
(){
public
void
Test
(){
String
content
=
"this is test file."
;
String
content
=
"this is test file."
;
String
fileName
=
"D:/java-traning/test1.md"
;
String
fileName
=
"D:/java-tra
i
ning/test1.md"
;
checkNotNull
(
fileName
,
"test fileName must not be null."
);
checkNotNull
(
fileName
,
"test fileName must not be null."
);
File
testFile
=
new
File
(
fileName
);
File
testFile
=
new
File
(
fileName
);
try
{
try
{
Files
.
createParentDirs
(
testFile
);
// write file.
if
(
testFile
.
isDirectory
()){
if
(
testFile
.
exists
()){
if
(
testFile
.
exists
()){
testFile
.
delete
();
testFile
.
delete
();
}
}
}
else
{
testFile
.
mkdirs
();
}
Files
.
write
(
content
,
testFile
,
Charsets
.
UTF_8
);
Files
.
write
(
content
,
testFile
,
Charsets
.
UTF_8
);
// read file
// read file
...
@@ -45,6 +41,83 @@ public class IOTests {
...
@@ -45,6 +41,83 @@ public class IOTests {
String
readFileContent
=
Joiner
.
on
(
"\r\n"
).
join
(
Files
.
readLines
(
testFile
,
Charsets
.
UTF_8
));
String
readFileContent
=
Joiner
.
on
(
"\r\n"
).
join
(
Files
.
readLines
(
testFile
,
Charsets
.
UTF_8
));
Assert
.
assertEquals
(
content
,
readFileContent
);
Assert
.
assertEquals
(
content
,
readFileContent
);
// read file use fileInputStream
FileInputStream
fileInputStream
=
null
;
String
fileInputStreamContent
=
""
;
try
{
fileInputStream
=
new
FileInputStream
(
testFile
);
byte
bytes
[]=
new
byte
[
1024
];
int
n
=
0
;
//中间变量,记录实际读入的字节总数
//循环读取
while
((
n
=
fileInputStream
.
read
(
bytes
))!=-
1
)
{
//read方法:read(byte[] b),最多读取b.length个字节,返回值是实际读入缓冲区的字节总数,
//当没有更多的数据时。返回-1
fileInputStreamContent
=
new
String
(
bytes
,
0
,
n
);
//表示从0~读入的总字节数
System
.
out
.
println
(
fileInputStreamContent
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
//关闭文件流,必须放在这里。
try
{
fileInputStream
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
Assert
.
assertEquals
(
fileInputStreamContent
,
content
);
String
copyFileName
=
"D:/java-training/test1_copy.md"
;
String
copyFileName2
=
"D:/java-training/test1_copy2.md"
;
File
copyFile
=
new
File
(
copyFileName
);
File
copyFile2
=
new
File
(
copyFileName2
);
copyFile
.
delete
();
copyFile2
.
delete
();
FileOutputStream
fileOutputStream
=
null
;
try
{
Files
.
createParentDirs
(
copyFile
);
fileOutputStream
=
new
FileOutputStream
(
copyFile
);
fileOutputStream
.
write
(
content
.
getBytes
());
fileOutputStream
.
flush
();
}
catch
(
IOException
e
){
e
.
printStackTrace
();
}
finally
{
//关闭文件流
try
{
fileOutputStream
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
FileOutputStream
fos
=
null
;
try
{
FileInputStream
fis
=
new
FileInputStream
(
testFile
);
fos
=
new
FileOutputStream
(
copyFileName2
);
byte
[]
buf
=
new
byte
[
fis
.
available
()];
fis
.
read
(
buf
);
fos
.
write
(
buf
);
fos
.
flush
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
//关闭文件流
try
{
fos
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
Assert
.
assertTrue
(
new
File
(
copyFileName
).
exists
());
Assert
.
assertTrue
(
new
File
(
copyFileName2
).
exists
());
}
}
catch
(
IOException
fileIoEx
){
catch
(
IOException
fileIoEx
){
String
ss
=
fileIoEx
.
toString
();
String
ss
=
fileIoEx
.
toString
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment