(一)IO编程题01
* 在指定的路径下新建一个 .txt 文件 "test.txt",利用程序在文件中写入如下内容:
public class Test01 {
public static void main(String[] args) {
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.txt"));
dos.writeUTF("hello wrod");
dos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dos != null){
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
* 2. 利用程序读取 test.txt 文件的内容, 并在控制台打印
public class Test02 {
public static void main(String[] args) {
InputStreamReader osw = null;
try {
FileInputStream fos = new FileInputStream("C:\\Users\\Administrator\\Desktop\\test.txt");
osw = new InputStreamReader(fos,"utf-8");
char[] cbuf = new char[1024];
int len;
while ((len = osw.read(cbuf)) != -1){
String str = new String(cbuf,0,len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
* 3. 利用程序复制 test.txt 为 test1.txt
public class Test03 {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("C:\\Users\\Administrator\\Desktop\\test.txt")));
bw = new BufferedWriter(new FileWriter(new File("C:\\Users\\Administrator\\Desktop\\test1.txt")));
char[] cbuf = new char[1024];
int len;
while ((len = br.read(cbuf)) != -1){
bw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
* 4. 列出当前目录下全部java文件的名称
public class Test04 {
public static void main(String[] args) {
File src = new File("I:\\cd9");
String[] fileNames = src.list();
if (fileNames != null){
for (String fileName : fileNames){
if (fileName.endsWith(".java")){
System.out.println(fileName);
}
}
}
}
}
* 5. 列出d:\ch9目录下Java源文件的名字及其大小,并删除其中的一个Java源文件?
public class Test05 {
public static void main(String[] args) {
File fe = new File("I:\\cd9");
String[] fileNames = fe.list();
long size = 0;
if (fileNames != null){
for (String fileName : fileNames) {
if (fileName.endsWith(".java")){
size = fileNames.length;
System.out.println("文件名字:" + fileName + " " + "文件大小:" + size);
}
}
}
delete(fe);
}
public static void delete(File fe){
File[] fi = fe.listFiles();
for (File file : fi){
String filename = file.getName();
if (filename.endsWith(".java")){
System.out.println("成功删除:" + file.getName());
file.delete();
break;
}
}
}
}
(二)IO编程题02
* 7. 操作I:\cd9下的my.txt文件
* 1) 判断my.txt文件是否存在
* 2) 若存在则删除;若不存在则创建
public class Test01{
public static void main(String[] args) {
File fe = new File("I:\\cd9\\my.txt");
if (!fe.exists()){
try {
fe.createNewFile();
} catch (IOException e) {
}
System.out.println("创建成功");
}else {
fe.delete();
System.out.println("删除成功");
}
}
}
* 9. 使用File类删除某个文件夹(例如D盘下的temp文件夹)下的所有文件和文件夹:
* 1) 判断temp文件夹下的文件类型,如果是文件则直接删除
* 2) 如果是文件夹则获取该文件夹下的子文件和文件夹
* 3) 使用递归的方式删除所有temp文件夹下的文件和文件夹
public class Test02{
public static void main(String[] args) {
File fe = new File("I:\\cd9\\");
judge(fe);
File dl =