目录
练习一:文件目录遍历
编写一个程序,遍历打印一个目录下面的所有的目录,包括目录下面的目录。
// :方法一:栈 + 循环遍历
public void test01() {
// File file = new File("D:/Test");
File file = new File("D:/Program Files (x86)/eclipse");
Stack<File> stack = new Stack<File>();
int count = 0; // 计数器--统计文件和文件夹数
stack.push(file); // 压栈
while (!stack.isEmpty()) { // 栈非空跳出循环
file = stack.pop(); // 取出栈顶File
if (file.isDirectory()) { // 如果是文件夹
File[] array = file.listFiles(); // 把文件夹目录记录
for (int i = 0; i < array.length; i++) { // 遍历目录
File file2 = array[i];
System.out.println(++count + ": " + file2.getName()); // 输出
if (array[i].isDirectory()) { // 如果是目录,压栈
stack.push(file2);
}
}
}
}
}
// :方法二:递归
// 缺点栈溢出
public void test011() {
File file = new File("D:\\Test");
//File file = new File("D:/Program Files (x86)/eclipse");
showFileName(file);
}
//递归方法
public void showFileName(File file) {
System.out.println(file.getName()); //打印文件夹或文件名
if(file.isDirectory()) { //如果是目录,则循环遍历
File[] childFiles = file.listFiles();
for (int i = 0; i < childFiles.length; i++) {
showFileName(childFiles[i]); //递归
}
}
}
练习二:目录创建
编写一个程序,判断某个目录是否存在,不存在的话创建
// 2, 编写一个程序,判断某个目录是否存在,不存在的话创建
public void test02() {
File file = new File("D:/java/javase/1.jpg");
if (!file.isDirectory()) { // 不是目录则指向父目录
file = file.getParentFile();
}
if (file.exists()) // 判断目录是否存在
System.out.println("目录存在!");
else { // 不存在则创建
file.mkdirs();
System.out.println("目录创建完成!");
}
}
练习三:多次文件保存
从C盘读一张图片,然后保存到D盘,多次执行重新保存,保证名字不重复。
public void test03() {
InputStream inputStream = null;
OutputStream outputStream = null;
int t = -1;
for (int i = 0; i < 10; i++) {
try { // 读取图片,异常处理--防止图片不存在
inputStream = new FileInputStream(new File("D:/1.jpg"));
// 保存图片的名字通过i改变
outputStream = new FileOutputStream("D:/Test/Test2/picture/" + i + ".jpg");
try {
while ((t = inputStream.read()) != -1) {
outputStream.write(t); // 保存图片
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
练习四:自定义异常处理
提示用户输入姓名,年龄,如果用户年龄输入不合法,捕获异常,给出输入错误提示。
public class User2Exception {
private String nameString;
private int age;
public String getNameString() { return nameString; }
public void setNameString(String nameString) { this.nameString = nameString; }
public int getAge() { return age; }
public void setAge(int age) throws Exception {
if (age < 0 || age > 150)
throw new Exception("年龄范围异常!"); //抛出异常
else
this.age = age;
}
}
public void test04() {
User2Exception user2Exception = new User2Exception();
Scanner scanner = new Scanner(System.in);
System.out.println("信息录入,请输入姓名:");
user2Exception.setNameString(scanner.next());
System.out.println("信息录入,请输入年龄:");
try { // 异常处理
user2Exception.setAge(scanner.nextInt());
} catch (Exception e) {
System.out.println(e);
}
System.out.println(user2Exception.getNameString());
System.out.println(user2Exception.getAge());
}
练习五:自定义异常类
定义异常类MyException 继承Exception,定义方法 toString() 提示错误
定义类Suan,定义方法计算两个数加法,如果和<50 抛出异常 MyException,否则返回和
定义MainDemo类,调用Suan计算方法,捕获异常,输出错误信息
//定义异常类MyException 继承Exception,定义方法 toString()提示错误
public class MyExceptions extends Exception {
/**
* 使用默认的serialVersionUID----序列化ID
*/
private static final long serialVersionUID = 1L;
@Override
public String toString() {
return "太小了!";
}
}
//定义类Suan,定义方法计算两个数加法,如果和<50 抛出异常 MyExceptions,否则返回和
public class Suan {
public int SuanAdd(int a, int b) throws MyExceptions { //throws MyExceptions
if ((a + b) < 50) {
throw new MyExceptions(); //和<50 抛出异常
} else
return (a + b);
}
}
//定义MainDemo类,调用Suan计算方法,捕获异常,输出错误信息
public class MainDemo {
public static void main(String[] args) {
Suan suan = new Suan();
try {
suan.SuanAdd(3, 9);//3+9=12 < 50 --将触发异常
} catch (MyExceptions e) {
e.printStackTrace();
}
}
}