-
实现字节数组和任何基本类型和引用类型执行的相互转换
提示:使用ByteArrayInutStream和ByteArrayOutputStream。
package Practice;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Practice01_1 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//基本数据类型--->字节数组
DataToByteArray tba = new DataToByteArray("hello world");
byte[] bt = tba.strToByteArray();
System.out.println(bt.length);
//字节数组---->数据类型
ByteArrayToData bat = new ByteArrayToData(bt);
String str = bat.byteArrayToStr();
System.out.println(str);
System.out.println("***********************************");
//引用类型--->字节数组
Employee emp = new Employee("Luke", 26);
ObjToByteArray otba = new ObjToByteArray(emp);
bt = otba.objToByteArray();
System.out.println(bt.length);
//字节数组--->引用类型
Employee emp1 = null;
ByteArrayToObj baot = new ByteArrayToObj(bt, emp1);
emp1 = (Employee) baot.byteArrayToObj();
System.out.println(emp1.getName()+"---->"+emp1.getAge());
}
}
//基本数据类型--->字节数组
class DataToByteArray{
byte[] bytes;
private String str;
private int num;
private char ch;
private boolean flag;
public DataToByteArray(String str) {
// TODO Auto-generated constructor stub
this.str = str;
}
public DataToByteArray(int num) {
// TODO Auto-generated constructor stub
this.num = num;
}
public DataToByteArray(char ch) {
// TODO Auto-generated constructor stub
this.ch= ch;
}
public DataToByteArray(boolean flag) {
// TODO Auto-generated constructor stub
this.flag = flag;
}
public byte[] strToByteArray() throws IOException {
//1.创建源
//2.选择流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos); //dos-->baos
//3.操作
dos.writeUTF(str);//data-->dos
//void writeUTF(String str)
//使用机器无关的方式使用modified UTF-8编码将字符串写入底层输出流。
//首先,将两个字节写入输出流,就像通过writeShort方法给出要跟随的字节数。 该值是实际写出的字节数,而不是字符串的长度。
//按照长度,字符串的每个字符依次输出,使用修改的UTF-8编码字符。 如果没有异常被抛出,计数器written将增加写入输出流的总字节数。
//这将至少是两个加上长度的str ,最多两加三,长度为str 。
//System.out.println(str.getBytes().length);
//4.释放
close(dos);
bytes = baos.toByteArray(); baos-->byte[]
return bytes;
}
public byte[] intToByteArray() throws IOException {
//1.创建源
//2.选择流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos); //2)dos-->baos
//3.操作
dos.writeInt(num); //1)data-->dos
//4.释放
close(dos);
bytes = baos.toByteArray(); 3)baos-->byte[]
return bytes;
}
public byte[] charToByteArray() throws IOException {
//1.创建源
//2.选择流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos); //2)dos-->baos
//3.操作
dos.writeChar(ch); //1)data-->dos
//4.释放
close(dos);
bytes = baos.toByteArray(); 3)baos-->byte[]
return bytes;
}
public byte[] booleanToByteArray() throws IOException {
//1.创建源
//2.选择流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos); //2)dos-->baos
//3.操作
dos.writeBoolean(flag); //1)data-->dos
//4.释放
close(dos);
bytes = baos.toByteArray(); 3)baos-->byte[]
return bytes;
}
public void close(Closeable... ios) {
for (Closeable io : ios) {
if (io != null) {
try {
io.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//字节数组---->数据类型
class ByteArrayToData{
byte[] bytes;
public ByteArrayToData(byte[] bytes) {
// TODO Auto-generated constructor stub
this.bytes = bytes;
}
public String byteArrayToStr() throws IOException {
//1.创建源
//2.选择流
ByteArrayInputStream bais = new ByteArrayInputStream(bytes); //byte--->bais
DataInputStream dis = new DataInputStream(bais); //bais--->dis
//3.操作
String str = dis.readUTF();
//4.释放
close(dis);
return str;
}
public int byteArrayToInt() throws IOException {
//1.创建源
//2.选择流
ByteArrayInputStream bais = new ByteArrayInputStream(bytes); //byte--->bais
DataInputStream dis = new DataInputStream(bais); //bais--->dis
//3.操作
int num = dis.readInt();
//4.释放
close(dis);
return num;
}
public char byteArrayToChar() throws IOException {
//1.创建源
//2.选择流
ByteArrayInputStream bais = new ByteArrayInputStream(bytes); //byte--->bais
DataInputStream dis = new DataInputStream(bais); //bais--->dis
//3.操作
char ch = dis.readChar();
//4.释放
close(dis);
return ch;
}
public boolean byteArrayToBoolean() throws IOException {
//1.创建源
//2.选择流
ByteArrayInputStream bais = new ByteArrayInputStream(bytes); //byte--->bais
DataInputStream dis = new DataInputStream(bais); //bais--->dis
//3.操作
boolean flag = dis.readBoolean();
//4.释放
close(dis);
return flag;
}
public void close(Closeable... ios) {
for (Closeable io : ios) {
if (io != null) {
try {
io.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//引用类型--->字节数组
class ObjToByteArray{
private Object obj;
private byte[] bytes;
public ObjToByteArray(Object obj) {
// TODO Auto-generated constructor stub
this.obj = obj;
}
public byte[] objToByteArray() throws IOException {
//1.创建源
//2.选择流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos); //b)oos-->baos
//3.操作
oos.writeObject(obj); //a)obj-->oos
bytes = baos.toByteArray(); //c)baos-->byte[]
//4.释放
close(oos);
return bytes;
}
public void close(Closeable... ios) {
for (Closeable io : ios) {
if (io != null) {
try {
io.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//字节数组--->引用类型
class ByteArrayToObj{
private byte[] bytes;
private Object obj;
public ByteArrayToObj(byte[] bytes, Object obj) {
// TODO Auto-generated constructor stub
this.bytes = bytes;
this.obj = obj;
}
public Object byteArrayToObj() throws IOException, ClassNotFoundException {
//1.创建源
//2.选择流
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
//3.操作
obj = ois.readObject();
//4.释放
close(ois);
return obj;
}
public void close(Closeable... ios) {
for (Closeable io : ios) {
if (io != null) {
try {
io.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//对象Employee---必须可序列化
class Employee implements Serializable {
private String name;
private int age;
/**
* @param name
* @param age
*/
public Employee(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
-
复制文件夹d:/sxtjava下面所有文件和子文件夹内容到d:/sxtjava2。
提示:涉及单个文件复制、目录的创建、递归的使用
package Practice;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 2. 复制文件夹d:/sxtjava下面所有文件和子文件夹内容到d:/sxtjava2。
提示:涉及单个文件复制、目录的创建、递归的使用
思路:
1.先遍历文件,得到文件名和文件夹名
2.生成目标路径,
如果目标路径为目录,新建文件夹;
如果目标路径为文件,copy原文件到目标文件
* @author lei.ai
*
*/
public class Practice02 {
public static void main(String[] args) throws IOException {
copy("D:/code/Pro08_IO/copy", "copy2", 0);
}
public static void copy(String srcpath, String destpath, int i) throws IOException {
File src = new File(srcpath);
File dest = new File(destpath);
dest.mkdirs(); //创建目的地目录
String srcName;
srcName = src.getName(); //原文件名或者文件夹名
if (null == src || !src.exists()) { //递归头
return;
} else if (src.isFile()) {
destpath = destpath +"/"+ srcName;
dest = new File(destpath);
// System.out.println(destpath);
copyFile(src, dest);
} else {
if (i != 0) { //如果i=0,就不将原文件夹也创建到目的地文件夹下
//如果初始i就不为0,那么一开始就会执行以下内容,使原文件夹也会被复制到目标文件夹下
destpath = destpath + "/" + srcName;
}
i++;
// System.out.println(destpath);
File[] files = src.listFiles();
for (File file : files) {
copy(file.getAbsolutePath(), destpath, i);
}
}
}
public static void copyFile(File src, File dest) throws IOException {
//1.创建源
//2.选择流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
//3.操作
byte[] flush = new byte[1024];
int len;
while ((len = bis.read(flush))!=-1) {
bos.write(flush, 0, len);
bos.flush();
}
//4.释放资源---分别释放,先打开的后关闭
if (bos != null) {
bos.close();
}
if (bis != null) {
bis.close();
}
}
}
- 使用IO包中的类读取D盘上exam.txt文本文件的内容,每次读取一行内容,将每行作为一个输入放入ArrayList的泛型集合中并将集合中的内容使用加强for进行输出显示。
package Practice;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;
/**
* 3. 使用IO包中的类读取D盘上exam.txt文本文件的内容,每次读取一行内容,
* 将每行作为一个输入放入ArrayList的泛型集合中并将集合中的内容使用加强for进行输出显示。
* @author lei.ai
*
*/
public class Practice03 {
public static void main(String[] args) throws IOException {
File src = new File("dest.txt");
List<String> list = FileUtils.readLines(src,"utf-8");
for (String str : list) {
System.out.println(str);
}
}
}