Java基础第二十讲:输入输出(二)
如果您发现本文排版有问题,可以先点击下面的链接切换至老版进行查看!!!
Java基础第二十讲:输入输出(二)
001 |
import java.io.BufferedReader; |
002 |
import java.io.File; |
003 |
import java.io.FileNotFoundException; |
004 |
import java.io.FileReader; |
005 |
import java.io.FileWriter; |
006 |
import java.io.IOException; |
007 |
import java.io.InputStreamReader; |
008 |
009 |
public class HappyChristmas { |
010 |
011 |
// 创建多层文件夹 |
012 |
public static boolean createDirectory(File parent, int level) { |
013 |
014 |
File[] fa = new File[ 10 ]; |
015 |
for ( int i = 0 ; i < level; i++) { |
016 |
for ( int j = 0 ; j < 10 ; j++) { |
017 |
fa[j] = new File(parent, "" + j); |
018 |
fa[j].mkdir(); |
019 |
createDirectory(fa[j], level - (i + 1 )); |
020 |
} |
021 |
} |
022 |
023 |
return true ; |
024 |
} |
025 |
026 |
// 删除文件夹 |
027 |
public static boolean deleteDirectory(File file) { |
028 |
if (file.delete()) { |
029 |
return true ; |
030 |
} else { |
031 |
File[] files = file.listFiles(); |
032 |
for ( int i = 0 ; i < files.length; i++) { |
033 |
if (files[i].isFile()) { |
034 |
files[i].delete(); |
035 |
} else { |
036 |
deleteDirectory(files[i]); |
037 |
} |
038 |
} |
039 |
if (file.delete()) { |
040 |
return true ; |
041 |
} |
042 |
} |
043 |
return true ; |
044 |
045 |
} |
046 |
047 |
public static void main(String[] args) { |
048 |
049 |
// 创建一个目录 c:\happy |
050 |
051 |
File file1 = new File( "c:\\happy" ); |
052 |
053 |
if (file1.mkdir()) { |
054 |
System.out.println( "c:\\happy" + "目录创建成功" ); |
055 |
} else { |
056 |
System.out.println( "c:\\happy" + "目录创建失败" ); |
057 |
} |
058 |
059 |
// 创建一个文件 c:\java\happyChristmas.txt |
060 |
File file2 = new File( "c:\\happy\\happyChristmas.txt" ); |
061 |
try { |
062 |
if (file2.createNewFile()) { |
063 |
System.out.println( "c:\\happy\\happyChristmas.txt" + "文件创建成功" ); |
064 |
} else { |
065 |
System.out.println( "c:\\happy\\happyChristmas.txt" + "文件创建失败" ); |
066 |
} |
067 |
} catch (IOException e) { |
068 |
e.printStackTrace(); |
069 |
} |
070 |
071 |
// 创建一个多级目录 c:\java\2\0\1\0\1\2\2\4 |
072 |
073 |
File file3 = new File( "c:\\happy\\2\\0\\1\\0\\1\\2\\2\\4" ); |
074 |
075 |
if (file3.mkdirs()) { |
076 |
System.out |
077 |
.println( "c:\\happy\\2\\0\\1\\0\\1\\2\\2\\4" + "多级目录创建成功" ); |
078 |
} else { |
079 |
System.out |
080 |
.println( "c:\\happy\\2\\0\\1\\0\\1\\2\\2\\4" + "多级目录创建失败" ); |
081 |
} |
082 |
083 |
// 移动happyChristmas.txt到 c:\java\2\0\1\0\1\2\2\4 |
084 |
File file4 = new File( |
085 |
"c:\\happy\\2\\0\\1\\0\\1\\2\\2\\4\\happyChristmas.txt" ); |
086 |
087 |
if (file2.renameTo(file4)) { |
088 |
System.out |
089 |
.println( "c:\\happy\\2\\0\\1\\0\\1\\2\\2\\4\\happyChristmas.txt" |
090 |
+ "文件拷贝成功" ); |
091 |
} else { |
092 |
System.out |
093 |
.println( "c:\\happy\\2\\0\\1\\0\\1\\2\\2\\4\\happyChristmas.txt" |
094 |
+ "文件拷贝失败" ); |
095 |
} |
096 |
097 |
//从控制台输入内容存储到happyChristmas.txt中 |
098 |
BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); |
099 |
System.out.println( "请输入内容:\n" ); |
100 |
try { |
101 |
FileWriter writer = new FileWriter(file4.toString()); |
102 |
103 |
while (!(in.readLine().equalsIgnoreCase( "exit" ))){ |
104 |
String happyString = in.readLine(); |
105 |
System.out.print( "您输入的内容是: \"" +happyString+ "\",正在存储中\n" ); |
106 |
writer.write(happyString+System.getProperty( "line.separator" )); |
107 |
} |
108 |
writer.flush(); |
109 |
writer.close(); |
110 |
111 |
} catch (IOException e) { |
112 |
e.printStackTrace(); |
113 |
} |
114 |
115 |
//从文件中读取 |
116 |
try { |
117 |
FileReader fr = new FileReader(file4.toString()); |
118 |
BufferedReader br = new BufferedReader(fr); |
119 |
String ls = br.readLine(); |
120 |
121 |
System.out.println( "读取中,文件的内容如下:" ); |
122 |
123 |
while (ls!= null ){ |
124 |
System.out.println(ls); |
125 |
ls = br.readLine(); |
126 |
} |
127 |
128 |
} catch (FileNotFoundException e) { |
129 |
e.printStackTrace(); |
130 |
} catch (IOException e) { |
131 |
e.printStackTrace(); |
132 |
} |
133 |
134 |
// 创建8层混淆目录 |
135 |
if (createDirectory(file1, 8 )) { |
136 |
System.out.println( "创建混淆目录成功" ); |
137 |
} else { |
138 |
System.out.println( "创建混淆目录失败" ); |
139 |
} |
140 |
141 |
// 删除文件 c:\happy\2\0\1\0\1\2\2\4\happyChristmas.txt |
142 |
if (file4.delete()) { |
143 |
System.out |
144 |
.println( "c:\\happy\\2\\0\\1\\0\\1\\2\\2\\4\\happyChristmas.txt" |
145 |
+ "删除文件成功" ); |
146 |
} else { |
147 |
System.out |
148 |
.println( "c:\\happy\\2\\0\\1\\0\\1\\2\\2\\4\\happyChristmas.txt" |
149 |
+ "删除文件失败" ); |
150 |
} |
151 |
152 |
// 删除 c:\happy 目录 |
153 |
if (deleteDirectory(file1)) { |
154 |
System.out.println(file1.toString() + "文件夹删除成功" ); |
155 |
} else { |
156 |
System.out.println(file1.toString() + "文件夹删除失败" ); |
157 |
} |
158 |
159 |
} |
160 |
161 |
} |
- 分类:
- Java
更新时间:
上一篇:Java基础第十九讲:输入输出(一)下一篇:第二十一讲:网络编程(一)
相关文章
Java基础第十九讲:输入输出(一)
本讲内容: File、InputStream、OutputStream、Reader、Writer 分享至上: 分享源头 阅读更多…