黑马程序员——IO输入与输出(二)


--------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------

 一、其他对象
    (一)System类
         1、获取系统属性信息:Properties getProperties();
import java.util.*; 
class SystemDemo 
{
 public static void main(String[] args) 
 {
  Properties prop = System.getProperties();
  //因为Properties是Hashtable的子类,也就是Map集合的一个子类对象。
  //那么可以通过Map的方法取出该集合中的元素
  //该集合存储的都是字符串,没有泛型定义。

  //在系统中自定义系统信息
  System.setProperty("myname","beyond");
  //获取单个系统信息
  String value = System.getProperty("os.name");
  System.out.println(value);
  //在虚拟机启动时,动态加载一些属性信息 java -Dhaha=qqqq SyetemDemo
  String v=System.getProperty("haha");
  System.out.println(v);
  //或取所有系统信息
  for(Object obj:prop.keySet())
  {
   String values=(String)prop.get(obj);
   System.out.println(obj+"::"+value);
  }
  
 }
}
      (二)Runtime类
         每个java应用程序都一个Runtime类实例,是应用程序能够与其运行的环境相连接,可以通过getRuntime方法获取当前运行时。
          1、static  Runtime getRuntime();
class RuntimeDemo 
{
 public static void main(String[] args) 
 {
  Runtime r = Runtime.getRuntime();throws Exception
 Process p=r.exec("c:\\winmine.exe");//执行一个程序。Process是一个类。

  p.destroy();//杀进程
  System.out.println("Hello World!");
 }
}
       (三)Date类
import java.util.*;
import java.text.*;
class  DateDemo
{
 public static void main(String[] args) 
 {
  Date d= new Date();
  System.out.println(d);
  //将模式封装到SimpleDateFormat对象中
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日hh:mm:ss");
  //调用format方法让模式格式化指定Date对象
  String time= sdf.format(d);
  System.out.println(time);
 }
}
      (四)Calendar类
import java.util.*;
import java.text*;
class CalendarDemo 
{
 public static void main(String[] args) 
 {
  Calendar c = Calendar.getInstance();
  //获取年月日
  sop(c.get(Calendar.YEAR)+"年");
  //设置年月日
  c.set(2012,3,23);
  //对时间量的偏移
  c.add(Calendar.DAY_OF_MONTH,-18)//将日期向后退18天,显示18天后的日期
  /*
  Date d = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
   String year =sdf.format(d);
  System.out.println(year);
  */
 }
 public static void sop(Object obj)
 {
  System.out.println(obj);
 }
}
         (五)Math类
class  MathDemo
{
 public static void main(String[] args) 
 {
  double b=Math.ceil(12.35);//向上取整
  sop(b);
  double b1=Math.floor(111.01);//向下取整
  double b2=Math.round(15.34);//四舍五入
  double b5=Math.pow(2,3);//2的3次方
  sop(b5);
  double b3 =Math.random();//产生一个>=0.0且<1的一个伪随机数
  int b4 =(int)(Math.random()*10+1);//产生一个1到10之间的随机数
 }
 public static void sop(Object obj)
 {
  System.out.println(obj);
 }
}


二、File类
  用来将文件或文件夹封装成对象,方便对文件与文件夹的属性信息进行操作,File对象可以作为参数传递给流的构造函数。
  (一)File类方法演示
       1、File类的构造方法
import java.io.*;
class  FileDemo
{
 public static void main(String[] args) 
 {
  constructMethod();
 }
 public static void constructMethod()
 {
  //将文件封装成对象
  File f1=new File("a.txt");
  File f2=new File("D:\\EditPlus 3","a.txt");
  //先封装一个父目录对象
  File d=new File("D:\\EditPlus 3");
  File f3=new File(d,"a.txt");
  //使用java分隔符,创建跨平台的文件对象
  File f4=
                  new File("D:"+File.separator+"EditPlus 3"+File.separator+"a.txt");
  sop(f1);
  sop(f2);
  sop(f3);
  sop(f4);
 }
 public static void sop(Object obj)
 {
  System.out.println(obj);
 }
}
        2、创建、删除、判断、获取信息
           创建:boolean createNewFile();在指定位置创建文件,若创建成功,则返回 true。若该文件已经存在,则不创建,返回false。这和输出流不一样,输出流对象 一建立就创建文件,而且会覆盖已经存在的文件。
            boolean mkdir();创建文件夹
            boolean mkdirs();创建多级文件夹
           删除:boolean delete();删除失败返回false。 
                 void deleteOnExit();在程序退出时删除指定文件。
           判断:boolean exists();文件是否存在。
                  isFile();
                  isDirectory();
                  isNidden();判断是否被隐藏。
                  isAbsolute();判断是否绝对的。
           获取:getName();
                  getPath();
                  getParent();
                  getAbsolutePath();
                  long lastModified();获取最后一次修改的时间
                  long length();获取文件的长度

三、Properties简述
   Properties是Hashtable的子类,也就是说它具备Map集合的特点。而且它里面存储的键值对都是字符串。Properties是集合和IO技术相结合的集合容器,该对象的特点是,可以用于键值对形式的配置文件。
    (一)Properties存取
import java.io.*;
import java.util.*;
class  PropertiesDemo
{
 public static void main(String[] args) 
 {
  setAndGet();
 }
 public static void setAndGet()
 {
  Properties prop = new Properties();
  //设置集合元素
  prop.setProperty("zhangsan","30");
  prop.setProperty("lisi","20");
  //获取集合元素
  Set<String> names = prop.stringPropertyNames();
  for(String name:names)
  {
   System.out.println("name"+name+"::"+prop.getProperty(name));
  }

 }
}
    (二)Properties存取配置文件
        1、方式一:自己定义方法
import java.io.*;
import java.util.*;
class  PropertiesDemo
{
 public static void main(String[] args) throws IOException
 {
  //setAndGet();
  method_1();
 }
 //把配置文件的键值对读到集合中,
 public static void method_1()throws IOException
 {
  //该操作中。源是文本文件。
  BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));
  //创建一个Properties集合用于存储取到的键值对
  Properties prop= new Properties();
  //一次读一行数据
  String line = null;
  while((line=bufr.readLine())!=null)
  {
   //将读到的一行数据用等号分割
   String[] arr = line.split("=");
   //将分割出来的键和值分别存储到集合中
   prop.setProperty(arr[0],arr[1]);
  }
  //关闭读取流
  bufr.close();
  System.out.println(prop);
 }
}
        2、Properties的加载方法load()
import java.io.*;
import java.util.*;
class  PropertiesDemo
{
 public static void main(String[] args) throws IOException
 {
  //setAndGet();
  //method_1();
  loadDemo();
 }
 public static void loadDemo()throws IOException
 {
  Properties prop = new Properties();
  FileInputStream fis = new FileInputStream("info.txt");
  //加载读取流
  prop.load(fis);
  //修改集合中指定键的值
  prop.setProperty("wangjiao","20");
  //将修改后的结果保存到文件
  FileOutputStream fos = new FileOutputStream("info.txt");
  prop.store(fos,"feifei");
  //列出键值对
  prop.list(System.out);
  fis.close();
  fos.close();
 }

 (二)序列流(合并流)
        SequenceInputStream 对多个流进行合并,SequenceInputstream表示其他输入流的逻辑串联,他从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,一次类推,直到到达包含的最后一个输入流的文件末尾为止。
import java.io.*;
import java.util.*;
class  SequenceInputStreamDemo
{
 public static void main(String[] args) throws IOException
 {
  System.out.println("Hello World!");
  seguenceInputStream();
 }
 public static void seguenceInputStream()throws IOException
 {
  //因为要把多个文件合并成一个文件。所以涉及到了多个输入流,
  //为了能够简单的输出到同一个文件,使用合并流将这些读取流合并
  //首先。创建一个带有枚举的集合将这些输入流存储起来
  Vector<FileInputStream> v = new Vector<FileInputStream>();
  //第二步。把文件读取流对象存入到集合中
  v.add(new FileInputStream("1.txt"));
  v.add(new FileInputStream("2.txt"));
  v.add(new FileInputStream("3.txt"));
  //获取集合中的枚举对象,以便将这些流对象取出
  Enumeration<FileInputStream> en = v.elements();
  //第三步,创建一合并流,将集合中的流合并一个文件读取流
  SequenceInputStream sis = new SequenceInputStream(en);
  //第四步。创建一个文件输出流,将读取到的数据存入到指定文件
  FileOutputStream fos = new FileOutputStream("4.txt");
  byte[] buf = new byte[1024];
  int len=0;
  while((len=sis.read(buf))!=-1)
  {
   fos.write(buf,0,len);
  }
  sis.close();
  fos.close();
 }
}
    (三)切割文件
import java.io.*;
import java.util.*;
class SplitFile 
{
 public static void main(String[] args)throws IOException 
 {
  splitFile();

 }
 public static void splitFile()throws IOException
 {
  //创建一个文件读取流,将要切割的文件读取进来
  FileInputStream fis = new FileInputStream("c:\\1.bmp");
  //文件分割的思想是将文件分段读取分段存储
  byte[] buf = new byte[1024*1024];
  FileOutputStream fos=null;
  int len= 0;
  count=0;
  while((len=fis.read(buf))!=-1)
  {
   fos = new FileOutputStream("c:\\"+(count++)+".part");
   fos.write(buf,0,len);
   fos.close();
  }
  fis.close();
 }
 public static void merge()throws IOException
 {
  ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
  for (int x=1;x<=3;x++)
  {
   al.add(new FileInputStream("C:\\"+x+".part"));
  }
  //内部类访问的成员要final修饰
  final Iterator<FileInputStream> it = al.iterator();
  Enumeration en = new Enumeration<FileInputStream>()
  {
   public boolean hasMoreElements()
   {
    return it.hasNext();
   }
   public FileInputStream nextElement()
   {
    return it.next();
   }
  };
  SequenceInputStream sis = new SequenceInputStream(en);
  FileOutputStream fos = new FileOutputStream("c:\\2.bmp");
  byte[] buf = new byte[1024];
  int len = 0;
  while ((len=sis.read(buf)!=-1)
  {
   fos.write(buf,0,len);
  }
  sis.close();
  fos.close();
 }
}

    (四)操作对象(对象的序列化)
        ObjectInputStream与ObjectOutputStream 被操作的对象需要实现Serializable(标记接口),此接口中没有方法,这个接口实际上是给类加上UID号。
import java.io.*;
class ObjectStreamDemo 
{
 public static void main(String[] args)throws Exception 
 {
  //writeObj();
  readObj();
 }
 public static void writeObj()throws IOException
 {
  //将一个对象数据写到文本文件中
  //创建一个能操作对象的写入流对象,该对象实际上是对操作文件对象的一个                  装饰
  ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
  oos.writeObject(new Person("zhansan",30));
  oos.close();
 }
 public static void readObj()throws Exception
 {
  //把文本中的对象读出来
  ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
  Person p =(Person)ois.readObject();
  System.out.println(p);
  ois.close();
 }
}
//想要被流对象操作的类必须实现Serializable。注意:静态是不能被序列化的,因为在方法区
//如果给对象的属性加上transient,则该属性也不能被序列化。
class Person implements Serializable
{
 //自定义一个序列号,当该类中有改变时,以前存储到文件的对象还能被读取
 public static final long serialVersionUID=42L;//给类定义一个固定标示 
 private String name;
 private int age;
 Person(String name,int age)
 {
  this.name=name;
  this.age=age;
 }
 public String toString()
 {
  return name+":"+age;
 }
}

       (五)管道流
             PipedInputStream和PipedOutputStream 输入输出可以直接进行连接,不需要中转站,通过结合线程使用。
           1、PipedInputStream管道输入流应该接到管道输出流;管道输入流提供要写入管道输出流的所有数据字节。通常,数据由某个线程从PipedInputStream对象读取,并由其他线程将其写入到相应的PipedOutputStream。不建议对这两个对象尝试使用单线程,因为这样可能思索线程。
import java.io.*;
class Read implements Runnable
{
 private PipedInputStream in;
 Read(PipedInputStream in)
 {
  this.in=in;
 }
 public void run()
 {
  try
  {
   byte[] buf =new byte[1024];
    int len = in.read(buf);
   
    String s = new String(buf,0,len);
    //管道读到数据后。将其打印出来
    System.out.println(s);
  
   in.close();
  }
  catch (IOException e)
  {
   throw new RuntimeException("管道读取失败");
  } 
 }
}
class Write implements Runnable
{
 private PipedOutputStream out;
 Write(PipedOutputStream out)
 {
  this.out = out;
 }
 public void run()
 {
 try
 {
  out.write("gai dao lai le".getBytes());
  out.close();
 }
 catch (IOException e)
 {
  throw new RuntimeException("管道输出流失败");
 }
 }
}
class  PipedStreamDemo
{
 public static void main(String[] args)throws IOException 
 {
  //创建管道输入流和管道输出流的对象
  PipedInputStream in = new PipedInputStream();
  PipedOutputStream out = new PipedOutputStream();
  in.connect(out);
  //将管道流封装到线程当中
  Read r =new Read(in);
  Write w = new Write(out);
  new Thread(r).start();
  new Thread(w).start();

 }
}
      (六)RandomAccessFile
          随机访问文件,自身具备读写方法。随机访问文件的行为类似存储在文件系统中的一个大型的byte数组。通过skipBytes(int x),seek(int x);来达到随机访问。
         该类不算是不算是IO体系中的子类,而是直接继承自Object。但是它是IO包中的成员,因为它具备读和写的功能。内部封装了一个数组,而且通过指针对数组进行操作,同时可以通过seek改变指针的位置。
          其实完成读写的原理就是内部封装了字节流输入和输出。通过构造函数可以看出,该类只能操作文件。而且操作文件还有模式:只读r,读写rw等。如果模式为只读r,不会创建文件,回去读取一个已存在的文件,如果该文件不存在,则会出现异常。如果模式为rw,操作的文件不存在,会自动创建。如果存在则不会覆盖。
import java.io.*;
class  RandomAccessFileDemo
{
 public static void main(String[] args) throws IOException
 {
  //writeFile();
  readFile_2();
 }
 public static void writeFile()throws IOException
 {
  //创建随机读取文件的对象
  RandomAccessFile raf = new RandomAccessFile("raf.txt","rw");
  //由于底层操作的是字节流,所以只能写字节数组
  raf.write("邓飞飞".getBytes());
  raf.writeInt(23);//为了以后的随机读取方便采用int类型的数据表示年龄
  raf.write("王泽娇".getBytes());
  raf.writeInt(24);
  raf.close();
 }
 public static void readFile_1()throws IOException
 {
  RandomAccessFile raf= new RandomAccessFile("raf.txt","r");//文件模式 设为只读
  //读取方式和字节流文件的操作方式一样
  byte[] buf = new byte[6];
  raf.read(buf);
  String name = new String(buf);
  int age = raf.readInt();
  System.out.println("name"+name+"::"+age);
  raf.close();
 }
 public static void readFile_2()throws IOException
 {
  RandomAccessFile raf= new RandomAccessFile("raf.txt","r");//文件模式 设为只读
  //读取方式和字节流文件的操作方式一样
  //调整对象中的指针的位置
  //raf.seek(10);
  //跳过指定的字节数读取。该方法只能向前跳
  raf.skipBytes(10);
  byte[] buf = new byte[6];
  raf.read(buf);
  String name = new String(buf);
  int age = raf.readInt();
  System.out.println("name"+name+"::"+age);
  raf.close();
 }

}
        (七)操作基本数据类型的流对象
           DataInputStream与DataOutputStream可用与操作基本类型数据的流对象
import java.io.*;
class  DataStreamDemo
{
 public static void main(String[] args)throws IOException 
 {
  //writeData();
  //readData();
  writeUTFDemo();
  readUTFDemo();
 }
 public static void writeData()throws IOException
 {
  //将基本数据类型写到文件中
  DataOutputStream dos = 
                             new DataOutputStream(new FileOutputStream("data.txt"));
  dos.writeInt(305);
  dos.writeDouble(123.04);
  dos.writeBoolean(true);
  dos.close();

 }
 public static void readData()throws IOException
 {
  //将文件中基本数据读出来,注意:读的顺序和写入的顺序要一致
  DataInputStream  dis =
                               new DataInputStream(new FileInputStream("data.txt"));
  int num = dis.readInt();
  double d=dis.readDouble();
  boolean b = dis.readBoolean();
  System.out.println(num);
  System.out.println(d);
  System.out.println(b);
  dis.close();
 }
 public static void writeUTFDemo()throws IOException
 {
  DataOutputStream dos =
                          new DataOutputStream(new FileOutputStream("utfdata.txt"));
  //使用指定编码集的写方法
  dos.writeUTF("你好");
  dos.close();
 }
 //用指定编码的写方法写的只能用对应的指定编码读的方法读取
 public static void readUTFDemo()throws IOException
 {
  DataInputStream dis = 
                           new DataInputStream(new FileInputStream("utfdata.txt"));
  String s = dis.readUTF();
  System.out.println(s);
  dis.close();
 }
}
       (八)操作字节数组:ByteArrayInputstream与ByteArrayOutputstream
           1.ByteArrayInputstream 包含一个内部缓冲区,该缓冲区包含从流中读取的字节。内部计数器跟踪read方法要提供的下一个字节。因为是操作字节数组,所以没有调用底层资源,所以不用关闭流。而且也不会发生异常。在构造的时候,需要传入数据源作为参数,而且数据源是一个字节数组。
           2.ByteArrayOutputstream 此类实现了一个输出流,其中的数据被写入一个byte数组。缓冲区会随着数据的写入而自动增长。可以使用toByteArray()和toString()获取数据。不用关闭,而且也不会发生异常。在构造的时候,不用定义数据目的,应为该对象已经在内部封装了可边长度的字节数组,这就是数据的目的地。
import java.io.*;
class  ByteArrayStream
{
 public static void main(String[] args) 
 {
  //数据源
  ByteArrayInputStream bis = new ByteArrayInputStream("abcdefg".getBytes());
  //数据目的
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  int by= 0;
  while((by=bis.read())!=-1)
  {
   bos.write(by);
  }
  //输出目的数组的大小
  System.out.println(bos.size());
  //输出目的数组中的内容
  System.out.println(bos.toString());
 }
}
        (九)字符编码
            字符流的出现是为了方便操作字符。更重要的是加入了编码转换。通过子类转换流完成。InputStreamReader和OutputstreamWriter,在两个对象进行构造的时候可以加入字符集
          1.编码:字符串变成字节数组  str.getBytes(charsetname);
          2.解码:字节数组变成字符串   new String(byte[],charsetname); Arrays.toString(byte[]);
import java.io.*;
class  EncodeDemo
{
 public static void main(String[] args) 
 {
  String s= "你好";
  //编码
  byte[] b1 = s.getBytes("GBK");
  System.out.println(Arrays.toString(b1));
  //解码
  String s1 = new String(b1,"GBK");
  System.out.println(s1);

 }
}

                                                       

**********************************************************************************************************************************************************

练习:

1.建立一个java文件列表文件

package cn.kang;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class JavaFileList {


/**
* @param args
* 建立一个java文件列表文件
*/
public static void main(String[] args) {

File dir = new File("/home/kangdawei/workspace");
List<File> list = new ArrayList<File>();

fileToList(dir, list);

//System.out.println(list.size());

File file = new File(dir,"javalist.txt");
writeToFile(list, file.toString());

}

public static void fileToList(File dir, List<File> list){

File[] files = dir.listFiles();

for(File file : files){
if(file.isDirectory()){
fileToList(file, list);
}else{
if(file.getName().endsWith(".java")){
list.add(file);
}
}

}
}

public static void writeToFile(List<File> list,String javaListFile){

BufferedWriter bufw = null;

try {
bufw = new BufferedWriter(new FileWriter(javaListFile));

for(File f: list){
String path = f.getAbsolutePath();
bufw.write(path);
bufw.newLine();
bufw.flush();
}
} catch (IOException e) {
throw new RuntimeException("文件写入失败");
}finally{
if(bufw!=null){
try {
bufw.close();
} catch (IOException e) {
  throw new RuntimeException("文件关闭失败");
}
}
}
}
}

----------------------------------------------------------------------------------------------------------------------------------------------------

2.删除目录:

package cn.kang;


import java.io.File;


public class RemoveDir {


/**
* @param args
*/
public static void main(String[] args) {

File dir = new File("/home/kangdawei/kang");
removeDir(dir);
}

public static void removeDir(File dir){

File[] files = dir.listFiles();

for(int x=0;x<files.length;x++){

if(!files[x].isHidden() && files[x].isDirectory()){
removeDir(files[x]);
}
else{
System.out.println(files[x].toString()+"--file--"+files[x].delete());
}
}
System.out.println(dir+":::dir:::"+dir.delete());
}
}


3.记录应用程序运行的次数

package cn.kang;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class RunCount {

/**
* @param args
* 用于记录应用程序运行的次数
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub


Properties prop = new Properties();

File file = new File("count.ini");
if(!(file.exists()))
file.createNewFile();

FileInputStream fis = new FileInputStream(file);

prop.load(fis);

int count = 0;
String value = prop.getProperty("time");

if(value != null){
count = Integer.parseInt(value);
   if(count>=5){
    System.out.println("交钱!!!");
   }
}
count++;
prop.setProperty("time", count+"");

FileOutputStream fos = new FileOutputStream(file);
prop.store(fos, "");

fos.close();
fis.close();
}
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

4.键盘录入学生信息

package cn.kang;


public class Student implements Comparable<Student>{


private String name;
private int ma,cn,en;
private int sum;
public Student(String name, int ma, int cn, int en) {
super();
this.name = name;
this.ma = ma;
this.cn = cn;
this.en = en;
sum = ma + cn + en;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMa() {
return ma;
}
public void setMa(int ma) {
this.ma = ma;
}
public int getCn() {
return cn;
}
public void setCn(int cn) {
this.cn = cn;
}
public int getEn() {
return en;
}
public void setEn(int en) {
this.en = en;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
@Override
public int hashCode() {


return name.hashCode() + sum*13;
}
@Override
public boolean equals(Object obj) {

if(!(obj instanceof Student)){
throw new RuntimeException("类型不匹配!!!");
}
Student s = (Student) obj;

return this.name.equals(s.name) && this.sum == s.sum;
}
@Override
public int compareTo(Student o) {

int num = new Integer(this.sum).compareTo(new Integer(o.sum));
if(num == 0)
return this.name.compareTo(o.name);
return num;
}
@Override
public String toString() {
return "student["+name+","+ma+","+cn+","+en+"]";
}
}

///

package cn.kang;


import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;


public class StudentInfoTest {


/**
* @param args
* 通过键盘录入数据
* @throws IOException 
*/
public static void main(String[] args) throws IOException {

Comparator<Student> cmp = Collections.reverseOrder();

Set<Student> set = StudentInfoTool.getStudent(cmp);

StudentInfoTool.write2File(set);
}
}

///

package cn.kang;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;


public class StudentInfoTool {


public static Set<Student> getStudent() throws IOException{
return getStudent(null);
}

public static Set<Student> getStudent(Comparator<Student> cmp) throws IOException{

BufferedReader bufr = 
new BufferedReader(new InputStreamReader(System.in));

String line = null;

Set<Student> set = null;
if(cmp ==null){
set = new TreeSet<Student>();
}else{
set = new TreeSet<Student>(cmp);
}
while((line = bufr.readLine())!=null){
if("over".endsWith(line))
break;
String[] info = line.split(",");
Student s = new Student(info[0], 
Integer.parseInt(info[1]),
Integer.parseInt(info[2]), 
Integer.parseInt(info[3]));

set.add(s);
}
bufr.close();
return set;
}

public static void write2File(Set<Student> set) throws IOException{

BufferedWriter bufw = 
new BufferedWriter(new FileWriter("stuinfo.txt"));

for(Student s :set){
bufw.write(s.toString()+"\t");
bufw.write(s.getSum()+"");
bufw.newLine();
bufw.flush();
}
}
}

---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值