java 读取文件,内容方置Person 并写到另外地址
生活随笔
收集整理的這篇文章主要介紹了
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];} }
?
轉載于:https://www.cnblogs.com/anholt/p/3656176.html
總結
以上是生活随笔為你收集整理的java 读取文件,内容方置Person 并写到另外地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阑尾炎手术多少钱啊?
- 下一篇: 求一个好听的3字名字