1 ) 实现字节数组和任何基本类型和引用类型执行的相互转换
提示:使用ByteArrayInutStream和ByteArrayOutputStream。
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Date;
public class Work01 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 字节数组输入流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// 对象输出流
ObjectOutputStream oos = new ObjectOutputStream(bos);
// 操作(写)
oos.writeBoolean(false);
oos.writeUTF("你好,世界!");
oos.writeInt(123);
oos.writeObject("Hello,world");
// 时间对象的序列化
oos.writeObject(new Date());
// 自定义类对象的序列化
oos.writeObject(new Students("小万", 98, 18));
byte[] datas = bos.toByteArray();
System.out.println(Arrays.toString(datas));
// 创建字节数组输入流
ByteArrayInputStream bis = new ByteArrayInputStream(datas);
// 创建对象输入流
ObjectInputStream ois = new ObjectInputStream(bis);
// 操作(读取数据,按照顺序)
boolean flag = ois.readBoolean();
System.out.println(flag);
String str1 = ois.readUTF();
System.out.println(str1);
int a = ois.readInt();
System.out.println(a);
Object ss = ois.readObject();
if(ss instanceof String) {
String str = (String)ss;
System.out.println(str);
}
Object obj = ois.readObject();
if(obj instanceof Date) {
Date date = (Date)obj;
System.out.println(date);
}
obj = ois.readObject();
if(obj instanceof Students) {
Students stu = (Students)obj;
System.out.println(""+stu.getName()+"--"+stu.getScore()+"--"+stu.getAge());
System.out.println();
}
}
}
class Students implements java.io.Serializable{
private String name;
private int score;
private transient int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Students(String name, int score, int age) {
super();
this.name = name;
this.score = score;
this.age = age;
}
}
2 ) 复制文件夹d:/sxtjava下面所有文件和子文件夹内容到d:/sxtjava2。
提示:涉及单个文件复制、目录的创建、递归的使用
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Work02 {
private static File srcFile = new File("/home/sweetheart/eclipse-workspace");
private static File destFile = new File("/home/sweetheart/eclipse-workspace-copy");
private static boolean flag = true;
private static String fileName = "/home/sweetheart/eclipse-workspace";
private static String fileName1 = "/home/sweetheart/eclipse-workspace-copy";
public static void main(String[] args) {
copy(srcFile);
}
public static void copy(File f) {
if(flag) { // 如果是顶层文件夹
flag = false;
destFile.mkdir();
File[] files = f.listFiles();
for(File file:files) {
copy(file);
}
}else {
String destpath = f.getAbsolutePath().replace(fileName, fileName1);
System.out.println(destpath);
File f1 = new File(destpath);
if(f.isFile()) {
// 是文件的话直接拷贝
copyDetial(f,f1);
}else {
// 文件夹的话先创建
f1.mkdir();
// 列出子级文件对象
File[] files = f.listFiles();
// 递归调用
for(File file:files) {
copy(file);
}
}
}
}
// 拷贝具体的文件(从源路径到目标路径)
public static void copyDetial(File src,File dest){
// 选择流
try(InputStream is = new FileInputStream(src);
OutputStream os = new FileOutputStream(dest,true)){
// 操作(拷贝)
int len = -1;
byte[] datas = new byte[1024];
while((len=is.read(datas))!=-1) {
os.write(datas,0,len);
}
os.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3 ) 使用IO包中的类读取D盘上exam.txt文本文件的内容,每次读取一行内容,将每行作为一个输入放入ArrayList的泛型集合中并将集合中的内容使用加强for进行输出显示。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Work03 {
public static void main(String[] args) {
// 创建集合用来存储读取到的数据
List<String> textList = new ArrayList<>();
// 创建源
File src = new File("exam.txt");
// 选择流(采用try-catch-with...关闭)
try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(src)))){
String strLine = null;
// 循环读取并添加到集合中
while((strLine=br.readLine())!=null) {
textList.add(strLine);
}
// 利用增强for打印
for(String str:textList) {
System.out.println(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}