Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
java-exercise
Project
Project
Details
Activity
Releases
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
时良荣
java-exercise
Commits
3345b763
Commit
3345b763
authored
Nov 09, 2020
by
时良荣
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
张国敬 Java培训 = 文件读写
parent
431a2c1a
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
220 additions
and
0 deletions
+220
-0
ExerciseFirst.java
src/main/java/com/exercise/fileio/ExerciseFirst.java
+40
-0
ExerciseFourth.java
src/main/java/com/exercise/fileio/ExerciseFourth.java
+71
-0
ExerciseSecond.java
src/main/java/com/exercise/fileio/ExerciseSecond.java
+38
-0
ExerciseThird.java
src/main/java/com/exercise/fileio/ExerciseThird.java
+69
-0
test.txt
src/main/java/com/exercise/fileio/exercise/test.txt
+1
-0
test_copy.txt
src/main/java/com/exercise/fileio/exercise/test_copy.txt
+1
-0
ExerciseFirst.class
target/classes/com/exercise/fileio/ExerciseFirst.class
+0
-0
ExerciseFourth.class
target/classes/com/exercise/fileio/ExerciseFourth.class
+0
-0
ExerciseSecond.class
target/classes/com/exercise/fileio/ExerciseSecond.class
+0
-0
ExerciseThird.class
target/classes/com/exercise/fileio/ExerciseThird.class
+0
-0
No files found.
src/main/java/com/exercise/fileio/ExerciseFirst.java
0 → 100644
View file @
3345b763
package
com
.
exercise
.
fileio
;
import
java.io.File
;
/**
* 张国敬 Java培训 = 文件读写
* 编写一段程序,判断d:/study/io目录是否存在,存在则遍历输出这个目录的所有文件名称,不存在则创建这个目录
* @author elliot
*/
public
class
ExerciseFirst
{
public
static
void
main
(
String
[]
args
){
String
target
=
"/Users/elliot/JavaProjects/java-exercise/src/main/java/com/exercise/fileio/exercise"
;
listFiles
(
target
);
}
public
static
void
listFiles
(
final
String
path
)
{
File
file
=
new
File
(
path
);
if
(!
file
.
exists
()){
System
.
out
.
println
(
"该文件不存在,创建该文件"
);
file
.
mkdirs
();
return
;
}
String
[]
children
=
file
.
list
();
if
(
children
==
null
||
children
.
length
==
0
){
return
;
}
for
(
String
child
:
children
){
String
childPath
=
path
+
File
.
separator
+
child
;
File
childFile
=
new
File
(
childPath
);
if
(
childFile
.
isFile
()){
System
.
out
.
println
(
"找到了一个文件:"
+
childPath
);
}
else
{
listFiles
(
childPath
);
}
}
}
}
src/main/java/com/exercise/fileio/ExerciseFourth.java
0 → 100644
View file @
3345b763
package
com
.
exercise
.
fileio
;
import
java.io.*
;
/**
* 编写一个文件复制函数,copyFile(String from, String to),from为原文件名,to为复制后的新文件名,复制结束打印
* 所复制的文件大小和用时(毫秒),执行结果如:copy xxx use xxx ms, file size is xxx
* @author elliot
*/
public
class
ExerciseFourth
{
public
static
void
main
(
String
[]
args
){
String
fromFile
=
"/Users/elliot/JavaProjects/java-exercise/src/main/java/com/exercise/fileio/exercise/test.txt"
;
String
targetFile
=
"/Users/elliot/JavaProjects/java-exercise/src/main/java/com/exercise/fileio/exercise/test_copy.txt"
;
copyFile
(
fromFile
,
targetFile
);
}
/**
* 复制文件
* @param from
* @param to
* @return
*/
public
static
void
copyFile
(
String
from
,
String
to
){
long
currentTimeMillisStart
=
System
.
currentTimeMillis
();
File
file
=
new
File
(
from
);
if
(!
file
.
exists
()){
System
.
out
.
println
(
"文件不存在"
);
return
;
}
InputStream
in
=
null
;
OutputStream
out
=
null
;
try
{
in
=
new
FileInputStream
(
file
);
out
=
new
FileOutputStream
(
new
File
(
to
));
byte
[]
bytes
=
new
byte
[
4096
];
int
len
=
0
;
while
((
len
=
in
.
read
(
bytes
))
>
0
){
out
.
write
(
bytes
,
0
,
len
);
}
}
catch
(
IOException
e
){
e
.
printStackTrace
();
}
finally
{
if
(
in
!=
null
){
try
{
in
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
if
(
out
!=
null
){
try
{
out
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
long
currentTimeMillisEnd
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
"copy from "
+
from
+
" use "
+
(
currentTimeMillisEnd
-
currentTimeMillisStart
)
+
"ms, file size is "
+
file
.
length
());
}
}
src/main/java/com/exercise/fileio/ExerciseSecond.java
0 → 100644
View file @
3345b763
package
com
.
exercise
.
fileio
;
import
java.io.File
;
/**
* 编写一段程序,删除d:/study/io这个目录下的大于20Kb的文件
* @author elliot
*/
public
class
ExerciseSecond
{
public
static
void
main
(
String
[]
args
){
String
target
=
"/Users/elliot/JavaProjects/java-exercise/src/main/java/com/exercise/fileio/exercise"
;
listFilesAndClear
(
target
);
}
public
static
void
listFilesAndClear
(
final
String
path
)
{
File
file
=
new
File
(
path
);
if
(!
file
.
exists
()){
return
;
}
String
[]
children
=
file
.
list
();
if
(
children
==
null
||
children
.
length
==
0
){
return
;
}
for
(
String
child
:
children
){
String
childPath
=
path
+
File
.
separator
+
child
;
File
childFile
=
new
File
(
childPath
);
if
(
childFile
.
isFile
()
&&
childFile
.
length
()
>
(
20
*
1024
)){
System
.
out
.
println
(
"找到了一个大于20KB的文件并删除:"
+
childPath
);
childFile
.
delete
();
}
else
{
listFilesAndClear
(
childPath
);
}
}
}
}
src/main/java/com/exercise/fileio/ExerciseThird.java
0 → 100644
View file @
3345b763
package
com
.
exercise
.
fileio
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.util.*
;
/**
* 编写一段程序,循环从键盘输入一行字符,把这行字符首尾颠倒后按行输出到一个文件中,遇到quit后退出
* @author elliot
*/
public
class
ExerciseThird
{
public
static
void
main
(
String
[]
args
){
System
.
out
.
println
(
"请输入一段字符:"
);
Scanner
scanner
=
new
Scanner
(
System
.
in
);
String
s
=
scanner
.
next
();
System
.
out
.
println
(
"输入的字符是:"
+
s
);
// 把这行字符首尾颠倒
String
reverseString
=
stringReverse
(
s
);
System
.
out
.
println
(
"倒序后的字符是:"
+
reverseString
);
// 输出到一个文件中
String
fileName
=
"/Users/elliot/JavaProjects/java-exercise/src/main/java/com/exercise/fileio/exercise/test.txt"
;
stringWrite
(
reverseString
,
fileName
);
}
/**
* 颠倒字符顺序
* @param string
* @return
*/
public
static
String
stringReverse
(
String
string
){
String
[]
s
=
string
.
split
(
""
);
String
str
=
new
String
();
List
<
String
>
list
=
new
ArrayList
<>(
Arrays
.
asList
(
s
));
Collections
.
reverse
(
list
);
for
(
Object
o
:
list
)
{
str
=
str
+
o
;
}
return
str
;
}
/**
* 将字符写到文件中
* @param str
* @param str
*/
public
static
void
stringWrite
(
String
str
,
String
fileName
){
FileWriter
fw
=
null
;
try
{
fw
=
new
FileWriter
(
fileName
);
fw
.
write
(
str
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
fw
!=
null
){
try
{
fw
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
src/main/java/com/exercise/fileio/exercise/test.txt
0 → 100644
View file @
3345b763
111111
\ No newline at end of file
src/main/java/com/exercise/fileio/exercise/test_copy.txt
0 → 100644
View file @
3345b763
111111
\ No newline at end of file
target/classes/com/exercise/fileio/ExerciseFirst.class
0 → 100644
View file @
3345b763
File added
target/classes/com/exercise/fileio/ExerciseFourth.class
0 → 100644
View file @
3345b763
File added
target/classes/com/exercise/fileio/ExerciseSecond.class
0 → 100644
View file @
3345b763
File added
target/classes/com/exercise/fileio/ExerciseThird.class
0 → 100644
View file @
3345b763
File added
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