java之IO流(五)

需求:有一个文本文本,需要将文本文件中的内容放到ArrayList集合中,
 遍历集合获取元素 
 源文件:b.txt----->读取---->BuffferedReader

 目的地:ArrayList<String>

举例:

public class FileToArrayListDemo {
public static void main(String[] args) throws IOException {
	//封装源文件
	BufferedReader br=new BufferedReader(
			new FileReader("b.txt"));
	//创建一个ArrayList集合
	ArrayList <String> arr=new ArrayList<String>();
	String line=null;
	while((line=br.readLine())!=null) {
		arr.add(line);
	}
	for(String s:arr) {
		System.out.println(s);
	}
	br.close();
}
}
需求:把ArrayList集合中的字符串数据存储到文本文件 
  ` ArrayList集合存储的元素String,可以存储一些字符串
   使用增强for遍历ArrayList
  使用BufferedWriter(文本文件)
  源文件:ArrayList<String>

  目的地:BufferedWriter输出文本文件,给文件中写入字符

举例:

public class ArrayListToFileDemo {
public static void main(String[] args) throws Exception {
	ArrayList<String> arr=new ArrayList<String>();
	arr.add("synchronized");
	arr.add("hello");
	arr.add("world");
	BufferedWriter bw=new BufferedWriter(
			new FileWriter("aa.txt"));
	for(String s:arr) {
		bw.write(s);
		bw.newLine();
		bw.flush();
	}
	bw.close();
}
}
我有一个文本文件中存储了几个名称,请大家写一个程序实现随机获取一个人的名字。 
1)封装一个文本文件:使用字符缓冲输入流读文件
2)创建一个ArrayList<String>
3)使用字符缓冲输入流readLine(),一次读取一行,就将该行数据添加到集合中
4)创建Random类对象
5)Random类对象.nextInt(集合对象.size()) ;

6)通过角标get(int index ):获取内容

举例:

public class Test {
public static void main(String[] args) throws IOException {
	BufferedReader br=new BufferedReader(
			new FileReader("b.txt"));
	ArrayList<String> arr=new ArrayList<String>();
	String line =null;
	while((line=br.readLine())!=null) {
		arr.add(line);
	}
	br.close();
	Random r=new Random();
	int index=r.nextInt(arr.size());
	String name = arr.get(index);
	System.out.println("幸运的人是:"+name);
}
}
数据流:针对Java基本类型的数据进行读写操作
  数据输入流:DataInputStream

  数据输出流:DataOutputStream 

举例:

public class DataStream {
public static void main(String[] args) throws Exception {
    write();	
	read();
}
private static void read() throws IOException {
    DataInputStream dis=new DataInputStream(
    		new FileInputStream("dos.txt"));
    int i = dis.readInt();
    short s = dis.readShort();
    byte b = dis.readByte();
    double d = dis.readDouble();
    float f = dis.readFloat();
    boolean flag = dis.readBoolean();
    char c = dis.readChar();
    dis.close();
    System.out.println(i);
    System.out.println(s);
    System.out.println(b);
    System.out.println(d);
    System.out.println(f);
    System.out.println(flag);
    System.out.println(c);   
	}
private static void write() throws Exception {		
		DataOutputStream dos = new DataOutputStream(new FileOutputStream("dos.txt")) ;
		//给流中写入数据
		dos.writeInt(10); 
		dos.writeShort(100);
		dos.writeByte(120);
		dos.writeDouble(13.34);
		dos.writeFloat(12.56F);
		dos.writeBoolean(true);
		dos.writeChar('a');
		//关闭资源
		dos.close();
	}
}
内存操作流:适用于临时存储文件.
  内存操作输入流:byteArrayInputStream
ByteArrayInputStream(byte[] buf)
  内存操作输出流: byteArrayOutputStream
  构造方法:ByteArrayOutputStream() 

 内存操作流:一个程序结束后,那么这些程序的变量,就会从内存消失(马上消失的这些数据进行读取写入)

举例:

public class ByteStreamDemo {
public static void main(String[] args) throws IOException {
	ByteArrayOutputStream b=new ByteArrayOutputStream();
	for(int x=0;x<10;x++) {
		b.write(("hello"+x).getBytes());
	}
	byte[] by=b.toByteArray();
	//创见内存操作输入流对象
	ByteArrayInputStream b1=new ByteArrayInputStream(by);
	int by1=0;
	while((by1=b1.read())!=-1) {
		System.out.print((char)by1);
	}	
}
}
打印流
  字符打印流(针对文本进行操作:PrintWriter)
  字节打印流(printStream 和标准输出流有关系 System.out;)  
 PrintWriter:属于输出流
  1)只能写数据(只能针对目的地文件进行操作),不能读数据(不能针对源文件进行操作)
  2)可以针对文件直接进行操作
  如果一个类中的构造方法里面有File对象或者String类型数据,这个类可以对文本文件直接操作
  FileInputStream
  FileOutputStream
  FileWriter
  FileReader..
  PrintWriter
  3)自动刷新功能::PrintWriter(OutputStream out/Writer out,boolean autoflush);第二个参数如果是true 表示启动自动刷新功能
  4)打印的方法:print(XXX x)/println(XXX  xx)
 举例:
public class PrintWriterDemo1 {
public static void main(String[] args) throws IOException {
    PrintWriter pw=new PrintWriter(
    		new FileWriter("pw.txt"));  	
    pw.println("hello");
    pw.close();
}
}
注释:本身在启动的时候就刷新了
源文件:StringDemo.java --->BufferedReader

目的文件:Copy.java   ----->PrintWriter

举例:

public class CopyFileDemo {
public static void main(String[] args) throws IOException {
	//封装文件
	BufferedReader br=new BufferedReader(
			new FileReader("bw.txt"));
	PrintWriter pw=new PrintWriter(
			new FileWriter("Copy.java"),true);
	String line=null;
	while((line=br.readLine())!=null) {
		pw.println(line);
	}
	pw.close();
	br.close();
}
}
标准的输入输出流
  InputStream in = System.in 
  PrintStream out = Syste.out ;
 jdk5以后,Java--->Scanner(InputStream in)
  键盘录入
  1)Scanner

  2)BufferedReader里面包装字符转换输入流,包装System.in

public class SystemInDemo {
public static void main(String[] args) throws IOException {
	//标注输入流
	BufferedReader br=new BufferedReader(
			new InputStreamReader(System.in));
	System.out.println("请输入一个字符串:");
	String line=br.readLine();
	System.out.println("你输入的字符串是:"+line);
	System.out.println("请输入一个整数:");
	String s=br.readLine();
	int num=Integer.parseInt(s);
	System.out.println("您输入的数字是:"+num);	
}
}

标准输出流

PrintStream ps = System.out ; 

        使用BufferedWriter 去包装System.out

举例:

public class SystemOutDemo {
public static void main(String[] args) throws IOException {
	PrintStream ps=System.out;
	ps.println("我爱高圆圆");
	System.out.println("------");
	BufferedWriter bw=new BufferedWriter(
			new OutputStreamWriter(System.out));
	bw.write("hello");
	bw.newLine();
	bw.write("world");
	bw.newLine();
	bw.flush();
	bw.close();
}
}
SequenceInputStream 表示其他输入流的逻辑串联(合并流) 
 构造方法
 public SequenceInputStream(InputStream s1, InputStream s2)
 复制文件
   a.txt----->b.txt
c.txt----->d.txt
  现在的需求:
   a.txt+b.txt--->c.txt

  StringDemo.java+SystemInDemo.java---->Copy.java

举例:

public class SequenceDemo1 {
public static void main(String[] args) throws IOException {
	//封装源文件
	FileInputStream fis1=new FileInputStream("a.txt");
	FileInputStream fis2=new FileInputStream("aa.txt");
	//创建合并流对象
	SequenceInputStream  sis=new SequenceInputStream(fis1,fis2);
	BufferedOutputStream bos=new BufferedOutputStream(
			new FileOutputStream("Copy2.txt"));
	byte[] b=new byte[1024];
	int len=0;
	while((len=sis.read(b))!=-1) {
		bos.write(b,0,len);
	}
	sis.close();
	bos.close();
}
}
sequenceInputStream另一种构造方式
 public SequenceInputStream(Enumeration e)
  将多个输入流对象进行合并

  a.txt+b.txt+c.txt--->d.txt

举例:

public class SequenceDemo2 {
public static void main(String[] args) throws IOException {
	Vector<InputStream> v=new Vector<InputStream>();
	InputStream fis1=new FileInputStream("a.txt");
	InputStream fis2=new FileInputStream("aa.txt");
    InputStream fis3=new FileInputStream("b.txt");
	v.add(fis1);
	v.add(fis2);
	v.add(fis3);
	Enumeration<InputStream> e = v.elements();
	SequenceInputStream sis=new SequenceInputStream(e);
	BufferedOutputStream bos=new BufferedOutputStream(
			new FileOutputStream("Copy10.txt"));
	byte[] b=new byte[1024];
	int len=0;
	while((len=sis.read(b))!=-1) {
		bos.write(b,0,len);
		bos.flush();
	}
	bos.close();
	sis.close();	
}
}

 

猜你喜欢

转载自blog.csdn.net/wt5264/article/details/80585158