PrintWriter和Scanner的综合运用写文件并读文件

下用PrinterWriter写入文件,再用Scanner读文件输出

package textfile;
import java.io.*;
import java.util.*;
public class TextFileTest {
      public static void main(String[] args) throws IOException{
          Employee[] staff=new Employee[3];
          staff[0]=new Employee("张珊珊",1992,12,15);
          staff[1]=new Employee("李花花",1993,2,5);
          staff[2]=new Employee("丘比特",1995,3,12);

          try(PrintWriter out=new PrintWriter("emloyee.txt","UTF-8"))//PrintWriter对象
          {
              writeData(staff,out);
          }

          try(Scanner in=new Scanner(new FileInputStream("emloyee.txt"), "UTF-8"))//Scanner对象
          {

              Employee[] newstaff=readData(in);//读回的数组
              for(Employee e:newstaff)
              {
                  System.out.println(e.getName()+":"+e.getBirthyear()+","+e.getBirthmonth()+","+e.getBirthday());
              }
          }       
      }
      //把emplyoees数组写入到文件中
      public static void writeData(Employee[] emplyoees,PrintWriter out) throws IOException
      {
          out.println(emplyoees.length);
          for(Employee e:emplyoees){
              writeEmployee(out,e);//写一个元素对象
          }
      }
      //写入一个对象
     public static void writeEmployee(PrintWriter out, Employee e) {
  out.println(e.getName()+":"+String.valueOf(e.getBirthyear())+","+String.valueOf(e.getBirthmonth())+","+String.valueOf(e.getBirthday()));
       }
      //从文件中读一个数组
     public static Employee[] readData(Scanner in)
     {
         int n=in.nextInt();
         in.nextLine();
         Employee[] emplyoees=new Employee[n];
         for(int i=0;i<n;i++){
             emplyoees[i]=readEmplyee(in);
         }
         return  emplyoees;
     }
     //读一个对象
    public static Employee readEmplyee(Scanner in) {
        // TODO Auto-generated method stub
         String line=in.nextLine();
         String[] info=line.split("[:,]");

         String n=info[0];
         int y=Integer.parseInt(info[1]);
         int m=Integer.parseInt(info[2]);
         int d=Integer.parseInt(info[3]);
         Employee e=new Employee(n,y,m,d);       
        return e;
    }   
}
public class Employee {
    String name;
    int birthyear,birthmonth,birthday;

    Employee(String n,int y,int m,int d ){
        name=n;
        birthyear=y;
        birthmonth=m;
        birthday=d;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public int getBirthyear() {
        return birthyear;
    }
    public void setBirthyear(int birthyear) {
        this.birthyear = birthyear;
    }

    public int getBirthmonth() {
        return birthmonth;
    }

    public void setBirthmonth(int birthmonth) {
        this.birthmonth = birthmonth;
    }

    public int getBirthday() {
        return birthday;
    }

    public void setBirthday(int birthday) {
        this.birthday = birthday;
    }    
}

这里写图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. PrintStream案例: ```java import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; public class PrintStreamDemo { public static void main(String[] args) { try { File file = new File("output.txt"); FileOutputStream fos = new FileOutputStream(file); PrintStream ps = new PrintStream(fos); ps.println("Hello, PrintStream!"); ps.println("This is a test."); ps.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 2. PrintWriter案例: ```java import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; public class PrintWriterDemo { public static void main(String[] args) { try { File file = new File("output.txt"); PrintWriter pw = new PrintWriter(file); pw.println("Hello, PrintWriter!"); pw.println("This is a test."); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 3. Scanner案例: ```java import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { try { File file = new File("input.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这些案例分别演示了PrintStream、PrintWriterScanner的用法。PrintStream和PrintWriter可以用来向文件或控制台输出内容,Scanner可以用来从文件或控制台取内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值