java 读取文件,内容方置Person 并写到另外地址
a.txt 文本内容如下:
name=user
age=34
image=aa.PNG
url=E:\\
package cn.com.test05; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; class Person{ String name; int age; String image; byte[] imageB; String url; public String getName() { return name; } public void setName(String name) { System.out.println(Thread.currentThread().getName()); this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public byte[] getImageB() { return imageB; } public void setImageB(byte[] imageB) { this.imageB = imageB; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String toString(){ return "我叫"+name+"=="+age+"===="+image; } } public class t03 { public static void main(String[] args) throws Exception { Person p= new Person(); t03.get(p); System.out.println(p); out(p); } public static void out(Person p) throws Exception{ File f = new File(p.url+"a.txt"); OutputStream out= new FileOutputStream(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) throws Exception{ File f = new File(url+image); OutputStream out= new FileOutputStream(f); out.write(b); out.close(); } public static void get(Person p) throws Exception{ File f = new File("F:\\a.txt"); InputStream in= new FileInputStream(f); byte[] b=new byte[(int) f.length()]; in.read(b); String str = new String(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) throws Exception{ File f = new File("F:\\"+str); InputStream in= new FileInputStream(f); byte[] b=new byte[(int) f.length()]; in.read(b); in.close(); return b; } public static String getValue(String str){ return str.split("=")[1]; } }