java将文件读取到其他地方_java 读取文件,内容方置Person 并写到另外地址

packagecn.com.test05;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.InputStream;importjava.io.OutputStream;classPerson{

String name;intage;

String image;byte[] imageB;

String url;publicString getName() {returnname;

}public voidsetName(String name) {

System.out.println(Thread.currentThread().getName());this.name =name;

}public intgetAge() {returnage;

}public void setAge(intage) {this.age =age;

}publicString getImage() {returnimage;

}public voidsetImage(String image) {this.image =image;

}public byte[] getImageB() {returnimageB;

}public void setImageB(byte[] imageB) {this.imageB =imageB;

}publicString getUrl() {returnurl;

}public voidsetUrl(String url) {this.url =url;

}publicString toString(){return "我叫"+name+"=="+age+"===="+image;

}

}public classt03 {public static void main(String[] args) throwsException {

Person p= newPerson();

t03.get(p);

System.out.println(p);

out(p);

}public static void out(Person p) throwsException{

File f= new File(p.url+"a.txt");

OutputStream out= newFileOutputStream(f);

out.write(p.toString().getBytes());

outImage(p.getUrl(),p.getImage(),p.getImageB());

out.close();

}public static void outImage(String url,String image,byte[] b) throwsException{

File f= new File(url+image);

OutputStream out= newFileOutputStream(f);

out.write(b);

out.close();

}public static void get(Person p) throwsException{

File f= new File("F:\\a.txt");

InputStream in= newFileInputStream(f);byte[] b=new byte[(int) f.length()];

in.read(b);

String str= newString(b);

System.out.println(str);

String[] s= str.split("\r\n");

p.setName(getValue(s[0]));

p.setAge(Integer.parseInt(getValue(s[1])));

p.setImage(getValue(s[2]));

p.setUrl(getValue(s[3]));

p.setImageB(getImageB(getValue(s[2])));

in.close();

}public static byte[] getImageB(String str) throwsException{

File f= new File("F:\\"+str);

InputStream in= newFileInputStream(f);byte[] b=new byte[(int) f.length()];

in.read(b);

in.close();returnb;

}public staticString getValue(String str){return str.split("=")[1];

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值