zoukankan      html  css  js  c++  java
  • 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("
    ");
            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];
        }
    }
  • 相关阅读:
    【注册验证】 控制器(添加数据+后台验证)
    AJAX 简介
    我的博客生涯
    windows xp sp3 下载地址
    get、set、add、remove → (字段、属性)+(委托、事件)
    ArcObjects整体介绍
    委托
    对字符串的处理的又一个强大的工具,正则表达式
    对象序列化存在的原因
    关于文件操作的几个类
  • 原文地址:https://www.cnblogs.com/anholt/p/3656176.html
Copyright © 2011-2022 走看看