面向对象技术书面作业6 (io)
1.什么是输入流?什么是输出流?
2.什么是字节数据流?什么是字符数据流,两者的不同是什么?
3.程序中如何获取当前工作目录?
4.程序中如何判断文件是否存在, 是否可读,是否可写?
5.程序中如何创建路径?
6.选择一个.java文件,将该文件内容显示在控制台上,然后将其拷贝到d:\cfile\yy.java文件中,同时统计该文件夹中文件的总个数,并输出所有文件的名称。
1.什么是输入流?什么是输出流?
输入和输出都是相对于内存来说的
输入是到内存里面去也叫作读
输出是从内存中出来也叫作写
2.什么是字节数据流?什么是字符数据流,两者的不同是什么?
字节流是一次读取一个byte,是万能流什么文件都能读
字符流是一次读取一个字符,只能读取txt文件
3.程序中如何获取当前工作目录?
//程序中获取当前工作目录
public class Test01 {
public static void main(String[] args) {
File file =new File("");
System.out.println("当前工作目录是 " + file.getAbsolutePath());
}
}
//4.程序中如何判断文件是否存在, 是否可读,是否可写?
public class Test02 {
public static void main(String[] args) {
File file =new File("C:\\Users\\JavaSE\\javase作业\\src\\javase\\Test7\\Test02.java");
//判断文件是是否存在
System.out.println(file.exists());
//判断文件是否可写
System.out.println(file.canWrite());
//判断文件是否可读
System.out.println(file.canRead());
}
}
5.程序中如何创建路径?
import java.io.File;
//5.程序中如何创建路径?
public class Test03 {
public static void main(String[] args) {
File file =new File("D:\\Test");
//判断目录是否存在
System.out.println(file.exists());//false
//判断文件是否存在,如果不存在,则以文件形式创建
if (!file.exists()){
file.mkdir();
}
System.out.println(file.exists());//true
}
}
6.选择一个.java文件,将该文件内容显示在控制台上,然后将其拷贝到d:\cfile\yy.java文件中,同时统计该文件夹中文件的总个数,并输出所有文件的名称。
注:这里搞错了,其实可以直接用IO流的,不需要用到反射,看到将文件内容显示到控制台,下意识想到了反射,用IO流的方法,具体可参考上机作业中有类似的
public class Student {
//属性
public String name;
private int id;
//构造方法
public Student() {
}
public Student(String name, int id) {
this.name = name;
this.id = id;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class Test04 {
public static void main(String[] args) {
//获得class文件
Class studentClass = null;
try {
studentClass = Class.forName("javase.Test7.Student");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//获得所有属性的数组
Field[] fields = studentClass.getDeclaredFields();
//获得所有构造方法的数组
Constructor[] constructors = studentClass.getConstructors();
//为了拼接字符串
StringBuffer stringBuffer =new StringBuffer();
//开始拼接字符串
stringBuffer.append(Modifier.toString(studentClass.getModifiers()) + " class " + studentClass.getSimpleName() + " {");
stringBuffer.append("\n");
//遍历属性
for (Field field :fields){
stringBuffer.append("\t");
stringBuffer.append(Modifier.toString(field.getModifiers()));
stringBuffer.append(" ");
stringBuffer.append(field.getType().getSimpleName());
stringBuffer.append(" ");
stringBuffer.append(field.getName());
stringBuffer.append(";\n");
}
//遍历构造方法
for (Constructor constructor : constructors){
stringBuffer.append("\t");
stringBuffer.append(Modifier.toString(constructor.getModifiers()));
stringBuffer.append(" ");
stringBuffer.append(studentClass.getSimpleName() + " (");
Class[] parameterTypes= constructor.getParameterTypes();
for (Class p: parameterTypes){
stringBuffer.append(p.getSimpleName());
stringBuffer.append(",");
};
if (parameterTypes.length > 0){
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
}
stringBuffer.append("){}\n");
}
System.out.println(stringBuffer);
//创建字节输出流
FileOutputStream fileOutputStream = null;
//将StringBuffer变为String,因为StringBuffer不能变为byte数组
String string = new String(stringBuffer);
//创建byte数组
byte[] bytes = string.getBytes();
//判断是否存在此文件,不存在则创建
File file1 = new File("D:\\cfile");
if (!file1.exists()){
file1.mkdirs();
}
File file2 = new File("d:\\cfile\\yy.java");
if (!file2.exists()){
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
fileOutputStream = new FileOutputStream("D:\\cfile\\yy.java");
//开始写
fileOutputStream.write(bytes);
//输出流最后要刷新
fileOutputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}